OS Lec 11 Peterson Soution

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

1

Lahore Garrison University


CSC351-Operating System

Fall 2023
2
Text Book

 Operating system concepts by Abraham


Silberschatz, Galvin, Gagne, 9th edition

Lahore Garrison University


3

►Process Synchronization
► Race Condition
► Critical Section
► Mutual Exclusion
Preamble (Past ► Progress

lesson brief) ► Bounded Waiting

Lahore Garrison University


4
Chapter 5

 Process Synchronization

Lahore Garrison University


5
Peterson’s Solution

Peterson’s Solution is a classical software based solution to the


critical section problem.
 In Peterson’s solution, we have shared variable:
 int turn : The process whose turn is to enter the critical section.

Lahore Garrison University


Solution : Algorithm 1

 Two process solution (Software based solution)

 Shared variables:

 int turn;

initially turn = 0

 turn = i  Pi can enter its critical section


Solution : Algorithm 1

Process P0 Process P1
do { do {
while (turn!=0) ; while (turn!=1) ;
<CS> <CS>
turn = 1; turn = 0;
<RS> <RS>
} while (1); } while (1);

 Problem ? , Does it meet all conditions ?



Solution : Algorithm 2

Two process solution (Software based solution)

Shared variables

 boolean flag[2]; // Set to false

 flag [i] = true  Pi ready to enter its critical section


Solution : Algorithm 2

Process P0 Process P1
do { do {
flag[0] = true; flag[1] = true;
while (flag[1]) ; while (flag[0]) ;
<CS> <CS>
flag [0] = false; flag [1] = false;
<RS> <RS>
} while (1); } while (1);
 Problem ? , Does it meet all conditions ?
 Does not satisfy the progress condition, if both set flag at same time
Algorithm 3: Peterson’s Solution

 Two process solution (Software based solution)

 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
Peterson’s Solution

 The Peterson’s solution for process Pi.


IfIfother
otherprocess
process(P(Pj)j)wishes
wishes
do
do{{ totoenter
enterthe
thecritical
criticalsection,
section,
flag[i] ititcan
cando
doso.
flag[i]==TRUE;
TRUE; so.
turn
turn==j;j;
while
while((flag[j]
flag[j]&&
&&turn
turn==
==j);
j);  If both processes
CRITICAL try to enter at the
CRITICALSECTION
SECTION
same time …
flag[i]
flag[i]==FALSE;
FALSE;  turn will be set
to both i and j at
REMAINDER
REMAINDERSECTION
SECTION roughly the same
}}while time.
while(TRUE);
(TRUE);
 The eventual
value of turn
Algorithm 3: Peterson’s Solution

Process P0 Process P1
do { do {
flag[0] = true; flag[1] = true;
turn = 1; turn = 0;
while (flag[1] && turn == 1); while (flag[0] && turn ==0);

<CS> <CS>
flag [0] = false; flag [1] = false;
<RS> <RS>
} while (1); } while (1);
Algorithm 3: Peterson’s Solution

 Meets all three requirements:

1. Mutual exclusion is preserved.


 Pi enters its critical section only if either flag[j]==false or
turn==i.

 If both processes want to enter their critical sections at the same time,
then flag[i] == flag[j] == true.

 However, the value of turn can be either 0 or 1 but cannot be both.

 Hence, one of the processes must have successfully executed the


while statement (to enter its critical section), and the other process has to
wait, till the process leaves its critical section.  mutual exclusion is
Algorithm 3: Peterson’s Solution

2. The progress requirement is satisfied.


 Case 1:
 Pi is ready to enter its critical section.
 If Pj is not ready to enter the critical section (it is in the remainder
section).
 Then flag[j] == false, and Pi can enter its critical section.
 Case 2:
 Pi and Pj are both ready to enter its critical section.
 flag[i] == flag[j] == true.
 Either turn == i or turn == j.
 If turn == i, then Pi will enter the critical section.
 If turn == j, then Pj will enter the critical section.
Algorithm 3: Peterson’s Solution

3. The bounded-waiting requirement is met.


 Once Pj exits its critical section, it will reset flag[j]
to false, allowing Pi to enter its critical section.
 Even if Pj immediately resets flag[j] to true, it
must also set turn to i.

 Then, Pi will enter the critical section after at most


one entry by Pj.
16
Reference

 To cover this topics , different reference material has been used


for consultation.
 Operating systems concept by Abraham silberchatz edition 9
 Tutorialspoint.com
 Google.com

Lahore Garrison University

You might also like