Professional Documents
Culture Documents
UNIT- 3
SYNCHRONIZATION AND DEADLOCKS
Background: A situation in which several processes access and manipulate the same data
concurrently and the outcome of the execution depends on the particular order in which
the access takes place is called race condition. To prevent race conditions, concurrent
processes must be synchronized
The important feature of the system is that, when one process is executing in its
critical section, no other process is to be allowed to execute in its critical section.
The critical-section problem is to design a protocol that the processes can use to
cooperate. Each process must request to enter its critical section. The section of code
implementing this request is the “entry section”.
Do
{
Entry section
Critical section
Exit section
Remainder section
} while (1);
Fig 3.1 General structure of Critical-section problem
The critical section is followed by an “Exit section”. The remaining is the
remainder section.
2. Progress: - If no process is executing in its critical section and there exist some
processes that wish to enter their critical section, then the selection of the processes that
will enter the critical section next cannot be postponed indefinitely.
3. Bounded waiting: - A bound must exist 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.
There are two approaches used to handle critical section in operating system
1. Preemptive kernels
2. Non-Preemptive kernels
3.1.1 Semaphores
A semaphore is a synchronization tool used to solve the critical-section problem of mutual
exclusive.
A semaphore can be accessed only through two standard operations. They are
1. Wait ( )
2. Signal ( )
The definition of wait ( ) and signal ( ) are
Definition of wait ( ) Definition of signal ( )
Wait (S) signal (S)
{ {
while S<=0 S++;
S- -; }
}
S- is the semaphore integer variable
When one process modifies the semaphore values, then no other process can
simultaneously modify that same semaphore value. Operating systems often distinguish
between counting and binary semaphore.
The value of a binary semaphore can range between 0 and 1. On some systems,
binary semaphores are known as mutex locks, as they are locks that provide
mutual exclusion.
We can use binary semaphores to deal with critical- section problem for multiple
processes. The n processes share a c semaphore, mutex, initialized to 1. Each process Pi is
organized as below
Do
//critical section
//remainder section
} while (true);
Processes will share common semaphore variable called “mutx” (mutual exclusion).
DEADLOCKS:
Introduction
The waiting process will never change their state because their resource they have
requested on held by other waiting process. This situation is called as “Deadlock”.
OR
A set of blocked processes each holding a resource and waiting to acquire a resource held
by another process in the set.
1. Preemptable resource - A resource, that can be taken away from the running process
that holds the resource that may causing system failure.
A process must request a resource before using it and must release the resource after using
it. A process may request as many resources as it requires to carry out its designated task.
The number of resources requested may not exceed the total number of resources available
in the system.
Under the normal mode of operation, a process may utilize a resource in only the
following sequence.
Request - If the request cannot be granted immediately then the requesting process
must wait until it can acquire the resource.
2. Hold and Wait - A process must be holding at least one resource and waiting to acquire
additional resources that are currently being held by other processes.
4. Circular wait - There must exists a set of waiting process {P0, P1, P2, and P3.....Pn}
such that P0 is waiting for a resource that is held by P1. P2 is waiting for a resource that is
held by P2....Pn-1 is waiting for a resource that is held by P0 and Pn is waiting for a resource
that is held by P0.
Process
Pi Requests instance of Rj
Pi is holding an instance of Rj
Request edge must points to only the rectangle Rj, whereas an assignment edge must also
designate one of the dots in the rectangle.
When process Pi requests an instance of resource type Rj, a request edge is inserted in the
resource-allocation graph. When this request can be fulfilled, the request edge is
instantaneously transformed to an assignment edge. When the process no longer needs
access to the resource, it releases the resource; as a result. The assignment edge is deleted.
Computer Science and Engineering 45
Operating System:15CS43T 2020-21
Resource instances:
Process state
Process P1 is holding resource type R2 & waiting for resource types R1.
Process P2 is holding resource type R1 & R2 & waiting for resource type R3.
Given definition of a resource-allocation graph, it can be shown that, if the graph contains
no cycles, then no process in the system is deadlocked. If the graph does contain a cycle,
then a deadlock may exist.
Cycles:
P1R1P2R3P3R2P1
P2R3P3R2P2
Cycle P1R1P3R2P1
3. Deadlock Detection-- Allow the system to enter a deadlock state, detect it and
recover.
1. Mutual Exclusion – The mutual- exclusion condition must hold for non-sharable
resources. The sharable resources cannot have deadlock because there is no mutual
exclusion condition.
2. Hold and Wait – must guarantee that whenever a process requests a resource, it does
not hold any other resources.
Require process to request and be allocated all its resources before it begins
execution, or allow process to request resources only when the process has none.
3. No Preemption –If a process that is holding some resources requests another resource
that cannot be immediately allocated to it, then all resources currently being held are
released.
Preempted resources are added to the list of resources for which the process is
waiting.
Process will be restarted only when it can regain its old resources, as well as the
new ones that it is requesting.
4. Circular wait - One way to ensure that this condition never holds is to impose a total
ordering of all resources types and to require that each process requests resources in an
increasing order of enumeration
The simplest and most useful model requires that each process declare the
maximum number of resources of each type that it may need.
A deadlock-avoidance algorithm dynamically examines the resource-allocation
state to ensure that a circular wait condition can never exist.
The resource-allocation state is defined by the number of available and allocated
resources and the maximum demands of the process.
2. Resource-allocation-graph algorithm
3. Banker’s algorithm
allocation state if, for each Pi, the resource requests that Pi can still make can be satisfied
by the currently available resources plus the resources held by all Pj with j<i
A deadlocked state is on “unsafe state”. In an unsafe state, the operating system cannot
prevent processes form requesting resource such that a deadlock occurs.
So we can define deadlock avoidance algorithm: If the system is in safe state, the
system can be deadlocked and if the system is in unsafe state, then system can in deadlock.
Suppose that process Pi requests resource Rj. The request can be granted only if
converting the request edge Pi->Rj to an assignment edge Rj->Pi does not result in the
formation of a cycle in the resource –allocation graph. If no cycles exists, then the
allocation of the resource will leave the system in a safe state. If a cycle if found, then the
allocation will put the system in an unsafe state. Therefore, process Pi will have to wait for
its request to be satisfied.
3 Banker’s Algorithm
The banker’s algorithm uses an “avoidance policy”. This algorithm is applicable to
resource allocation systems with multiple instances of each resource type.
When a new process enters the system, it must declare the maximum number of instances
of each resource type that it may need. This number may not exceed the total number of
resources in the system. When a user requests a set of resources, the system must
determine whether the allocation of these resources will leave the system in safe state. If it
will, the resources are allocated; otherwise, the process must wait until some other process
releases enough resources.
Implementation:
Let ‘n’ indicates the number of processes in the system & ‘m’ indicates the number of
resource types, then following data structures are needed for the banker’s algorithm.
Max:-an “n*m” matrix defines the maximum demand of each process. If max[i][j]
equals k, then process Pi may request at most k instance of resource type Rj.
Need: - An “n*m” matrix indicates the remaining resource need of each process. If
Need[i][j] equals k, then process Pi need K more instances of resource type Rj to
complete its task. Need[i][j] = Max[i][j]-Allocation[i][j]
This algorithm may require an order of m*n2 operations to determine whether a state is
safe
Resource-Request Algorithm:
This algorithm is used to determine if requests can be safely granted.
Let Requesti be the request vector for process Pi. If Requesti[j] == k, then process Pi
wants k instances of resource type Rj. When a request for resources is made by process Pi,
the following actions are taken.
Example:
5 Processes P0 through P4;
3 resource types : A (10 instances), B (5 Instances) and C (7 instances)
Snapshot at time T0
Process Allocation Max Available
ABC AB ABC
P0 010 7C
53 332
P1 200 322
P2 302 902
P3 211 222
P4 002 433
Solution:
Process Need = Max - Allocation
ABC
P0 010
P1 200
P2 302
P3 211
P4 002
The system is in a safe sequence state since the sequence P1, P3, P4, P2, P0 satisfies
safety criteria.
1. The algorithms are used to test the state of the system to determine whether a
deadlock has occurred or not.
2. An algorithm to recover from the deadlock.
There are two types of methods are used to detect the deadlock
1. Single instance of each resource type
2. Several instance of resource type
PiRq and
RqPj for some resource Rq.
A deadlock exits in the system if and only if the wait-for graph contains a cycle. To detect
deadlock, the system needs to maintain the wait-for graph and periodically invoke an
algorithm that searches for cycle in the graph.
The below figure 3.2 illustrate the example to represent a resource-allocation graph and
the corresponding wait-for graph.
P5
P5
R1 R3 R4
P1 P1 P2 P3
P2 P3
R2 R5
P4 P4
The deadlock detection algorithm of a system with multiple instance of resource type uses
the following data structure.
If request [i][j] equals k, then process Pi is requesting k more instance of resource type Rj.
Algorithm
1 Let Work and Finish be vectors of length m and n respectively. Initialize
Work = Available for i= 0, 1,....., n-1, If Allocationi ≠ 0 , then Finish[i] = false for
This algorithm may require an order of m*n2 operations to determine whether the system
is in a deadlocked state.
Example:
Consider a system with five processes P0 through P4 and three resources types A, B,C.
Resource A has seven instances, resource type B has two instances, and resource type C
has six instances.
P3 211 100
P4 002 002
If we execute the algorithm in the sequence <P0, P2, P3, P1, P4> then the system is not in
deadlock.
This method will break the deadlock cycle but it is expensive since the aborted
process may have computed for a long time and must be recomputed later.
This method involves overhead since after each process is aborted, a deadlock
detection algorithm must be invoked to determine whether any process are still
deadlocked.
In this method, resources are preempted from process and are given to other process
successively until the deadlock cycle is broken.
Starvation: - pick the victim in such a way not starvation situation occurs.
References:
[1] Operating System Principles – Abraham Silberschatz, Peter Baer Galvin, Greg
Gagne, 8th edition, Wiley-India.
[2 ] Operating Systems, I. Chandra Mohan, PHI, 2013
[3 ] http://www.tutorialspoint.com/operating_system/
[4] http://courses.cs.vt.edu/~csonline/OS/Lessons/index.html
[5] http://www.nptel.ac.in