816 views
2 votes
2 votes

I was doing operating systems in which there was a topic of turn variable well there are two processes P0,P1 and   int turn =0

For Process P0                                                            For Process P1 

Non Critical Section                                                      Non Critical Section

while(turn!=0)                                                               while(turn!=1)

//enter critical section                                                    //enter critical section

turn=1;                                                                          turn=0;

Non Critical Section                                                        Non Critical Section

Now these are two pseudo codes for two processes so if variable turn initial value is 0 then according to you which process will get into critical section first.Is it P0 or P1??

According to me 

Like i m getting confused as if turn=0 then if it enters while loop 

while(0 !=0 ) since condition doesnot match false  it will leave for P0 and not enter critical section 

while(0 !=1 ) since condition is true so P1 will get a chance to enter critical section according to me 

According to book 

But in the book P0 was getting first so why it is so that P0 get a chance to enter first and then P1 got chance to enter 

Please somebody tell me correct ans to this problem.I am really stuck and confused 

2 Answers

Best answer
3 votes
3 votes

you are missing ";" after while.
while(turn!=0) ;                                                              
while(turn!=1);
as it is given Turn=0 at the start so, P0 gets the first chance.

Now if turn = 1  P0 will be in busy waiting and when turn becomes 0, it makes while condition false  thus P0 goes into critical section.

selected by

Related questions

3 votes
3 votes
0 answers
1
hacker16 asked Nov 9, 2017
471 views
Turn is shared variable but the values are not shared.Explain?
0 votes
0 votes
2 answers
2
Kartavya Kothari asked Aug 27, 2018
630 views
A ____ can be used to prevent a user program from never returning control to the operating system.