Unfortunately, this is an incorrect solution. Find the problem.bool flag[2]; // global flags int turn; // global turn variable Process i (i = 0 or 1) ---------------------- flag[i] = TRUE; // I am interested while (turn != i) { // while it is not my turn while (flag[j]) // while you are interested ; // do nothing: busy waiting turn = i; // because your are interested, it is my turn } // critical section flag[i] = FALSE // I am done and not interested
Process P0 Process P1 while (true) do while (true) do ...... ...... flag[0] := true; flag[1] := true; while flag[1] do while flag[0] do if turn = 1 then if turn = 0 then flag[0] := false; flag[1] := false; while turn = 1 do while turn = 0 do ; ; flag[0] := true; flag[1] := true; end if end if end while end while // Critical Section // Critical Section turn := 1; turn := 0; flag[0] := false; flag[1] := false; ...... ...... end while; end while;
You do not have to turn in your paper. What I really expect you to do is using these problems to gauge your understanding of the subject. So, do the problems after finish reading the above sections. |