Synchronization for Two Processes
The above figure represents the code snippet for the Execution of process P0 into the Critical Section
- Initially, the process P0 arrives.
- The block executes the first instruction. i.e. while (turn != 0).
- Initially, the value to turn variable is set to 0, therefore it returns 0 to the while loop.
- As the value of the turn variable is 0, the while loop condition is not satisfied, and process P0 enters into the Critical section.
- Once process P0 has entered into the critical section, process P1 cannot enter into the critical section block unless it completes its execution and set the turn value as 1.
The above figure represents the code snippet for the Execution of process P1 into the Critical Section
- Initially, process P1 arrives.
- The block executes the first instruction. i.e. while (turn != 1).
- Initially, the value to turn variable is set to 1, therefore it returns 1 to the while loop.
- As the value of the turn variable is 1, the while loop condition is not satisfied, and process P1 enters into the Critical section.
- Once process P1 has entered into the critical section, process P0 cannot enter into the critical section block unless it completes its execution and set the turn value as 0.
The execution of process P1 into an infinite loop
- When initially the turn value is set to 0 and process P1 arrives.
- It executes the first while loop condition that is while (turn != 1)
- Initially, as the turn value is set to 0, it returns 1 to the while loop
- The value 1 does not break the condition present in the while loop
- Therefore, process P1 gets trapped inside the infinite while loop
- The process P1 remains in an infinite loop until the turn value becomes 1.
Turn Variable in Operating System
The turn variable is defined as a synchronization mechanism that is implemented in the user mode. The turn variable is also known as the Strict Alternation Approach. It is a synchronization mechanism that is implemented for synchronizing two processes. For synchronization, it makes use of a variable known as the turn variable. This approach is used only when working with two processes.
Contact Us