in Operating System edited by
26,558 views
58 votes
58 votes

Consider Peterson's algorithm for mutual exclusion between two concurrent processes i and j. The program executed by process is shown below.

repeat
    flag[i] = true;
    turn = j;
    while (P) do no-op;
    Enter critical section, perform actions, then
    exit critical section
    Flag[i] = false;
    Perform other non-critical section actions.
Until false;

For the program to guarantee mutual exclusion, the predicate P in the while loop should be

  1. flag[j] = true and turn = i
  2. flag[j] = true and turn = j
  3. flag[i] = true and turn = j
  4. flag[i] = true and turn = i
in Operating System edited by
26.6k views

4 Comments

@Arjun if this question comes in MSQ then answer would be b,c ?
as we don’t have to consider deadlocks here.

0
0

@JAINchiNMay in option c there will be no mutual exclusion as the current process will not check status of another process

1
1

@cool_dude_69 suppose flag[i] is line 1… and there are 2 processes...then when P1 executes line 1-3 it goes into cs and then prempt it...now when P2 comes it also executes 1-3 but this time the while loop will return true hence i think according to option c ME is also there … correct me where i am wrong ?

0
0

5 Answers

52 votes
52 votes
Best answer

 Answer is Option B as used in Peterson's solution for Two Process Critical Section Problem which guarantees

  1. Mutual Exclusion
  2. Progress
  3. Bounded Waiting

Both $i$ and $j$ are concurrent processes. So, whichever process wants to enter critical section(CS) that will execute the given code.

A process $i$ shows it's interest to enter CS by setting $flag[i]$ = TRUE and only when $i$ leaves CS it sets $flag[i]$= FALSE.

From this it's clear that when some process wants to enter CS then it must check value of $flag[ ]$ of the other process.  

$\therefore$     " $flag[j]$ == TRUE " must be one condition that must be checked by process $i$.

Here, the $turn$ variable specifies whose turn is next i.e. which process can enter the CS next time. "$turn$ " acts like an unbiased scheduler, it ensures giving fair chance to the processes for execution. When a process sets $flag[ ]$ value, then $turn$ value is set equal to other process so that same process is not executed again (strict alteration when both processes are ready). i.e., usage of turn variable here ensures "Bounded Waiting" property.

Before entering CS every process needs to check whether other process has shown interest first and which process is scheduled by the $turn$ variable. If other process is not ready, $flag[other]$ will be false and the current process can enter the CS irrespective of the value of $turn.$ Thus, the usage of $flag$ variable ensures "Progress" property.

If $flag[other]$ = TRUE and $turn$ = other, then the process has to wait until one of the conditions becomes false. (because it is the turn of other process to enter CS). This ensures Mutual Exclusion.

Thus, ans is $(b)$.

** one interesting point that can be observed is, if 2 processes wants to enter the CS, the process which executes " $turn = j$" statement first is always the first one to enter the CS (after the other process executes $turn = j$".

selected by

1 comment

Thus, correct code will be:-

repeat
    flag[i] = true;
    turn = j;
    while (flag[j] == true and turn == j) 
       do no-op;
    Enter critical section, perform actions, then
    exit critical section
    Flag[i] = false;
    Perform other non-critical section actions.
Until false;

 

1
1
36 votes
36 votes

Answer is (B). suppose two processes $p1$ and $p2$. To gurantee mutual exclusion only $1$ process must be in $CS$ at a time. now, shared variable is turn $P1 P2 f[1]=$ true $f[2]=$true turn$=2$ $p1$ will now check for condition while($f[2]=$ true and turn $=2$) then it will go for busy wait. Then, $p2$ will execute and it will update turn $=1$ $p2$ will now check condition while($f[1]=$ true and turn $=1$) then it will go for busy wait, but here as the value of turn is updated by $p2$; $p1$ will be able to enter into CS as the condition will become false therefore, $p1$ will enter CS and $p2$ will be in busy wai until the $p1$ will come out and make $f[1]=$ false hence, no two process can enter into CS at a time so predicate $p$ is option (B).

edited by

4 Comments

As it is mentioned that below code is running by a processor,

flag[i] --- Pi is running,

then we should check others interest p[j] == true.

and they gave

turn = j;

dont consider this as process name for time being.

as per the petersons concept,

when a & b are need CS,

then

we keep

I[a] = true.

turn =a;

we check condition by

(I[b]== true and turn == a);

 

now similarly they mentioned turn = j;

then we have to check (flag[j] and turn =j;)

so option is B
0
0
edited by

@Arjun Please have a look at this.

I have a doubt regarding Option C. The question says that mutual exclusion should be guaranteed and nothing is said about deadlock. So even if putting flag[i] = true and turn = j in predicate P makes the algorithm suffer from deadlock because the second process didn't execute twice , doesn't it guarantee mutual exclusion? Because mutual exclusion says that if one process is in CS, another process cannot enter CS. So I don't think that deadlock implies no mutual exclusion.

1
1
edited by

@avistein

Peterson's algorithm must satisfy Mutual exclusion, Progress and Bounded waiting properties.

flag[i] = true and turn = j

The above option does not satisfy Progress and also does not satisfy Bounded waiting(a process can get stuck in the while loop for infinite time).

0
0
18 votes
18 votes

If process Pi wants to enter into CS then it will make Flag[i]=True and turn=j. Now for while loop, consider 2 points:

(i)  If we take turn=i, then it already makes condition False in while loop and thus both processes can enter simultaneously into  CS. So, take turn=j

(ii)  if we take flag[i]=TRUE, then the condition will become like process ' i ' wants to enter which made flag[i]=TRUE and        turn=j already, so it will always remain in while loop forever. So, take Flag[j]=TRUE

Thus (B) is the answer for sure :)

