Os 10
Os 10
CS2006
Lecture 10
Process Synchronization
10th March 2025
while (true) {
/* produce an item in next
produced */
while (true) {
while (counter == 0)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next
consumed */
}
Synchronization Issue
register1 = counter
register1 = register1 + 1
counter = register1
register2 = counter
register2 = register2 - 1
counter = register2
where register1,2 are the local CPU registers.
Synchronization Issue (3)
The concurrent execution of “counter++” and “counter--” is equivalent to a sequential
execution.
Consider this execution interleaving with “counter = 5” initially:
next_free_slota = 7 next_free_slotb = 7
in = 8
7
15 CS-2006 Operating Systems
Race condition
A situation where several processes access
and manipulate the same data concurrently,
and the outcome of the execution depends on
the particular order in which the access takes
place, is called race condition.
Debugging is not easy
Most test runs will run fine
To prevent race conditions, concurrent processes must be
synchronized
Process A
Process B
T1 T2 T3 T4
B attempts to enter B leaves
critical section critical section
Mutual Exclusion
At any given time, only one process is in the critical section
18 CS-2006 Operating Systems
Critical Section
Avoid race conditions by not allowing two processes to
be in their critical sections at the same time
22 CS-2006 Operating
Systems
Solution to CS Problem – Mutual Exclusion
1. Mutual Exclusion – If process Pi is executing in its
critical section, then no other processes can be
executing in their critical sections.
Implications:
Critical sections better be focused and short.
Better not get into an infinite loop in there.
No Interrupt Checking
No Context Switching
Check for
Interrupt:
Start Fetch Next Decode Execute
Instruction Instruction Instruction Process
Interrupt
Halt
Process 1 Process 2
1.while (FLAG == FALSE); 1.while
1.while (FLAG
(FLAG ==
== FALSE);
FALSE);
2.FLAG = FALSE; 2.
2.FLAG = FALSE;
3.critical_section(); 3.critical_section();
4.FLAG = TRUE;
5.noncritical_section();
Timeout
Process 1 Process 2
while(TRUE) while(TRUE)
{ {
// wait for turn // wait for turn
while (turn != 1); while (turn != 2);
critical_section(); critical_section();
turn = 2; turn = 1;
noncritical_section(); noncritical_section();
} }
31 CS-2006 Operating Systems
Strict Alternation
Turn = 1
2
Process 1 Process 2
While(1) While(1)
1.while (Turn != 1); 1.while (Turn != 2);
1.while (Turn != 2);
2.
2.critical_section();
2.critical_section();
3.Turn = 2; 3.Turn = 1;
4.noncritical_section(); 4.noncritical_section();
Timeout
Only one Process is in the Critical Section at a time
33
turn = 1
Strict Alternation
Process 1 Process 2
while(TRUE) while(TRUE)
{ {
// wait // wait
while (turn != 1); while (turn != 2);
critical_section(); critical_section();
turn = 2; turn = 1;
noncritical_section(); noncritical_section();
} }
Process 1
Runs
Enters its critical section
Exits; setting turn to 2.
Process 1 is now in its non-critical section.
Assume this non-critical procedure takes a long time.
Process 2, which is a much faster process, now runs
Once it has left its critical section, sets turn to 1.
Process 2 executes its non-critical section very quickly and returns to the top of the
procedure.
CS-2006 Operating Systems
34
turn = 1
Process 1 Process 2
while(TRUE) while(TRUE)
{ {
// wait // wait
while (turn != 1); while (turn != 2);
critical_section(); critical_section();
turn = 2; turn = 1;
noncritical_section(); noncritical_section();
} }
35
Strict Alternation
What we have is a violation of one of the conditions
that we listed above
Process 0 Process 1
while(TRUE) while(TRUE)
{ {
interested[0] = TRUE; interested[1] = TRUE;
Timeout while(interested[0]!=FALSE);
while(interested[1]!=FALSE);
DEADLOCK
The variable turn indicates whose turn it is to enter the critical section.
The interested array is used to indicate if a process is ready to enter the critical
section. interested[i] = true implies that process Pi is ready
while(TRUE)
{
interested[i] = TRUE;
turn = j;
// wait
while(interested[j]==TRUE && turn == j );
critical_section();
interested[i] = FALSE;
noncritical_section();
}
flag[0],flag[1]:=false
turn := 0;
Process P0: Process P1:
repeat repeat
flag[0]:=true; flag[1]:=true;
// 0 wants in // 1 wants in
turn:= 1; turn:=0;
// 0 gives a chance to 1 // 1 gives a chance to 0
while(flag[1]&&turn==1){}; while(flag[0]&&turn==0){};
CS CS
flag[0]:=false; flag[1]:=false;
// 0 is done // 1 is done
RS RS
forever forever
51
THE BAKERY ALGORITHM
THE BAKERY ALGORITHM
• The following table shows the status of all
the processes as they execute the ‘for’
loops in their entry sections.
• The gray cells show processes waiting in
the second while loops in their entry
sections.
• The table shows that P0 never waits for any
process and is, therefore, the first process
to enter its critical section,
• while all other processes wait in their
second while loops for j = = 0, indicating
that they are waiting for P0 to get out of its
critical section and then they would make
progress (i.e., they will get out the while
loop, increment j by one, and continue their
THE BAKERY ALGORITHM
execution).
• You can make the following observations
by following the Bakery algorithm closely
with the help of the following table:
• P1 not interested to get into its critical
section number[1] is 0
• P2, P3, and P4 wait for P0
• P0 gets into its CS, get out, and sets its
number to 0
• P3 get into its CS and P2 and P4 wait for it
to get out of its CS
• P2 gets into its CS and P4 waits for it to get
out
• P4 gets into its CS
• Sequence of execution of processes:
<P0,P3,P2,P4> THE BAKERY ALGORITHM
THE BAKERY ALGORITHM
References
Operating System Concepts (Silberschatz, 8th edition)
Chapter 6