You are on page 1of 10

Synchronization Hardware

Overview
• Hardware Locks are used to solve the problem of process
synchronization.
• The process synchronization problem occurs when more than one
process tries to access the same resource or variable.
• If more than one process tries to update a variable at the same time
then a data inconsistency problem can occur.
• This process synchronization is also called synchronization
hardware in the operating system.
Synchronization Hardware
• Process Syncronization problem can be solved by software as well
as a hardware solution. 
• Peterson solution is one of the software solutions to the process
synchronization problem.
• Peterson algorithm allows two or more processes to share a single-
use resource without any conflict.
• The hardware solution is as follows:
• Test and set
• Swap and unlock
• lock algorithm
Test and Set
Test and Set
boolean lock = false;
boolean TestAndSet(boolean target){
boolean returnValue = target;
target = true;
return returnValue;
}
Do
{
while(TestAndSet(lock));
CRITICAL SECTION CODE;
lock = false;
REMAINDER SECTION CODE;
}
Swap Function
• Swap function uses two boolean variables lock and key.
• Both lock and key variables are initially initialized to false. 
• Swap algorithm is the same as lock and set algorithm.
• The Swap algorithm uses a temporary variable to set the lock to true
when a process enters the critical section of the program.
Swap
boolean lock = false;
individual key = false;
void swap(boolean &a, boolean &b){
boolean temp = a;
a = b;
b = temp;
}
do{
key=true;
while(key){
swap(lock,key);
}
CRITICAL SECTION CODE
lock = false;
REMAINDER SECTION CODE
}
Lock Algorithm
• One of the oldest methodologies
• It can be implemented for many processes
Do
{
Acquire(lock);
Critical Section;
Release(lock);
}
Lock algorithm
While (lock==1);
Lock=1;
Critical section
Lock=0;
Peterson’s solution

You might also like