0% found this document useful (0 votes)
17 views20 pages

Lect 25 Synchronization

The document discusses solutions to the critical section problem in operating systems. It describes the general structure of a process and the requirements for solving critical section problems, including mutual exclusion, progress, and bounded waiting. It then summarizes several algorithms for handling critical sections, including Peterson's algorithm which uses shared variables to guarantee the three requirements are met. The document notes Peterson's algorithm may not work reliably on modern architectures due to instruction reordering. It also describes the Bakery algorithm which assigns processes numbers to determine the order of entering the critical section.

Uploaded by

Alben D'Souza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views20 pages

Lect 25 Synchronization

The document discusses solutions to the critical section problem in operating systems. It describes the general structure of a process and the requirements for solving critical section problems, including mutual exclusion, progress, and bounded waiting. It then summarizes several algorithms for handling critical sections, including Peterson's algorithm which uses shared variables to guarantee the three requirements are met. The document notes Peterson's algorithm may not work reliably on modern architectures due to instruction reordering. It also describes the Bakery algorithm which assigns processes numbers to determine the order of entering the critical section.

Uploaded by

Alben D'Souza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

OPERATING SYSTEMS CS F372

BIJU K RAVEENDRAN & TEAM

LECT #25: SYNCHRONIZATION


General Structure of a Process Pi
Only 2 processes say P0 and P1
General structure of Process Pi will be
do{
ENTRY SECTION
CRITICAL SECTION
EXIT SECTION
REMAINDER SECTION
} while(1);
Processes may share some common variable to
synchronize there action.
Friday, October 20, 2023 Biju K Raveendran @ BITS Pilani Goa 2
Solution to Critical Section Problem
1. Mutual Exclusion - If process Pi is executing in its critical section, then
no other processes can be executing in their critical sections
2. Progress - If no process is executing in its critical section and there
exist some processes that wish to enter their critical section, then
the selection of the processes that will enter the critical section next
cannot be postponed indefinitely
3. Bounded Waiting - A bound must exist on the number of times that
other processes are allowed to enter their critical sections after a
process has made a request to enter its critical section and before
that request is granted
 Assume that each process executes at a nonzero speed
 No assumption concerning relative speed of the N processes
Friday, October 20, 2023 Biju K Raveendran @ BITS Pilani Goa 3
Critical Section Handling in OS
• Two approaches depending on if kernel is preemptive or
non- preemptive
– Preemptive – allows preemption of process when
running in kernel mode
– Non-preemptive – runs until exits kernel mode,
blocks, or voluntarily yields CPU
• Essentially free of race conditions in kernel mode

Friday, October 20, 2023 Biju K Raveendran @ BITS Pilani Goa 4


Algorithm 1
 Shared variables:
– int turn;
initially turn = 0
– turn - i  Pi can enter its critical section
 Process Pi
do {
while (turn !=i);
critical section
turn = j;
reminder section
}while(1);

Friday, October 20, 2023 Biju K Raveendran @ BITS Pilani Goa 5


Algorithm 1
Shared variable int turn (initial value 0)
Process 0 Process 1
do { do {
while (turn !=0); // do nothing while (turn !=1); // do nothing
<critical section> <critical section>
turn = 1; turn = 0;
<reminder section > <reminder section >
}while(1); }while(1);

 Satisfies mutual exclusion, but not progress


Friday, October 20, 2023 Biju K Raveendran @ BITS Pilani Goa 6
Algorithm 1
• This solution guarantees mutual exclusion
• Drawback 1: processes must strictly alternate
– Pace of execution of one process is determined by pace
of execution of other processes
• Drawback 2: if one processes fails other process is
permanently blocked
• This problem arises due to fact that it stores name of the
process that may enter critical section rather than the
process state

Friday, October 20, 2023 Biju K Raveendran @ BITS Pilani Goa 7


• Shared variables Algorithm 2
– Boolean flag[2];
initially flag [0] = flag [1] = false.
– flag [i] = true  Pi ready to enter its critical section
• Process Pi
do { flag[i] = true;
while (flag[j]);
critical section
flag [i] = false;
remainder section
}while(1);
Friday, October 20, 2023 Biju K Raveendran @ BITS Pilani Goa 8
Algorithm 2
Shared variable Boolean flag[2] (flag[0] & flag[1] initial value false)
Process 0 Process 1
do { do {
flag[0]=true; flag[1]=true;
while (flag[1]); // do nothing while (flag[0]); // do nothing
<critical section> <critical section>
flag[0]=false; flag[1]=false;
<reminder section > <reminder section >
}while(1); }while(1);

