Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Understanding a rudimentary solution to Critical Section Problem

  1. Aug 27, 2013 #1


    User Avatar

    (Source: page 20 of this :http://www.ics.uci.edu/~bic/courses/JaverOS/ch2.pdf)

    The part I put in bold looks like nonsense.
    Since program1 executes after the Critical Section and after turn gets set to 2, there is nothing stopping p2 from leaving its wait loop and entering its critical section, no matter what happens in program1. Looks like a perfectly fine solution to me.

    Am I right? Am I not seeing something?

    (NOTE: p1 is the thread and program1 is the non-critical part of p1.)
    Last edited: Aug 27, 2013
  2. jcsd
  3. Sep 2, 2013 #2


    User Avatar
    Gold Member

    It looks OK for the most part. Each thread flips the flag immediately after exiting its critical section, thus allowing the other thread to enter its own critical section. As you noted, if either thread failed to flip the flag (i.e., died during its critical section phase) that would deadlock the other thread. So error-handling code in the critical section should ensure that, should the thread crash, the flag is flipped anyway.
  4. Sep 2, 2013 #3


    User Avatar
    Gold Member

    And I guess it goes without saying that the code within the critical section should be kept as short as possible.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook