Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  Operating System  >  Hardware Synchronization Algorithms: Unlock & Lock, Test & Set, Swap

Hardware Synchronization Algorithms: Unlock & Lock, Test & Set, Swap | Operating System - Computer Science Engineering (CSE) PDF Download

Introduction

Process Synchronization problems occur when two processes running concurrently share the same data or same variable. The value of that variable may not be updated correctly before its being used by a second process. Such a condition is known as Race Around Condition. There are a software as well as hardware solutions to this problem. In this article, we will talk about the most efficient hardware solution to process synchronization problems and its implementation.
There are three algorithms in the hardware approach of solving Process Synchronization problem: 

  • Test and Set 
  • Swap 
  • Unlock and Lock 

Hardware instructions in many operating systems help in the effective solution of critical section problems. 

Test and Set:

Here, the shared variable is lock which is initialized to false. TestAndSet(lock) algorithm works in this way – it always returns whatever value is sent to it and sets lock to true. The first process will enter the critical section at once as TestAndSet(lock) will return false and it’ll break out of the while loop. The other processes cannot enter now as lock is set to true and so the while loop continues to be true. Mutual exclusion is ensured. Once the first process gets out of the critical section, lock is changed to false. So, now the other processes can enter one by one. Progress is also ensured. However, after the first process, any process can go in. There is no queue maintained, so any new process that finds the lock to be false again can enter. So bounded waiting is not ensured. 

Test and Set Pseudocode – 

//Shared variable lock initialized to false

boolean lock;

boolean TestAndSet (boolean &target){

    boolean rv = target;

    target = true;

    return rv;

}

while(1){

    while (TestAndSet(lock));

    critical section

    lock = false;

    remainder section

}

Swap

Swap algorithm is a lot like the TestAndSet algorithm. Instead of directly setting lock to true in the swap function, key is set to true and then swapped with lock. First process will be executed, and in while(key), since key=true , swap will take place and hence lock=true and key=false. Again next iteration takes place while(key) but key=false , so while loop breaks and first process will enter in critical section. Now another process will try to enter in Critical section, so again key=true and hence while(key) loop will run and swap takes place so, lock=true and key=true (since lock=true in first process). Again on next iteration while(key) is true so this will keep on executing and another process will not be able to enter in critical section. Therefore Mutual exclusion is ensured. Again, out of the critical section, lock is changed to false, so any process finding it gets t enter the critical section. Progress is ensured. However, again bounded waiting is not ensured for the very same reason. 

Swap Pseudocode – 

// Shared variable lock initialized to false 

// and individual key initialized to false;

boolean lock;

Individual key;

void swap(boolean &a, boolean &b){

    boolean temp = a;

    a = b;

    b = temp;

}

while (1){

    key = true;

    while(key)

         swap(lock,key);

    critical section

    lock = false;

    remainder section

Unlock and Lock 

Unlock and Lock Algorithm uses TestAndSet to regulate the value of lock but it adds another value, waiting[i], for each process which checks whether or not a process has been waiting. A ready queue is maintained with respect to the process in the critical section. All the processes coming in next are added to the ready queue with respect to their process number, not necessarily sequentially. Once the ith process gets out of the critical section, it does not turn lock to false so that any process can avail the critical section now, which was the problem with the previous algorithms. Instead, it checks if there is any process waiting in the queue. The queue is taken to be a circular queue. j is considered to be the next process in line and the while loop checks from jth process to the last process and again from 0 to (i-1)th process if there is any process waiting to access the critical section. If there is no process waiting then the lock value is changed to false and any process which comes next can enter the critical section. If there is, then that process’ waiting value is turned to false, so that the first while loop becomes false and it can enter the critical section. This ensures bounded waiting. So the problem of process synchronization can be solved through this algorithm. 

Unlock and Lock Pseudocode 

// Shared variable lock initialized to false 

// and individual key initialized to false

boolean lock;

Individual key;

Individual waiting[i];

while(1){

    waiting[i] = true;

    key = true;

    while(waiting[i] && key)

        key = TestAndSet(lock);

    critical section

    j = (i+1) % n;

    while(j != i && !waiting[j])

         j = (j+1) % n;

    if(j == i)

         lock = false;

    else

         waiting[j] = false;

    remainder section

The document Hardware Synchronization Algorithms: Unlock & Lock, Test & Set, Swap | Operating System - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Operating System.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
10 videos|99 docs|33 tests

Top Courses for Computer Science Engineering (CSE)

10 videos|99 docs|33 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Hardware Synchronization Algorithms: Unlock & Lock

,

Test & Set

,

Test & Set

,

Previous Year Questions with Solutions

,

Sample Paper

,

practice quizzes

,

Objective type Questions

,

Exam

,

video lectures

,

pdf

,

Important questions

,

Hardware Synchronization Algorithms: Unlock & Lock

,

Hardware Synchronization Algorithms: Unlock & Lock

,

ppt

,

MCQs

,

shortcuts and tricks

,

Summary

,

study material

,

past year papers

,

Swap | Operating System - Computer Science Engineering (CSE)

,

Swap | Operating System - Computer Science Engineering (CSE)

,

mock tests for examination

,

Test & Set

,

Extra Questions

,

Viva Questions

,

Free

,

Swap | Operating System - Computer Science Engineering (CSE)

,

Semester Notes

;