160 likes | 192 Views
6. Mutual Exclusion. CSC 410 Operating Systems. Mutual Exclusion in Software.
E N D
6. Mutual Exclusion CSC 410 Operating Systems
Mutual Exclusion in Software When multiple processes share a common memory only one process can be allowed to access this memory at any given time. Each process accessing the shared data excludes all others from doing so at the same time. This is called mutual exclusion. When a process is accessing shared data, the process is said to be in its critical section (or critical region). To enforce mutual exclusion only one process may be allowed in its critical region at any one time. The exponential growth-rate in distributed systems has created the need for a software-only mechanism for enforcing mutual exclusion. This means that we cannot expect to have such mechanisms implemented in the hardware design of every computer system we encounter. Furthermore the software-only solution must be implementable in a HLL that can be easily ported to any platform and run on any OS.
Mutual Exclusion Program #1 Global variable: Pnum:=1 procedure P1 begin while true do begin stuff; while Pnum=2 do; critical_region; Pnum:=2; other_stuff; end; end P1; procedure P2 begin while true do begin stuff; while Pnum=1 do; critical_region; Pnum:=1; other_stuff; end; end P2; Problem: ?
Mutual Exclusion Program #2 Global variables: P1in:=false, P2in:=false procedure P2 begin while true do begin stuff; while P1in do; P2in:=true; critical_region; P2in:=false; other_stuff; end; end P2; procedure P1 begin while true do begin stuff; while P2in do; P1in:=true; critical_region; P1in:=false; other_stuff; end; end P1; Problem: ?
Mutual Exclusion Program #3 Global variables: P1wantsin:=false, P2wantsin:=false procedure P1 begin while true do begin stuff; P1wantsin:=true; while P2wantsin do; critical_region; P1wantsin:=false; other_stuff; end; end P1; procedure P2 begin while true do begin stuff; P2wantsin:=true; while P1wantsin do; critical_region; P2wantsin:=false; other_stuff; end; end P2; Problem: ?
Mutual Exclusion Program #4 Global variables: P1wantsin:=false, P2wantsin:=false procedure P1 begin while true do begin stuff; P1wantsin:=true; while P2wantsin do begin P1wantsin:=false; delay(random); P1wantsin:=true; end; critical_region; P1wantsin:=false; other_stuff; end; end P1; procedure P2 begin while true do begin stuff; P2wantsin:=true; while P1wantsin do begin P2wantsin:=false; delay(random); P2wantsin:=true; end; critical_region; P2wantsin:=false; other_stuff; end; end P2; Problem: ?
Mutual Exclusion Program #5 Globals: P1wantsin:=false, P2wantsin:=false, favored(p1,p2):=p1 procedure P1 begin while true do begin stuff; P1wantsin:=true; while P2wantsin do begin if favored=p2 thenbegin p1wantsin:=false; while favored=p2 do; p1wantsin:=true; end; critical_region; favored:=p2; P1wantsin:=false; other_stuff; end; end P1; procedure P2 begin while true do begin stuff; P2wantsin:=true; while P1wantsin do begin if favored=p1 thenbegin p2wantsin:=false; while favored=p1 do; p2wantsin:=true; end; critical_region; favored:=p1; P2wantsin:=false; other_stuff; end; end P2; Problem: ?
@$#%&*!! 42?… number 42? 329 N-Process Mutual Exclusion When we consider n processes sharing memory rather than just two, the problem of mutual exclusion becomes much more complex. An efficient software-only algorithm for enforcing mutual exclusion among n process was developed by L.Lamport in which each process must “take a ticket” or be placed in a queue to wait for access to shared memory. This method is called Lamport’s Bakery Algorithm and is particularly well suited to distributed processing. Develop a software-only mechanism for enforcing mutual exclusion for three processes. (Hint: Consider Lamport’s Bakery Algorithm for n processes.) Turn in hand-written source code for a sample process and any OS software needed.
Semaphores A semaphore is a protected variable whose value can be accessed and altered only by the operations P( ) and V( ) and an initialization operation SemaphoreInit( ). Binary semaphores can have values true or false while counting semaphores can have non-negative integer values. function P(S:semaphore) if S>0 then S:=S-1; else wait_for(S); end if; end P; function V(S:semaphore) ifwaiting_for(S)then release_for(S); else S:=S+1; end if; end V; wait_for(S) adds the process calling P(S) to a queue. waiting_for(S) is a boolean function that checks to see if the queue is empty. release_for(S) releases the process waiting at the front of the queue.
Using Semaphores: Mutual Exclusion procedure P2 is begin while true do begin stuff; P(active); critical_region; V(active); end; end P2; begin example_one semaphore_init(active,1); parbegin P1; P2; parend; end example_one; program example_one is active : semaphore; procedure P1 is begin while true do begin stuff; P(active); critical_region; V(active); end; end P1; The semaphore active is set to one (1) and acts as a boolean to control a process’s access to its critical regions. Discuss why semaphores need special HW implementations?
Using Semaphores: Producer-Consumer Interprocess communication occurs when one process passes data to another process. This is more complex than a procedure call with parameter passing since the two processes do not necessarily share the same address space. The processes must be synchronized which means that the data transfer must occur in the proper time sequence. procedure producer is nextvalue : integer; begin while true dobegin calculate(nextvalue); P(access_numbuffer); numbuffer:=nextvalue; V(access_numbuffer); V(numbuffer_loaded); end; end producer; procedure consumer is nextvalue : integer; begin while true dobegin P(numbuffer_loaded); P(access_numbuffer); nextvalue:=numbuffer; V(access_numbuffer); make_use_of(nextvalue); end; end consumer; Initializations: semaphore_init(access_numbuffer,1), semaphore_init(numbuffer_loaded,0); Discuss the effects on operation of these procedures for the cases in which producer is (much faster, much slower) than consumer.
Mutual Exclusion with Test-and-Set When the computer HW can be designed to support the enforcement of mutual exclusion, the complexity of the resulting SW is greatly reduced. The indivisible instruction testandset(a,b)reads the value of a boolean b, copies it into a and then sets b to true all withing the span of a single uninterrup- tible instruction. Initially let go:=false as you verify that mutual exclusion is enforced between the process P1 and P2 below: procedure P1 no_P1 : boolean; begin while true do begin no_P1:=true; while no_P1 do testandset(no_P1,go); critical_region; go:=false; other_stuff; end; end P1; procedure P2 no_P2 : boolean; begin while true do begin no_P2:=true; while no_P2 do testandset(no_P2,go); critical_region; go:=false; other_stuff; end; end P2;
Parallel Processing: parbegin/parend The constructs parbegin and parend are used to enable multiple threads of control. Consider how these are used to parallelize the mathematical expression shown below: x:=(-b +(b**2-4*a*c)**0.5)/(2*a); Assuming that we have unlimited processors, what is the minimum number of computational steps necessary to compute a value for x? b b b 4 a 2 a Step 0 -1 Step 1 4 threads * * * c * Step 2 1 thread * Step 3 1 thread - .5 Step 4 1 thread ** Step 5 1 thread + Step 6 1 thread * X
Parallelized Code for Example Problem parbegin temp1:=-b; temp2:=b**2; temp3:=4*a; temp4:=2*a; parend; temp5:=temp3*c; temp5:=temp2-temp5; temp5:=temp5**0.5; temp5:=temp1+temp5; X:=temp5/temp4; In this case, the four operations within the parbegin/parend construct are evaluated in parallel - the remaining five operations must still be performed sequentially. By performing the calculation in parallel, it is possible to reduce the real execution time substantially. Consider the increase in OS overhead necessary to implement the parbegin/parend on a distrubuted computer network. 1 2 3 4 5 6
Parallelization Homework Write parallelized code for each of the following expressions. Attempt to minimize the number of steps needed. (1) y:= 4*X**3+2*X**2-6*x+12; (2) Z:= (X + 3*Y)/(2*W - X**V); (3) P:= (X-Y)**5; (4) P:= (X-Y)*(X-Y)*(X-Y)*(X-Y)*(X-Y); (5) R:= 1+X/(1+Y/(1+Z/(1+W))); (6) Y:= 12+X*(-6+X*(2+X*4)); Compare the complexity of problem (1) with problem (6) and compare problem (3) with problem (4).