by

1 comment

you cleared my doubt thanks brother
0
0
0 votes
0 votes

Basically, Peterson’s algorithm provides guaranteed mutual exclusion by using the two following constructs – flag[] and turnflag[] controls that the willingness of a process to be entered in critical section. While turn controls the process that is allowed to be entered in critical section. So by replacing P with the following,

flag [j] = true and turn = j

process i will not enter critical section if process j wants to enter critical section and it is process j’s turn to enter critical section. The same concept can be extended for more than two processes.

2 Comments

@iwasifirshad A small point i would like to make clear , in normal peterson Algo what we have seen is , the process trying to enter CS will check if interested[other]==True and turn==current process(P1) , if both the conditions are met ,P1 will be busy in waiting in the while loop ( P1 will understand , there is a chance that P2 might be in CS or iterating in while loop)---

Case 1: If P2 is iterating in while loop ( interested[p1]==T && turn ==P2 is true)  so as soon as P1 sets the turn=P1 , P2 will break the while loop and enter the CS

(PS- the first process to set the turn variable will get first chance to access CS )

Case 2: If P2 is in CS, P1 will iterate in the while loop untill P2 sets interested[P2]=false i.e it comes out of the CS 

P1

:repeat
L1:    intersted[1] = true;
L2:    turn = 2; // turn= other process
L3:    while (intersted[2]==True && turn==2) do no-op;
L4:    Enter critical section, perform actions, then
L5:    exit critical section
L6:    intersted[1] = false;
L7:    Perform other non-critical section actions.
L8:Until false;

P2

:repeat
L1':    intersted[2] = true;
L2':    turn = 1; // other process
L3':    while (intersted[1]​​​​​​​​​​​​​​==True && turn==1) do no-op;
L4':    Enter critical section, perform actions, then
L5':    exit critical section
L6':    intersted[2]​​​​​​​ = false;
L7':    Perform other non-critical section actions.
L8':Until false;

 

BUT here in this question , turn variable is not used in the same way ,here turn=other process 

so now how to identify the correct option , option C and D are eliminated as we always check interested [other process] == true and now setting turn=current process will violate Mutual Exclusion principle , consider Line 1,2,3 in the given code,initially interested[0] &  interested[1]= FALSE

P1:1,2,3,CS | P2:1,2,3,CS ( violates Mutual Exclusion )

so , turn =other process ,option B is correct.

0
0

@iwasifirshad A small point i would like to make clear , in normal peterson Algo what we have seen is , the process trying to enter CS will check if interested[other]==True and turn==current process(P1) , if both the conditions are met ,P1 will be busy in waiting in the while loop ( P1 will understand , there is a chance that P2 might be in CS or iterating in while loop)---

Case 1: If P2 is iterating in while loop ( interested[p1]==T && turn ==P2 is true)  so as soon as P1 sets the turn=P1 , P2 will break the while loop and enter the CS

(PS- the first process to set the turn variable will get first chance to access CS )

Case 2: If P2 is in CS, P1 will iterate in the while loop untill P2 sets interested[P2]=false i.e it comes out of the CS 

P1

:repeat
L1:    intersted[1] = true;
L2:    turn = 2; // turn= other process
L3:    while (intersted[2]==True && turn==2) do no-op;
L4:    Enter critical section, perform actions, then
L5:    exit critical section
L6:    intersted[1] = false;
L7:    Perform other non-critical section actions.
L8:Until false;

P2

:repeat
L1':    intersted[2] = true;
L2':    turn = 1; // other process
L3':    while (intersted[1]​​​​​​​==True && turn==1) do no-op;
L4':    Enter critical section, perform actions, then
L5':    exit critical section
L6':    intersted[2]​​​​​​​ = false;
L7':    Perform other non-critical section actions.
L8':Until false;

 

BUT here in this question , turn variable is not used in the same way ,here turn=other process 

so now how to identify the correct option , option C and D are eliminated as we always check interested [other process] == true and now setting turn=current process will violate Mutual Exclusion principle , consider Line 1,2,3 in the given code,initially interested[0] &  interested[1]= FALSE

P1:1,2,3,CS | P2:1,2,3,CS ( violates Mutual Exclusion )

so , turn =other process ,option B is correct.

0
0
Answer:

Related questions