Peterson's Algorithm with some modifications -


would peterson's algorithm work after flipping turn , flag orders; ex:

p0:   turn = 1;   flag[0] = true;          while (flag[1] && turn == 1)          {              // busy wait          }          // critical section          ...          // end of critical section          flag[0] = false; 

=================

p1:   turn = 0;   flag[1] = true;          while (flag[0] && turn == 0)          {              // busy wait          }          // critical section          ...          // end of critical section          flag[1] = false; 

no, doesn't work if flip orders, because allows both processes enter critical section simultaneously.

for example, suppose initial state flag[0] = false, flag[1] = false, turn = 0:

process 0 runs:

turn = 1;  // flag[0] = false, flag[1] = false, turn = 1 

then context switch process 1:

turn = 0; flag[1] = true;   // flag[0] = false, flag[1] = true, turn = 0  while (flag[0] && turn == 0) {} // evaluates false because     // process 0 interrupted before set flag[0] true  // process 1 enters critical section... 

then context switch process 0:

flag[0] = true;   // flag[0] = true, flag[1] = true, turn = 0 while (flag[1] && turn == 1) {} // evaluates false  // process 0 enters critical section.. 

now both processes inside critical section.

setting flag has come first because it's thing other process can't overwrite. if process sets turn shown above other process can overwrite , enter critical section before first process has chance set flag.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -