• Operating System Video Tutorials

Operating System - Test Set Lock in Process Synchronization



Test Set Locks in Process Synchronization

Test Set Lock approach utilizes a shared variable, often referred to as a "lock," to regulate access to the critical section of code, ensuring that only one process can execute in that section at a time and preventing race conditions.

The Test Set Lock operates based on a simple concept: a lock variable that can hold two values either 0 or 1. The lock's value determines whether the critical section is available:

  • Zero(0) means the critical section is free, and a process can enter(unlock).

  • One(1) indicates the critical section is in use, preventing other processes from entering until the lock is released(lock).

This lock variable is shared by all processes that need to access the critical section. When a process wants to enter, it first checks the lock. If the lock is 0(unlock), it proceeds to change it to 1(lock) to prevent other processes from accessing section.

This ensures mutual, meaning only one process can execute in the critical section at any given time.

Operational Process of the Test and Set Lock

The process follows these steps when trying to enter the critical section −

  • Check the Lock: The process first checks the value of the lock. If it's 0, the process proceeds; if it's 1, the process waits until the lock becomes available(set to 0).

  • Set the Lock: If the lock is 0, the process sets it to 1, indicating that it has entered the critical section and preventing others from accessing it.

  • Critical Section: The process executes its task in the critical section.

  • Release the Lock: After completing its task, the process sets the lock back to 0, allowing other processes to enter.

The Test Set instruction is an atomic operation that ensures the checking and setting of the lock happen in a single, uninterrupted step. The operation works as follows −

  • The current value of the lock is saved in a temporary variable(for instance, RV).

  • The lock variable is then set to 1, indicating that the critical section is now locked.

  • The operation returns the value that was stored in RV, showing whether the lock was free(0) or already set (1).

Because the Test Set operation is atomic, no other process can interface during its execution. This ensures that once a process successfully acquires the lock, it is the only one that will enter the critical section until it releases the lock.

Step-by-Step Example

Let's illustrate an example where two processes, P1 and P2 are trying to enter the critical section:

P1's Execution:

P1 checks the lock's value, and if it is 0, sets the lock to 1 and proceeds to enter the critical section. Once P1 completes its task, it resets the lock to 0, thereby permitting other process to enter.

P2's Execution:

While P1 is in the critical section, P2 checks the lock, finds it set to 1, and waits. Once P1 completes its task and releases the lock by setting it back to 0, p2 can acquire the lock, enter the critical section, and perform its task.

Below is the Pseudo-code representation of how the Test Set Lock operates:

do {
   test_and_set(lock);
}while(lock == 1);

In this example, the function test_and_set(lock) is called repeatedly in a loop. If the lock is set to 1, the process will wait until it becomes 0. Once the lock is free, the process breaks out of the loop and enters the critical section.

Atomicity and Race Conditions

The Test and Set lock relies on atomicity to prevent race conditions. If the operation weren't atomic, multiple processes could potentially check the lock and set it at the same time, allowing more than one process to enter the critical section simultaneously. This would violate mutual exclusion. However, because the Test and Set operation is atomic, this type of conflict is avoided, ensuring that only one process can access the critical section at any moment.

The Test and set lock ensures that only one process can hold the lock at any time, but it doesn't guarantee fairness. If many processes are trying to access the critical section simultaneously, some may be delayed for extended periods. This issue is known as starvation, where a processes specifies the lock before it.

Advantages of Test Set Locks

The possible advantages of using test-and-set locks in process synchronization are as follows −

  • Mutual Exclusion: The Test and Set lock ensures that only one process can access the critical section at a time, guaranteeing mutual exclusion.

  • Simplicity: The Test and St lock is straightforward to implement and understand. Its simple design involves juts a few steps checking the lock, setting it, and releasing it.

  • Efficiency in Low-Level Systems: For low-level systems with minimal synchronization needs, the test and Set lock can be an efficient solution.

Disadvantages of Test Set Locks

The possible disadvantages of using test-and-set locks in process synchronization are as follows −

  • No Bounded Waiting: The Test and Set lock does not offer bounded waiting, meaning that some processes may be indefinitely delayed while others continuously acquire the lock.

  • starvation: Because of the lack of bounded waiting, some processes may never get the opportunity to execute in the critical section.

  • Busy Waiting: The reliance on busy waiting can be inefficient, especially when there is significant contention for the lock.

Conclusion

The Test and Set lock is a hardware-based method used to ensure mutual exclusion, allowing one process to enter the critical section at a time. While it is simple and effective approach to synchronization, it has certain drawbacks, including the potential for starvation and the inefficiencies introduced by busy waiting. Despite these challenges, the Test and Set lock remains a useful tool for managing access to shared resources in certain computing environment.

Advertisements