08 UNIT-3 Lect 1 & 2
08 UNIT-3 Lect 1 & 2
Process Synchronization and Deadlock: The Critical Section Problem, Synchronization Hardware,
Semaphores, Monitors, Classical Problems of Synchronization, Critical Region, Deadlock System
Model, Characterization, Deadlock Prevention, Detection and Avoidance, Recovery from Deadlock,
Combined approach to handle Deadlock, Banker’s Algorithm
A cooperating process is one that can affect or be affected by other processes executing in the
system. Cooperating processes can either directly share a logical address space (that is, both
code and data) or be allowed to share data only through files or messages.
Process Synchronization means sharing system resources by processes in a such way that,
Concurrent access to shared data is handled thereby minimizing the chance of inconsistent data.
Maintaining data consistency demands mechanisms to ensure synchronized execution of cooperating
processes.
Process Synchronization was introduced to handle problems that arose while multiple process
executions.
The Critical-Section Problem
Consider a system consisting of n processes {P0, P1, ..., Pn−1}. Each process has a segment of code,
called a critical section, in which the process may be changing common variables, updating a table,
writing a file, and so on. The important feature of the system is that, when one process is executing in
its critical section, no other process is allowed to execute in its critical section. That is, no two
processes are executing in their critical sections at the same time.
The critical-section problem is to design a protocol that the processes can use to cooperate. Each
process must request permission to enter its critical section. The section of code implementing this
request is the entry section. The critical section may be followed by an exit section. The remaining
code is the remainder section.
A solution to the critical-section problem must satisfy the following three requirements:
1. Mutual exclusion. If process Pi is executing in its critical section, then no other processes can be
executing in their critical sections.
2. Progress. If no process is executing in its critical section and some processes wish to enter their
critical sections, then only those processes that are not executing in their remainder sections can
participate in deciding which will enter its critical section next, and this selection cannot be postponed
indefinitely.
3. Bounded waiting. There exists a bound, or limit, on the number of times that other processes are
allowed to enter their critical sections after a process has made a request to enter its critical
section and before that request is granted.
Synchronization Hardware
Many systems provide hardware support for critical section code. The critical section problem could be solved
easily in a single-processor environment if we could disallow interrupts to occur while a shared variable or
resource is being modified.
In this manner, we could be sure that the current sequence of instructions would be allowed order without pre-
emption. Unfortunately, this solution is not feasible in a multiprocessor environment.
Disabling interrupt on a multiprocessor environment can be time consuming as the message is passed to all
the processors.
This message transmission lag, delays entry of threads into critical section and the system efficiency
decreases.
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:
1. Test and Set
2. Swap
3. Unlock and Lock
Test and Set
Test and set algorithm uses a boolean variable 'lock' which is initially initialized to false. This lock
variable determines the entry of the process inside the critical section of the code.
In test and set algorithm the incoming process trying to enter the critical section does not wait in a queue so
any process may get the chance to enter the critical section as soon as the process finds the lock variable
to be false. It may be possible that a particular process never gets the chance to enter the critical section
and that process waits indefinitely.
Swap
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.
while(1)
boolean lock = false; {
individual key = false; key=true;
void swap(boolean &a, boolean &b) while(key)
{ {
boolean temp = a; swap(lock,key);
a = b; }
b = temp; CRITICAL SECTION CODE
} lock = false;
REMAINDER SECTION CODE
}
Unlock and lock
Unlock and lock algorithm uses the TestAndSet method to control the value of lock. Unlock and lock
algorithm uses a variable waiting[i] for each process i. Here i is a positive integer i.e 1,2,3,... which
corresponds to processes P1, P2, P3... and so on. waiting[i] checks if the process i is waiting or not to
enter into the critical section.
All the processes are maintained in a ready queue before entering into the critical section. The
processes are added to the queue with respect to their process number. The queue is the circular
queue.
while(1) {
boolean lock = false; waiting[i] = true;
Individual key = false; key = true;
Individual waiting[i]; while(waiting[i] && key)
boolean TestAndSet(boolean &target) {
{ key = TestAndSet(lock);
boolean returnValue = target; }
target = true; CRITICAL SECTION CODE
return returnValue; j = (i+1) % n;
} while(j != i && !waiting[j])
j = (j+1) % n;
if(j == i)
lock = false;
else waiting[j] = false;
REMAINDER SECTION CODE
}