Transcript 幻灯片 1
Solutions of a Problem in Concurrent Programming Proseminar Software Pioneers By Prof. Dr. Heike Wehrheim Diplo. Inform. Björn Metzler Solutions of a Problem in Concurrent Programming - Yang Le 1 Contents • A brief introduction of Dijkstra and Lamport • Problem in Concurrent Programming • Dijkstra’s Solution • Semaphores Solution • Lamport’s Solution • Conclusion Solutions of a Problem in Concurrent Programming - Yang Le 2 Edsger W. Dijkstra and Leslie Lamport • Photographs are from google photo Solutions of a Problem in Concurrent Programming - Yang Le 3 Problem in Concurrent Programming • N computers • one “critical section”. 4 5 3 • Two requirements: Critical Section • Communication between computers • Undividable writing or reading 2 N 1 Solutions of a Problem in Concurrent Programming - Yang Le 4 Basic Goals of Dijkstra’s Solution • Symmetric between N computers • No “After you” – “After you” – blocking Please, you can go first. No, no. I will wait after you. i j Solutions of a Problem in Concurrent Programming - Yang Le 5 Dijkstra’s Solution Part I: Interger j; Li0:b[i]:=false; Li1:if k≠i then Li2: begin c[i]:=true; Li3: if b[k] then k:=i; goto Li1; end else . b[i] . c[i] Critical Section 1 2 3 i N 1 2 3 i N T T T F T T T T T T k i Code from “Solution of a Problem in Concurrent Programming Control” – E.W.Dijkstra Solutions of a Problem in Concurrent Programming - Yang Le 6 Dijkstra’s Solution Part II: Li4:begin c[i]:=false; for j:=1 step 1 until N do if j≠i and not c[j] then goto Li1 end; critical section; c[i]:=true;b[i]:=true; . . Critical Section i 1 2 3 N 1 2 3 i N b[i] T T T F T c[i] T T T F T go to Li0 k i Code from “Solution of a Problem in Concurrent Programming Control” – E.W.Dijkstra Solutions of a Problem in Concurrent Programming - Yang Le 7 Problems in Dijkstra’s Solution • When the i-th computer breaks down in the critical section, the whole system will be in a dead lock ??? ? Critical Section i 1 2 3 What is he doing for such a long time? Hey, we have waited too long! N Who knows. Just have to wait here. Solutions of a Problem in Concurrent Programming - Yang Le 8 Semaphores Solution • Semaphore values begin semaphore mutex; mutex := 1; • Two “indivisible actions” • • • parbegin begin L1: P(mutex); P- operation V- operation critcal section 1; V(mutex); remainder of cycle 1; Random choosing from the waiting list go to L1 end; begin L2: P(mutex); critical section 2; V(mutex); remainder of cycle 2; parendend go to L2 end; end Solutions of a Problem in Concurrent Programming - Yang Le 9 Problems of Semaphores Solution • We can no forecast or decide which process will be removed from the waiting list. • Explicit control - deadlock ????? Semaphore i What is he waiting for? And why make him the boss? Don’t know. Just wait and see. Solutions of a Problem in Concurrent Programming - Yang Le j 10 Lamport’s Solution Begin integer j; L1: choosing[i]:=1; number[i]:=1+maximum(number[1],...number[N]); choosing[i]:=0; for j:=1 step 1 until N do begin L2: if choosing[j]≠0 then goto L2; L3: if number[j]≠0 and (number[j],j)< (number[i],i) then goto L3; end Critical section; Number[i]:=0; 1 Noncritical section; Goto L1; end 1 Critical Section 2 3 i N 2 3 i N Choosing[] 0 0 0 0 0 Number[] 6 3 4 2 5 Solutions of a Problem in Concurrent Programming - Yang Le 11 Lamport’s Solution Critical Section Begin integer j; L1: choosing[i]:=1; number[i]:=1+maximum(number[1],...number[N]); choosing[i]:=0; for j:=1 step 1 until N do begin L2: if choosing[j]≠0 then goto L2; L3: if number[j]≠0 and (number[j],j)< (number[i],i) then goto L3; end Critical section; 1 Number[i]:=0; Noncritical section; 1 Goto L1; 2 3 i N end i 2 3 N Choosing[] 0 0 0 0 0 Number[] 6 3 4 2 5 Solutions of a Problem in Concurrent Programming - Yang Le 12 Problems in Lamport’s Solution • These computers are no longer symmetrical. • The number attribute will be too big. Critical Section i Let’s compare our “number” attribute, then decide who goes to first. No, I can’t. This is too big to fit in a double integer. j Solutions of a Problem in Concurrent Programming - Yang Le 13 Conclusion • Dijkstra’s Solution • • • Semaphores Solution • • • N computers get equal chances, and avoid “after you” – “after you” – blocking. Weak point: Once one computer breaks down inside the CS, that will be a catastrophe. Based on a semaphore value with two undividable action – P and V. Weak point: Too reliance upon a single hardware component. Lamport’s Solution • • Every computer got its own attribute and don’t afraid “break-down” in CS. This attribute will be too huge and increase the system’s load. Solutions of a Problem in Concurrent Programming - Yang Le 14