How to use turn variable for two process synchronization

474.9K views
•
February 25, 2021
by
Gate Smashers
YouTube video player
How to use turn variable for two process synchronization

TL;DR

Turn variable provides mutual exclusion for two processes by allowing only one to enter the critical section at a time. It uses a 0 or 1 turn value to decide which process runs first, and after exit the turn switches to the other process. The method guarantees mutual exclusion but may not guarantee progress, and it is platform independent and runs in user mode without kernel support.

Transcript

Dear students welcome to Gate Smashers in this video I'm going to explain Turn Variable which we call Strict Alteration Method and in this video we will discuss all the points related to Turn Variable which in your competitive exams, college/ university exams, interviews anywhere if you are asked you can easily answer. So guys quickly like the vide... Read More

Key Insights

  • Turn variable uses two processes only, not scalable to more than two, and relies on a shared turn indicator.
  • Two processes P0 and P1 coordinate entry into the critical section using the turn value. If turn equals the process id, that process enters; the other spins until its turn.
  • No kernel support is required; the mechanism runs in user mode, allowing direct application level coding without OS interventions.
  • The method guarantees mutual exclusion because only the process whose turn matches its id enters the critical section at any time.
  • Progress is not guaranteed in all cases; if one process continually takes the turn, the other may starve, depending on initial conditions.
  • Bounded waiting occurs because turns alternate after each entry and exit, preventing infinite waiting for either process.
  • After a process exits, it sets the turn to the other process, enabling the other to enter when its turn comes, ensuring orderly alternation.
  • The approach is hardware and software independent, meaning it can run on any platform without relying on specific hardware features.

Explore YouTube Video Summarizer or Get YouTube Transcript Extractor

Questions & Answers

Q: How does the turn variable determine which process can enter the critical section

The turn variable holds a value of 0 or 1 corresponding to the two processes, typically P0 and P1. If turn equals the process id, that process enters the critical section; otherwise, it spins in the entry code. This simple check enforces mutual exclusion by ensuring only the process whose turn matches its identifier proceeds.

Q: Why is the turn variable method considered a two-process solution

Because it relies on a single shared turn indicator that only supports two distinct values, 0 and 1. This binds access to two processes P0 and P1, making it unsuitable for more than two processes. For any two-process interaction, it can serialize access to the critical section using the turn value.

Q: What happens when the first process enters the critical section

When the first process enters, the other process remains in its entry loop, waiting for the turn value to change. After the first process completes its critical section, it updates the turn value to the other process (0 to 1 or 1 to 0), allowing the second process to enter when its turn arrives.

Q: Is kernel support required for turn variable synchronization

No, kernel support is not required. The method runs entirely in user mode, at the application level, and relies on a shared turn variable to coordinate access. This makes it simple to implement without OS-level synchronization primitives.

Q: Does the turn variable method guarantee progress

Progress is not guaranteed in all scenarios. If the turn value is fixed and the non-selected process repeatedly tries to enter, it may experience indefinite waiting under certain initial conditions. The design inherently favors the process that eventually gets to modify the turn value.

Q: How does bounded waiting manifest in this method

Bounded waiting is achieved by the explicit alternation of turns. After a process uses the critical section and exits, it flips the turn value, giving the other process a finite opportunity to enter next. This prevents a single process from hogging access indefinitely.

Q: What ensures mutual exclusion in this approach

Mutual exclusion is ensured because at any moment only the process whose turn matches its identifier is allowed to enter the critical section, while the other process spins in its entry loop. The turn comparison enforces that no two processes can be inside the critical section simultaneously.

Q: Can this method be used on any platform

Yes, the approach is platform independent because it does not rely on specific hardware or kernel features. It only requires shared access to a turn variable and the two processes to follow the entry and exit protocol, making it portable across different operating systems and environments.

Summary & Key Takeaways

  • The turn variable method explains a two process solution that uses a single turn value to coordinate entry into the critical section. It requires two processes and an initial value that designates which process enters first. It does not need kernel support and runs entirely in user mode, making it accessible for application level implementation.

  • Mutual exclusion is achieved because the non-selected process spins while the turn value indicates which process may enter the critical section. When a process exits, it updates the turn value to allow the other process to proceed, ensuring exclusive access during its critical section.

  • Bounded waiting is achieved by alternating turns; after one process finishes, the turn value is flipped so the other process gains access. This prevents infinite waiting and ensures each process gets a turn in a predictable order, depending on initial conditions.


Read in Other Languages (beta)

Share This Summary 📚

Explore More Summaries from Gate Smashers 📚