Operating System 5 CONCURRENCY: MUTUAL EXCLUSION AND SYNCHRONIZATION.
Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and...
Transcript of Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and...
![Page 1: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/1.jpg)
Concurrency and Synchronization
CS 447Monday 3:30-5:00Tuesday 2:00-3:30
Prof. R K JoshiCSE, IIT Bombay
![Page 2: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/2.jpg)
Interleaving in a multiprogamming environment
� A scenario� Observe the interleaving of execution
![Page 3: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/3.jpg)
Consider the following processes
P0: v=read(counter); v=v+1; write(counter,v);
P1: v=read(counter); v=v+1; write(counter,v); ...
Pn: v=read(counter); v=v+1; write(counter,v);
������������� ������������������
������������� ������������������������� ���������������
������������� ������������������������������������ �����������
![Page 4: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/4.jpg)
An execution trace
0--0P0::read0
counterP1::v P0::vStatement executed
Time slice
111P0::write5
111P0::=4
110P1::write3
010P1::=2
000P1::read1
![Page 5: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/5.jpg)
Observation
Value of counter is incorrectConcurrent execution through interleaving
No control over access
Mutual exclusion requirement
![Page 6: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/6.jpg)
Critical Section
� CS = Code that accesses a shared resource� This section of code is a critical section� Critical section needs to be protected
from violation of mutual exclusion� i.e. CS needs to execute mutually
exclusively over other CSs� Critical sections operate on one or more
of the same shared resource
![Page 7: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/7.jpg)
A Critical Section Protocol
while (true) {protocol enter CS
Critical section code
protocol exit CS}
while (true) {protocol enter CS
Critical section code
protocol exit CS}
P1P0
![Page 8: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/8.jpg)
What constitutes the enter and exit protocols?
� Interrupt based CS� Signaling and messaging� Shared variables + atomic R/W
operations� Semaphores� Monitors� Spin locks
![Page 9: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/9.jpg)
What properties must your critical section protocols guarantee
� Correctness of mutual exclusion � Progressiveness� Freedom from deadlocks� Freedom from livelocks� Freedom from starvation
![Page 10: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/10.jpg)
Shared variables and atomic R/W based solutions
� Processes use shared variables� They may use local varibles� They perform local computations� They decide locally based on the
shared state (variables) on whether their entry code is successful
� After the CS, they execute CS exit code
![Page 11: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/11.jpg)
Taking Turns
while (true) {while (Turn!=1)
Critical section code
Turn=1;}
while (true) {while (Turn!=0);
Critical section code
Turn=0;}
P1P0
Shared variable Turn=0
![Page 12: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/12.jpg)
Taking Turns
while (true) {while (Turn!=1)
Critical section code
Turn=1; Turn=0;}
while (true) {while (Turn!=0);
Critical section code
Turn=0; Turn=1;}
P1P0
Shared variable Turn=0
![Page 13: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/13.jpg)
Observations and Problems faced
� Mutual exclusion requirement is guaranteed
� The solution violates the progressiveness property
� If a process is not interested in CS, there is no progress
� Progressiveness – Uninterested processes must not hold interested process from entering CS
![Page 14: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/14.jpg)
An improvisation
while (true) {Willing[1]=1;while (Willing[0]);
Critical section code
Willing[1]=0;}
while (true) {Willing[0]=1;while (Willing[1]);
Critical section code
Willing[0]=0;}
P1P0
Shared Willing[0]=0;Shared Willing[1]=0;
![Page 15: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/15.jpg)
Observations and Problems faced
� Correctness of ME guaranteed� It is progressive� But possibility of a deadlock
![Page 16: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/16.jpg)
Observations and Problems faced
� Correctness of ME guaranteed� It is progressive� But possibility of a deadlock
![Page 17: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/17.jpg)
Observations and Problems faced
� Correctness of ME guaranteed� It is progressive� But possibility of a deadlock
![Page 18: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/18.jpg)
Observations and Problems faced
� Correctness of ME guaranteed� It is progressive� But possibility of a deadlock
![Page 19: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/19.jpg)
Observations and Problems faced
� Correctness of ME guaranteed� It is progressive� But possibility of a deadlock
![Page 20: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/20.jpg)
Observations and Problems faced
� Correctness of ME guaranteed� It is progressive� But possibility of a deadlock
P0 P1
![Page 21: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/21.jpg)
Observations and Problems faced
� Correctness of ME guaranteed� It is progressive� But possibility of a deadlock
Willing[0]
Willing[1]
P0 P1
![Page 22: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/22.jpg)
An Attempt(erroneous: firstly, the entry code does not allow entry to CS)
while (true) {Willing[1]=1;do if !(willing[0] AND willing[1])
while (Willing[0]);while (!willing[1);
Critical section code
Willing[1]=0;}
while (true) {do
Willing[0]=1;if !(willing[0] AND willing[1])
while (Willing[1]);while (!willing[0]);
Critical section code
Willing[0]=0;}
P1P0
Shared Willing[0]=0;Shared Willing[1]=0;
![Page 23: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/23.jpg)
Towards removing deadlock possibility
while (true) {Willing[1]=1;while (Willing[0])Do Something!;
Critical section code
Willing[1]=0;}
while (true) {Willing[0]=1;while (Willing[1])
Do Something!;
Critical section code
Willing[0]=0;}
P1P0
Shared Willing[0]=0;Shared Willing[1]=0;
![Page 24: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/24.jpg)
Improvisation
while (true) {Willing[1]=1;while (willing [0]) {
Willing[1]=0;sleep for some timeWilling [1]=1;
}Critical section code
Willing[1]=0;}
while (true) {Willing[0]=1;while (willing [1]) {
Willing[0]=0;sleep for some timeWilling [0]=1;
}Critical section code
Willing[0]=0;}
P1P0
Shared Willing[0]=0;Shared Willing[1]=0;
![Page 25: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/25.jpg)
Observations and problems faced
� Solves the deadlock problem
� But a livelock may occur
� Processes may get locked forever in a cycle of release-wait-hold
![Page 26: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/26.jpg)
How to remove the livelockpossibility?
� Why make both processes retract?
� Let only one process retract if it senses that livelock is possible
![Page 27: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/27.jpg)
Solution that is deadlockfree, progressive, but livelock-prone
while (true) {Willing[1]=1;while (willing [0]) {
Willing[1]=0;sleep for some timeWilling [1]=0;
}Critical section code
Willing[1]=0;}
while (true) {Willing[0]=1;while (willing [1]) {
Willing[0]=0;sleep for some timeWilling [0]=1;
}Critical section code
Willing[0]=0;}
P1P0
Shared Willing[0]=0;Shared Willing[1]=0;
![Page 28: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/28.jpg)
Possibility of Livelock is removed
while (true) {Willing[1]=1;while (willing [0]);
Critical section code
Willing[1]=0;}
while (true) {Willing[0]=1;while (willing [1]) {
Willing[0]=0;sleep for some timeWilling [0]=1;
}Critical section code
Willing[0]=0;}
P1P0
Shared Willing[0]=0;Shared Willing[1]=0;
![Page 29: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/29.jpg)
Possibility of Livelock is removed
while (true) {Willing[1]=1;while (willing [0]);
Critical section code
Willing[1]=0;}
while (true) {Willing[0]=1;while (willing [1]) {
Willing[0]=0;sleep for some timeif (!Willing[1]) Willing [0]=1;
}Critical section code
Willing[0]=0;}
P1P0
Shared Willing[0]=0;Shared Willing[1]=0;
![Page 30: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/30.jpg)
It’s unfair to P0!
� Why should it be P0 to withdraw all thetime!
And
� P0 may have to withdraw its willingness forever in a specific interleaving sequence � Starvation
![Page 31: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/31.jpg)
Dekker’s Algorithm (1965)
� Starvation free� Livelock free� Deadlock free� Progressive
![Page 32: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/32.jpg)
Attempt to remove starvation
while (true) {Willing[1]=1;if (turn=1)
while (willing [0]) {Willing[1]=0;while (Willing[0]);Willing[1]=1;
}Else while (Willing[0]);
Critical section code
Willing[1]=0; turn=0;}
while (true) {Willing[0]=1;if (turn=0)
while (willing [1]) {Willing[0]=0;while (Willing[1]);Willing[0]=1;
}Else while (Willing[1]);
Critical section code
Willing[0]=0; turn=1;}
P1P0
Shared Willing[0]=0; Shared turn=0;Shared Willing[1]=0;
![Page 33: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/33.jpg)
Attempt to remove starvation - II
while (true) {Willing[1]=1;if (turn=1)
while (willing [0]) {Willing[1]=0;while (Willing[0]);Willing[1]=1;
}Else while (Willing[0]);
Critical section code
Willing[1]=0; turn=1;}
while (true) {Willing[0]=1;if (turn=0)
while (willing [1]) {Willing[0]=0;while (Willing[1]);Willing[0]=1;
}Else while (Willing[1]);
Critical section code
Willing[0]=0; turn=0;}
P1P0
Shared Willing[0]=0; Shared turn=0;Shared Willing[1]=0;
![Page 34: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/34.jpg)
Attempt to remove starvation - III
while (true) {Willing[1]=1;
while (willing [0]) if (turn=1) {Willing[1]=0;while (Willing[0]);Willing[1]=1;}else while (Willing[0]);
Critical section code
Willing[1]=0; turn=0;}
while (true) {Willing[0]=1;
while (willing [1]) if (turn=0) {Willing[0]=0;while (Willing[1]);Willing[0]=1;}else while (Willing[1]);Critical section code
Willing[0]=0; turn=1;}
P1P0
Shared Willing[0]=0; Shared turn=0;Shared Willing[1]=0;
![Page 35: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/35.jpg)
Possibility of Livelock is removed
while (true) {Willing[1]=1;while (willing [0]);
Critical section code
Willing[1]=0;}
while (true) {Willing[0]=1;while (willing [1]) {
Willing[0]=0;while (Willing[1]);Willing[0]=1;
}Critical section code
Willing[0]=0;}
P1P0
Shared Willing[0]=0;Shared Willing[1]=0;
![Page 36: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/36.jpg)
Dekker’s Algorithm
while (true) {Willing[1]=1;while (willing [0]) {
if (arbitrator=0) Willing[1]=1;while (arbitrator=0);Willing [1]=1;
}Critical section code
Willing[1]=0; arbitrator=0;}
while (true) {Willing[0]=1;while (willing [1]) {
if (arbitrator=1) Willing[0]=0;while (arbitrator=1);Willing [0]=1;
}Critical section code
Willing[0]=0; arbitrator=1;}
P1P0
Shared Willing[0]=0; Shared arbitrator=0;Shared Willing[1]=0;
![Page 37: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/37.jpg)
Hammer in place of a screw driver?
� Can we design something simpler?� After all we need freedom from
� Non-progressive blockages!� Deadlocks!� Livelocks!� Starvation!
![Page 38: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/38.jpg)
Peterson’s Algorithm (around 1986)
while (true) {Willing[1]=1;arbitrator=0;while (willing [0]) AND (arbitrator=0);
Critical section code
Willing[1]=0;}
while (true) {Willing[0]=1;arbitrator=1;while (willing [1]) && (arbitrator=1);
Critical section code
Willing[0]=0;}
P1P0
Shared Willing[0]=0; Shared arbitrator=0;Shared Willing[1]=0;
![Page 39: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/39.jpg)
N process solution
� Lamport’s Bakery Algorithm
� A process picks up a token number
� They all go with their critical sections according to the order defined by the token numbers
� …..
![Page 40: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/40.jpg)
Towards Lamport’s Bakery Algorithm
Pickup a sequence number for itself;Wait for some condition;
CS
Reset to old state
Pi
Shared what’s shared?
![Page 41: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/41.jpg)
Towards Lamport’s Bakery Algorithm
Myseqno = current + 1Wait for some condition;
CS
Reset to old state
Pi
Shared current;
![Page 42: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/42.jpg)
Towards Lamport’s Bakery Algorithm
Willing [i] = 1while (current!=i);
CS
Willing[i]=0Current= min (I, willing[i]=1 over i=0..N-1)
Pi
Shared current=0;Shared willing [0..N-1];
![Page 43: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/43.jpg)
Towards Lamport’s Bakery Algorithm
seqno [i] = currentCurrent = current + 1For j=0; j<I; j++
While (seqno [j] < = seqno [i]) ;For (j=i+1; j<N; j++)
while (seqno[j] < seqno [i]);CS
Seqno [i] = MAX
Pi
Shared seqno[0..N-1] = MAXShared current = 0
![Page 44: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/44.jpg)
Towards Lamport’s Bakery Algorithm
Myseqno = max (seqno [0..N-1]) + 1
For (j=0; j<N; j++) while (seqno [j]!=0) AND ( (seqno [j] < seqno[i]) OR( (j<i) AND (seqno[i]=seqno[j])) ;
CS
Reset to old state
Pi
Shared seqno [0..N-1] =0Shared scanning [0..N-1]=0
![Page 45: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/45.jpg)
Lamport’s Bakery Algorithm
choosing[i]=true;sequenceNo [i] = max (sequenceNo[0]...sequenceNo[N-1])+1;choosing[i]=false;For j=0 to n-1
while (choosing[j]);while (sequenceNo[j] !=0) AND ( (sequenceNo [i] > sequenceNo[j]) OR (sequenceNo[i]=sequenceNo[j] AND i>j) );
CSsequenceNo[i]=0;
Pi
Shared sequenceNo[0..N-1]=0; Shared choosing [0..N-1] =0
![Page 46: Basics of Concurrency and SynchronizationMicrosoft PowerPoint - Basics of Concurrency and Synchronization Author Administrator Created Date 10/13/2003 23:18:16 ...](https://reader036.fdocuments.net/reader036/viewer/2022062507/5fdedd4e41edae15d57dd3d3/html5/thumbnails/46.jpg)
What are the drawbacks of the algorithmic solutions?
� i.e. solutions with shared variables and atomic read and write?� Scalability: No of processes is to be known
statically� Busy wait� Responsibility of implementation is with user
� Pointers to OS-supported solution?