Lect 25 Synchronization
Lect 25 Synchronization
while (!flag);
print x
• Thread 2 performs
x = 100;
flag = true
• What is the expected output?
Friday, October 20, 2023 Biju K Raveendran @ BITS Pilani Goa 16
Peterson’s Solution – Algorithm 3
• 100 is the expected output.
• However, the operations for Thread 2 may be reordered:
flag = true;
x = 100;
• If this occurs, the output may be 0!
• The effects of instruction reordering in Peterson’s Solution
• This allows both processes to be in their critical section at the same time!
Friday, October 20, 2023 Biju K Raveendran @ BITS Pilani Goa 17
Bakery Algorithm
• Critical section for ‘n’ processes
– Before entering its critical section, process receives a number.
Holder of the smallest number enters the critical section.
– If processes Pi and Pj receive the same number, if i < j, then Pi is
served first; else Pj is served first.
– The numbering scheme always generates numbers in non-
decreasing order of enumeration; i.e., 1,2,3,3,3,3,4,5...