Satisfies mutual exclusion, but not progress requirement.


If flag[0]=flag[1]=true  infinite loop
Friday, October 20, 2023 Biju K Raveendran @ BITS Pilani Goa 9
Algorithm 2
• This approach Satisfy mutual exclusion
• This approach may lead to dead lock
What is wrong with this implementation?
• A process sets its state without knowing the state of other.
Dead lock occurs because each process can insist on its
right to enter critical section
• There is no opportunity to back off from this situation

Friday, October 20, 2023 Biju K Raveendran @ BITS Pilani Goa 10


Peterson’s Solution – Algorithm 3
• Not guaranteed to work on modern architectures! (But good algorithmic
description of solving the problem)
• Two process solution
• Assume that the LOAD and STORE instructions are atomic; that is, cannot be
interrupted.
• The two processes share two variables:
– int turn;
– Boolean flag[2]
• The variable turn indicates whose turn it is to enter the critical section.
• The flag array is used to indicate if a process is ready to enter the critical section.
flag[i] = true implies that process Pi is ready!

Friday, October 20, 2023 Biju K Raveendran @ BITS Pilani Goa 11


Algorithm 3
• Combined shared variables of algorithms 1 and 2.
• Process Pi
do {
flag [i] = true;
turn = j;
while (flag [j] && turn = = j);
critical section
flag [i] = false;
remainder section
}while(1);
Friday, October 20, 2023 Biju K Raveendran @ BITS Pilani Goa 12
Algorithm 3
Shared variable Boolean flag[2] (flag[0] & flag[1] initial value false)
and turn (initial value 0)
Process 0 Process 1
do { do {
flag[0]=true; turn = 1; flag[1]=true; turn = 0;
while (flag[1] && turn ==1); while (flag[0] && turn ==0);
// do nothing // do nothing
<critical section> <critical section>
flag[0]=false; flag[1]=false;
<reminder section > <reminder section >
}while(1); }while(1);
Meets all three requirements
Solves the critical-section problem for two processes.
Friday, October 20, 2023 Biju K Raveendran @ BITS Pilani Goa 13
Peterson’s Solution – Algorithm 3
• Provable that the three CS requirement are met:
1. Mutual exclusion is preserved
Pi enters CS only if:
either flag[j] = false or turn = i
2. Progress requirement is satisfied
3. Bounded-waiting requirement is met

Friday, October 20, 2023 Biju K Raveendran @ BITS Pilani Goa 14


Peterson’s Solution – Algorithm 3
• Although useful for demonstrating an algorithm, Peterson’s Solution is not
guaranteed to work on modern architectures.
• Understanding why it will not work is also useful for better understanding race
conditions.
• To improve performance, processors and/or compilers may reorder operations
that have no dependencies.
• For single-threaded this is ok as the result will always be the same.
• For multithreaded the reordering may produce inconsistent or unexpected
results!

Friday, October 20, 2023 Biju K Raveendran @ BITS Pilani Goa 15


Peterson’s Solution – Algorithm 3
• Two threads share the data:

boolean flag = false;


int x = 0;
• Thread 1 performs

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...

Friday, October 20, 2023 Biju K Raveendran @ BITS Pilani Goa 18


Bakery Algorithm
• Notation
– lexicographical order(ticket #, process id #)
– (a,b) < (c,d) if a < c or if a = c and b < d
– max (a0,…, an-1) is a number, k, such that k  ai
for i = 0,…., n – 1
• Shared data
int number[n]; // initialized to 0

Friday, October 20, 2023 Biju K Raveendran @ BITS Pilani Goa 19


Bakery Algorithm
do {
number[i] = max(number[0], number[1], …,
number [n – 1])+1;
for ( j = 0 ; j < n ; j++ ) {
while ( number[j] != 0) &&
( number [ j , j ] ) < ( number [ i , i ] ) ;
}
CRITICAL SECTION
number[i] = 0 ;
REMAINDER SECTION
} while(1) ;
Friday, October 20, 2023 Biju K Raveendran @ BITS Pilani Goa 20

You might also like