Given the following solution for the critical section problem (the given code is
ID: 3847947 • Letter: G
Question
Given the following solution for the critical section problem (the given code is for process Pi;code for Pj is the same with i and j interchanged), show one scenario where it does not satisfy the progress requirement. Here, flag [i] and flag [j] are shared variables initialized to false. turn is also a shared variable initialized to i. do {/* Begin Entry Section */flag [i] = true; while (flag [j] || (turn = = j));/* End Entry section */Critical section/* Begin Exit section */flag [i] = false;turn = j;/*End exit section */Remainder section }Explanation / Answer
Line 1:
flag[i]=true;
Line 2:
while (flag[j] || (turn==j));
The above lines are at the entry section of the code of Process i. The Process j will have the similar code.
Now Consider First Process i got the chance from CPU.. Process i went ahead and executed line1. So now
flag[i] = True
Before process[i] could have executed more lines, cpu switches to Process j. Process j also goes ahead and executes line1, where it will set the flag for process[j], So
flag[j] = True
Remember, Process j is having the CPU with it now also.. and it goes ahead and executes line2. The line2 for Process[j] will be similar to below:
while (flag[i] || (turn==i));
We haven't touched variable turn till now, So assuming it has a value of i. Now Process 2 executes above line, and find that flag[i] = true, and hence keeps on waiting inside the while loop.
Meanwhile, CPU switches to Process i. Now Process i goes to execute its second line, which is :
while (flag[j] || (turn==j));
and it finds that althrough turn = i, but still flag[j] = True, which means it also waits inside the while loop.
You can see that although none of the process i or j has reached to the critical section yet and still they are waiting for other to complete. Hence, the above given solution is wrong and doesn't satisfies the progress requirement.
The correct solution will be have line 2 as:
while (flag[j] && (turn==j));
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.