The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+30 votes
3.7k views

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
asked in Operating System by Veteran (59.8k points)
edited by | 3.7k views
0

I think if we will change 

turn = j; --> turn = i; 

Then also it will work. Please correct me if i am wrong.

+2
yes it will work but condition P statement will change to flag[j]==true and turn==i
0
option B
0

@Shaik Masthan 

Suppose

1. there are two processes P1 and P2

2. flag[1] , flag[2], turn are the variables

when P1 will execute first two lines and enter CS status of variables will be flag[1]= T and turn = 2 and it got preempted.

when P2 execute first two lines, flag[2]= T and turn will be set to 1.

Now to stop P2 at entry point I agree option 2 is clearly the choice but according to above values I am getting turn == i to stop P2. Where I am going wrong?

 

 

0
you are confused, to clearly understood it

First write P1 code separately and P2 code separately ( i mean don't use i and j instead of them fix the values ), then analyse the code, automatically you will understood !
0
turn is a global variable right?

Can you please point out the mistake in above interleaving of processes. It will really be helpful. I am stuck here.
0

Can you please proceed after this @Shaik Masthan.

0

Yes to block P2 flag[j]= T and turn ==j . Sorry @Shaik Masthan missed it.

+1

as per option B :-  flag[j] = true and turn = j

for P$_1$ it would be like :- flag[2] = true and turn = 2

for P$_2$ it would be like :- flag[1] = true and turn = 1.

 

Coming continuation of your procedure,

Note that current context of your code is flag[1] = true , flag[2]=true and turn = 1

Now P$_1$ comes, it have to executes the line while ( flag[2] == true and turn == 2 ), it gives false ==> enter into CS

0
A) flag[i] = true;
    turn = j;
    while (flag[j] == true && turn == j) do no-op;
B)  flag[i] = true;
    turn = i;
    while (flag[j] == true && turn == i) do no-op;
C)  flag[i] = true;
    turn = j;
    while (flag[j] == true && turn == i) do no-op;

@Shaik Masthan

@tusharp

A) and B) will be peterson solution

C) Violates ME so it is not peterson solution  ....all are for Process i

correct if wrong

6 Answers

+9 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$".

answered by (391 points)
selected by
+29 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).

answered by Loyal (8.1k points)
edited by
0
C is also correct ans...........
0

@ jayendra what about option c can you explain....

0
@bikram sir Are B and C both the correct answer?
+5
+2
Here the question should mention that this is the code executed by the process Pi.Otherwise it creates  confusion.

It is a tricky question.We should eliminate options if possible:-

c:) creates deadlock

a& d:)will fail mutual inclusion as the process has set turn =j. Now it will check turn =i in for loop .so,both can enter.So remaining is b.which is surely the answer

Let us say both the processes tries to enter now both set flag to 1 but the key is turn variable.it will have i or j ,so one of the process will fail and other will pass and enter.
0
Hi Bikram Sir,

 

Please correct me if I am wrong here for not taking option C.

 

If Pi enters first and doesn't wish to enter second time, then Pj will never be able to enter in CS as for PJ the condition is

flag[j] = true and turn = i both are true, so it will loop forever and Pj will never be able to enter CS.
0
can anybody explain what is the issue with option A?
0
C is wrong option..suppose p0 enters into the cs and in between p1 struck into the while loop..and if p0 after completing its task its not interested to execute once again then at that situation p1 is not able to came out of the while loop..so it strucks forever (untill and unless p0 want to exaecute one more time...but there might be the casr that p0 does not want to execute in its lifetime) which cause a deadlock here...
0
After exit of critical section by pi it will set the flag to false, so that pj can enter
+1

@Shubhgupta

Pi

L1:repeat
L2:    flag[i] = true;
L3:    turn = j;
L4:    while (flag[j]==True && turn==i) do no-op;
L5:    Enter critical section, perform actions, then
L6:    exit critical section
L7:    Flag[i] = false;
L8:    Perform other non-critical section actions.
L9:Until false;

Pj

L1':repeat
L2':    flag[j] = true;
L3':    turn = i;
L4':    while (flag[i]==True && turn==j) do no-op;
L5':    Enter critical section, perform actions, then
L6':    exit critical section
L7':    Flag[i] = false;
L8':    Perform other non-critical section actions.
L9':Until false;

Now see this sequence :

L2 -> L2' -> L3 -> L4 -> L3' ->L4'

L2: flag[i]=true

L2': flag[j]=true

L3: turn=j

L4: flag[j]==true --> TRUE and turn==i -->FALSE. Pi enters in CS

L3': turn =i

L4': flag[i]==true --> TRUE and turn==j -->FALSE. Pj enters in CS

No mutual exclusion! 

0
Thanks @MiNiPanda, Don't know what i was thinking at that time.
0

@MiNi Panda please make me clear what happening when we are putting options in code  

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; 

if we are putting option A at the place of P THEN 

while (flag[j] = true and turn = i) do no-op; 



   i=true
turn=j

 now this is 2 process solution to here flag j will True so i=true j=true and turn=j in option A, flag j=true and turn=i but here turn =j so loop will break and process will enter in to the critical section. it means the process j is in C.S. now if how process i will enter in the critical section and violate the M.E.

0
in pj flag[j]=false i think
0
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
+13 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 :)

answered by Active (1.8k points)
+3 votes

Option C is correct(Best option)

First of all its not a complete implementation of Peterson Algorithm. Here we need to check Mutual Exclusion. Now for that Process i is in CS and Process j is trying to enter into CS. How I know it ? It can be seen in code " turn = j " that means Process j is trying to enter CS. Therefore to make sure Mutual Exclusion is there Process j should keep on iterating in the while( ) loop while Process i is in CS . Now since Process i is in CS we need to make sure that when Process i exits CS then only Process j enters. Therefore for that to happen Process i must execute " Flag[i] = false". From this we can take condition that  "Flag[i] = true " should be put in while loop. Secondly since "turn = j" therefore " turn == j " should also be added in while loop. Since turn = j is given therefore i would best choose option c rather d.

answered by Junior (639 points)
edited by
+10
Here first flag[i] and turn vaiables are set and then compared with same value:

flag[i] = true; 
turn = j; 
while(flag[i] == true and turn ==j); // it will always give infinite loop
0
but when the second process executes line no.2 won't the condition in while of the first process become(true&&false) and it would enter the critical section?
0 votes

Ans should be (c) as it should be FLAG[other] which is i and turn of current is checked i.e j

answered by (451 points)
0
'i' is the process which is executing the code!
–2 votes
ans is C is correct because in case of option B, the process will enter simultaneously enter into the critical section but incase of C mutual exclusion will be ensured.
answered by (205 points)
Answer:

Related questions



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

46,636 questions
51,132 answers
176,185 comments
66,551 users