let process $p1$ enters it executed $3$ line i.e turn=j means it made turn $=0$ (as $p1$ entered so, $i=1$ $j=0$ ) now assume $p1$ got preempted
now process $p0$ enterted it executed $3$ line made turn $=1$ (as $p0$ entered so, $i=0 \ j=1$)
after that $p0$ executed $4$ line made want$[0]=1$
next $p0$ executed $5$ line while($0$ &&true ) which is false so, it entered critical section
now $p1$ resumed and executed $4$ line made want$[1]=1$
next $p1$ executed $5$ line while($1$ &&false) which is false so, it entered critical section
now, both process $p0$ & $p1$ are in CS at same time so, above solution is wrong
the correct solution is as follows:
just interchange line $3$ & line $4$, than solution becomes correct
shared char want[2] = {0,0};
shared int turn = 0;
1. P_i()
2. { while (1) {
3. want[i] =1;
4. turn = j:
5. while (want[j] && turn!=i);
6. critical_section();
7. want[i] = 0;
8. remainder_section();
9. }
10. }