Using Turn Variables for solution of Critical Section Problem


Today, we will be using Turn Variables for solution of Critical Section Problem. We will see if turn variables can solve the problem of critical section just like we proved for semaphores. We will see if by using turn variables we can meet the 3 criterias (Mutual Exclusion, Progress, Bounded Waiting) of critical section problem or not. So, let's start, shall we?

We will consider 2 processes P0 and P1and check if they solve the problem and if they don't then how can we rectify them in order to solve the problem.

Using Turn Variables for solution of Critical Section Problem

One should know that there is an entry section which ensures that only one process enters critical section at a time , a exit section which ensures that a new process shall enter critical section when a process leaves critical section and remainder section which is nothing but accessing private resources of a process.
1. I have used a boolean variable turn that can take value either 0 or 1.
2. Let's start execution with variable turn = 0.
3. Now, let say P0 starts first. Since value of turn is 0 and while loop indicates value of turn ! = 0 hence the condition in while loop is false and we will exit the loop and enter the critical section.
4. Now, let's see if Mutual Exclusion is ensured or not.
5. Let say when P0 entered critical section, then we context switched to P1.
6. In P1 the value of turn is 0 and while loop has condition turn ! = 1 which means condition is true and the process P1 will be trapped in this while loop.
7. Now, P1 does a context switch and charge comes to P0 again which is executing in the critical section.
8. When P0 is done with its execution in critical section then it will exit the critical section and observe that value of turn is set to 1.
9. Now, lets see if P0 can enter critical section again or not. When P0 starts again then it checks while loop which says turn !=0 and value of turn is actually 1 so condition is true and P0 will be trapped in the while loop.
10. P1 which was earlier trapped in while loop can enter critical section now because value of turn is 1 and while condition turn !=1 is false.
11. So, this code works satisfies Mutual Exclusion.
12. Now, let's see if this code holds good Progress as well or not.   
13. Observe the above steps carefully and you will see that when P0 came out of critical section and it again wanted to go into critical section then it was not able to go because P1 must go into critical section first and then only P0 can go again into the critical section.
14. So, Progress is not fulfilled because it is suffering from strict alternation between 0 ans 1.
 

Conclusion

The use of turn variable only fulfilled one criteria of critical section problem which was Mutual Exclusion. It did not worked for Progress because there was strict alternation between 0 and 1.


Post a Comment (0)
Previous Post Next Post