Using Flag variable for solution of Critical Section


In the last 2 posts we have seen how semaphores and Turn variables can be used to solve the problem of critical section. Semaphore hold true for all 2 criterias of critical section while Turn variable proved true for 1 criteria only. Today, we are going to discuss how flag variables can solve the critical section problem. So, let's get started, shall we?

Using Flag variable for solution of Critical Section


Using Flags to solve the problem

Recall when we were discussing turn variable , we never asked the process if it wants to enter critical section or not. Due to which it did not showed us Progress.
This method is a rectification or updation of turn variable method.
1. We are using an array with 1 slot for process P0 and one slot for P1 both having values F initially.
2. So whenever a process wants to enter critical section then it will have to turn its slot to T. So we will use this mechanism to know whether a process wants to enter critical section or not.
3. Suppose P0 wants to enter then we will execute its code and flag[0] is turned T. Now the while loop is there to check whether P1 also wants to enter critical section or not. Since value of flag[1]=F so while loop condition is false and P0 will enter critical section.
4. Now lets check if the above code holds true for Mutual Exclusion or not. Suppose when P0 entered the critical section at that time we context switched to P1 and flag[1]=T. The while loop is executed which says flag[0] should be T and flag[0] is actually T thus P1 will be trapped in the while loop. 
5. Suppose context switch occurs and now we are in P0 again. Now P0 comes out of critical section and flag[0] is set F. Suppose we perform another context switch and now we are in P1. Now P1 can can enter critical section because flag[0] is F and while loop breaks.
6. Now, let's check if P0 can enter again into critical section or not. flag[0] is set T. Now, while loop will be executed which says if  flag[1] is T and flag[1] is actually set to T so P0 will be trapped in the while loop. Therefore, Mutual Exclusion is satisfied here.
7. Now let's check for Progress. Suppose the flag array is set to F for both slots again.
8. Now P1 is not interested in entering critical section and P0 wants to enter critical section so flag[0] is set T. Now, while loop is checked which says flag[1] should be T but it is F because P1 is not interested in entering critical section. So, P0 will enter critical section.
9. Now, let say P0 comes out of Critical section and turns flag[0] to F. now, again P0 wants to enter critical section and it turns flag[0] to T. Again it checks while loop whether flag[1] is T or not but P1 is not interested in entering so P0 will enter critical section again.
10. So, you can see that only those processes are participating which really wants to enter critical section. So, it should hold true for Progress but it will not.
11. Suppose the flag array is set to F again. Now , P0 wants to enter critical section so flag[0] is set T. Here, before executing while loop for P0 we context switched to P1. P1 also wants to enter critical section so flag[1] is also set T. But now, while loop of P1 which says flag[0] should be T is actually T so P0 will be trapped in while loop. Same goes for while loop for P0 because while of P0 says that flag[1] should be T which is actually T so P0 will also be trapped in while loop. Thus the system goes in deadlock.
12. So, by flag variable also progress is not satisfied.   



Post a Comment (0)
Previous Post Next Post