• Operating System Video Tutorials

Operating System - Turn Variable in Process Synchronization



Introduction to Turn Variable in Operating System

The "Turn Variable" method id used to solve the critical section problem in operating systems. A turn variable is a synchronization primitive used to manage process synchronization in user mode. It is designed for synchronizing two processes and often works alongside other synchronization mechanisms like semaphores or condition variables to implement a scheduling policy. The turn variables to implement a scheduling policy. The turn variable acts as a queue, specifying the order in which processes gain access to a shared resource.

Threads or process are arranged in a queue based on a predefined order. The turn variable determines which process from the queue gains access. The system guarantees that each process gets a turn to access the resource, effectively preventing starvation and ensuring fairness.

Implementation of Turn Variables in Complex Systems

Turn variables are used to ensure fair scheduling among multiple threads or processes. They specify the order in which processes will access shared resources, promoting fairness. Turn variables use a queue for the waiting list mechanism. Processes are placed in the queue, and the turn variable determines which process gets access.

Turn variables are more complex to implement as a function. They are often used in conjunction with primitive semaphores and condition variables to specify fair scheduling. These variables are typically used to implement a fair allocation of resources.

Importance of Turn Variables in Synchronization

The need for a turn variable arises due to issues associated with lock variables. If a lock variable is tied to a specific process, it can enter the critical section with the lock variable value of 1 simultaneously. Under certain conditions, the lock variable may not guarantee mutual exclusion. The lock variable mechanism is implemented in user mode and can be used to synchronize more than two processes.

The turn variable is a unique approach for software mechanisms implemented in user mode. It employs busy waiting and can be used only for two processes. A turn variable functions as a lock, allowing processes to coordinate access. The problem with the lock variable approach arise when more than one process can enter the critical section simultaneously if the lock variable is set to 1. This situation fails to ensure mutual exclusion.

How Turn Variable Works?

Turn variables are mostly used in conjunction with semaphores or with other variables that implement fair resource allocation. This synchronization mechanism ensures coordination between two processes by using a turn variable.

When process P1 arrives, it executes the instruction turn!=1. This sets the turn value to 0, so it does not break the while loop condition. If process P1 is specified inside an infinite while loop, the while loop keeps process P1 busy until the turn value becomes 1, at which point the condition breaks.

Features of Turn Variable

Following are the features of the turn variable −

  • Deadlock: Every turn variable is free from deadlock. Only one turn variable can be processed in the critical section at a time. Once the turn variable is updated, the next process can enter the critical section.

  • Progress: Turn variable doesn't guarantee progress. It strictly follows the alternate approach.

  • Bounded waiting: Each process gets the chance once the previous process has been executed. Therefore, the turn variable ensures bounded waiting.

  • Mutual Exclusion: Mutual exclusion prevents more than one process from accessing he same shared resources at the same time. The turn variable ensures the mutual exclusion property.

The concept of the Turn Variable

Following are the concepts of the turn variable in detail −

Initialization:

The Turn Variable can be initialized with either value 0 or 1. For example −

  • If turn = 0, process P0 will run first.

  • If turn = 1, process P1 will run first.

Entry Code for Process P0:

Before entering the critical section, P0 executes the entry code. It checks the condition while(turn!= 0). If the value of turn is not 0, P0 will wait. Once the condition is false, P0 proceeds to the critical section.

Entry code for process P1:

Similarly, P1 follows a similar entry process: while(turn!=1). If the value of turn id not 1, P1 will be blocked from entering the critical section.

Mutual Exclusion Guarantee:

If process P0 is in the critical section, P1 cannot enter, and vice versa. This guarantees that mutual exclusion is always maintained.

Key Characteristics of Turn Variable

Following are the characteristics of turn variable −

  • Works for Two Processes Only: The Turn Variable is designed to manage synchronization between only two processes, P0 and P1. More than two processes are not supported.

  • User Mode Operation: This method operates in the user mode, meaning no support from the kernel(operating system) is required. The code runs directly in the application layer.

Bounded Waiting Condition

Bounded waiting ensures that a process does not get an indefinite number of chances to enter the critical section while the other process waits forever. In the case of strict alternation, each process gets its turn to enter the critical section, ensuring bounded waiting. If P0 enters the critical section and exits, it will change the turn value to 1, allowing P1 to enter. After P1 exits, it changes turn to 0, allowing P0 to enter again. This process alternates between the two.

Platform independence in the turn variable method is independent of hardware and software platforms, It can be implemented on any system without depending on specific platform features.

Conclusion

The Turn Variable method is an efficient way to manage mutual exclusion for two processes. While it guarantees mutual exclusion and bounded waiting, it does not fully satisfy the progress condition, as one process can block the other from entering the critical section immediately. It is also independent of hardware and software, making it versatile across different platforms.

Advertisements