Open In App

Turn Variable in Operating System

Last Updated : 06 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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.

Note:

  • The Lock variable is defined as a synchronization mechanism for the processes.
  • The lock variable mechanism is implemented in the user mode and is used to synchronize more than two processes.

Example: Consider we have two processes process P1 and process P2. For the below two processes P0 and P1,

  • Turn value = 0: When the turn value is set to 0, it means that process P0 will enter into the critical section.
  • Turn value = 1: When the turn value is set to 1, it means that process P1 will enter into the critical section.

Synchronization for Two Processes

Two Process Synchronization - Process P0

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

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.

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?

Answer:

In an operating system, a variable is a symbolic name or unique identifier that represents a value stored in memory. It is used to store and manipulate data within the operating system environment. Variables can be assigned values and their values can change during the execution of a program or a system operation.

Q.2: Can variables be shared between other processes in an operating system?

Answer:

No, variables cannot be directly shared between other processes in most operating systems. Interprocess communication mechanisms like shared memory, pipes, sockets, and message queues are used to ease data sharing between processes.

Q.3: Can variables be turned into an operating system?

Answer:

No, variables cannot be “turned” in an operating system. The concept of “turning” a variable is not applicable in the context of OS. Variables are used to save and manipulate data, but they are not dynamic entities that can be turned or changed in a fundamental way.

Q.4: Can the turn variable approach handle more than two processes?

Answer:

No, the turn variable approach is designed for synchronizing two processes only. It does not have scope beyond the two processes for synchronization.

Q.5: What are the limitations/challenges of the turn variable approach?

Answer:

The turn variable approach is limited to synchronizing two processes only. It does not up well to scenarios involving more than 2 processes. Additionally, it can arise to potential problems like busy waiting or starvation if not implemented carefully. It is also not feasible for scenarios that need large advanced synchronization techniques such as deadlock avoidance or priority-based scheduling.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads