4

I added comments to each line to the best of my understanding, but I still don't get why we set waiting[j] = false; at the end without running process j's critical section. In my opinion, waiting[j] = false; should be replaced with i = j; so when it loops again, we run process j's critical section. Or else we'll always be running process i's critical section!

enter image description here

NoName
  • 9,824
  • 5
  • 32
  • 52

2 Answers2

1

For a process to enter its critical section, waiting[i] must be false, but waiting[i] can only be set to false if a process is leaving its critical section by calling waiting[j] = false, which I take to mean that now process j can enter its critical section prompting process i to wait. I'm still learning these concepts so I'm not 100% sure. Abraham and Silberschatz 9th edition does not do a very thorough job of explaining these algorithms.

therealak12
  • 1,178
  • 2
  • 12
  • 26
omri
  • 352
  • 2
  • 18
  • I'm just going to accept your answer because right after the final exam, I forgot everything I learned in this course LOL – NoName Sep 29 '19 at 21:07
1

Validity of the Algorithm

First, it is important to note that this algorithm solves the critical section problem only when there are two processes (here they are referred to as process 0 and process 1).

Next, as per the convention in Operating System Concepts by Abraham and Silberschatz . Peter B Galvin . Gerge Gagne, i refers to one of the processes amongst 0 and 1 and j refers to the other process.

Mapping of right code to right process

Having said that, it must be noted that this code is for the process i. The code for process j would be obtained by interchanging i and j in the given code. (In my opinion this is what caused confusion to you, since you said the following)

should be replaced with i = j; so when it loops again, we run process j's critical section. Or else we'll always be running process i's critical section!

Finally, consequences of waiting[j] = false (which happens in Process i)

Now, both these codes would execute as two different processes in a system. So as soon as you set waiting[j] = false in the last line of Process i following events occur:

  • The condition in while(waiting[j] && key == 1) for the code of Process j (Note that the code for Process j is obtained by replacing i with j as explained in the previous heading of Mapping of right code to right process) turns out to be false and therefore Process j resumes it's execution by entering the critical section.
  • Meanwhile, Process i loops back into the outermost while(true) loop, setting waiting[i] to true, key to 1 and waiting by looping over in the while(waiting[i] && key == 1) loop.
sksingh
  • 27
  • 1
  • 8