Synchronization for Two Processes

Two Process Synchronization – Process P0

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.

Two Process Synchronization – Process P1

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.

Similar Reads

What is the Need for Turn Variable?

The need for a turn variable arises due to the problem of the lock variable. In the lock variable approach, the process is able to enter into the critical section only when the lock variable is set to 1. In the lock variable approach, more than one process has a lock variable value of 1 at the same time. Due to such conditions, the lock variable is not able to guarantee mutual execution. Therefore, there comes a need for a turn variable....

Synchronization for Two Processes

Two Process Synchronization – Process P0...

Features of Turn Variable

Mutual exclusion: Mutual exclusion does not allow more than one process to access the same shared resource at the same time. Turn variable ensures mutual exclusion property. Progress: The turn variable does not guarantee progress. It follows the alteration approach strictly. Portability: The turn variable is implemented in user mode and does not require any kind of special instruction from the operating system. Therefore it provides portability. Bounded waiting: Each process gets the chance, once a previous process is executed the next process gets the chance therefore turn variable ensures bounded waiting. Deadlock: The turn variable is free from deadlock. Only one process is in a critical section at a time. Once the turn value is updated the next process goes into the critical section....

Conclusion

The turn variable is used to overcome the limitations of the lock variable. It is implemented in the user mode and for synchronization between two processes. Turn variable ensures mutual execution, portability, and bounded waiting and it is free from deadlock....

Frequently Asked Questions

Q.1: What is a variable in an operating system?...