0% found this document useful (0 votes)
59 views

Synchronization and Deadlocks

The document discusses critical section problems, semaphores, and deadlocks. It defines critical sections as code segments where shared data is accessed. A solution to critical section problems must satisfy mutual exclusion, progress, and bounded waiting. Semaphores are used as synchronization tools to overcome critical section problems. Deadlocks occur when a process requests a resource that is not available, causing it to wait indefinitely. The four conditions for deadlock are mutual exclusion, hold and wait, no preemption, and circular wait. Methods to handle deadlocks include prevention, avoidance, and detection and recovery.

Uploaded by

sathwick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Synchronization and Deadlocks

The document discusses critical section problems, semaphores, and deadlocks. It defines critical sections as code segments where shared data is accessed. A solution to critical section problems must satisfy mutual exclusion, progress, and bounded waiting. Semaphores are used as synchronization tools to overcome critical section problems. Deadlocks occur when a process requests a resource that is not available, causing it to wait indefinitely. The four conditions for deadlock are mutual exclusion, hold and wait, no preemption, and circular wait. Methods to handle deadlocks include prevention, avoidance, and detection and recovery.

Uploaded by

sathwick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

Chapter 3

Synchronization & Deadlocks

Critical Section:

Each process has a code segment, called critical section, in which the shared data is accessed and it
ensure that when one process is executing in its critical section, no other process is allowed to execute in its
critical section. This called critical section problem and designed as follow

Each process must request permission to enter its critical section

The section of code implementing this request is the entry section

It is followed by an exit section.

The remaining code is the remainder section

The general structure of a typical process Pi is shown in figure below

do {

entry section

critical section

exit section

reminder section

} while (1);

Solution to Critical-Section Problem must satisfy the following three requirements:

Mutual Exclusion. If process Pi is executing in its critical section, then no other processes can be
executing in their critical sections.

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.

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.

3.3 Semaphores:

To overcome the difficulties of critical section problem, we use a synchronization tool called a
Semaphore. It is an integer variable S that can be initialized. It can be accessed only through the standard
operation.

1. Wait (termed as P)

2. Signal (termed as V)
The definitions of wait and signal are:

General form:

Wait (S)
{
while(S<=0); // no operation
}

Signal (S)
{
S++;
}

Usage:

OS distinguish between counting & binary semaphores.

The value of counting semaphore ranges an unrestricted domain.


The value of binary semaphore ranges only 0 and 1. It is also known as mutex locks.

Binary semaphore deals for multiple processes. The n processes share a semaphore mutex, initialized
to 1. Each process Pi is organized as

do {

wait(mutex);

//critical section

Signal(mutex);

//remainder section

}while(TRUE);

Counting semaphores can be used to control access to a given resources consisting of a finite number
of instances. The semaphore is initialized to the number of resources available.
Deadlock

Deadlock:
A process requests a resource and it is not available at that time, then process enters a waiting state.
Sometimes, a waiting process will never change its state, because the resources they have requested are held
by other waiting process. This situation is called DEADLOCK.

4.1 System model:


A system consists of a finite number of resources to be distributed among a number of processes. 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 carryout its designated task.
Under the normal mode operation, process may utilize a resource in only the following sequence.
1. Request: The process requests the resource. If the request cannot be granted immediately, it enters to
waiting state until it acquires the resource.
2. Use: The process operates on the resource which is acquired by request.
3. Release: After the completion of the task using the resources. All resources are released.

4.2 Deadlock characterization:


In a deadlock, processes never finish executing, and system resources are tied up, preventing other jobs from
starting.
4.2.1 Necessary conditions:
A deadlock situation can arise if the following 4 conditions hold simultaneously in a system
1. Mutual exclusion:-
One resources must be held in non-shareable mode. Here, only one process at a time can use
the resource. If another process requests that resource, the requesting process must be delayed until the
resource has been released.

2. Hold and Wait:-


A process must be holding at least one resource and waiting to acquire another additional
resource that are currently being held by other process.

3. No Preemption:-
Resources cannot be preempted. Resource granted to a process can be released voluntarily,
after the process has completed its task.
4. Circular Wait:- Deadlock
processes are involved in a circular chain such that each process holds one or more resource being
requested by the next process in the chain.

4.2.2 Resource Allocation Graph (RAG) :-


A deadlock is described in terms of a directed graph called as a system resource allocation graph. This
consists of
a. A set of vertices, V.
b. A set of edges, E.
The set of vertices is again divided into 2 categories.
a. Set of all active processes
P = { P1,P2,.........,PN }
b. Set of all different type of resources
R = {R1,R2,........,RM }
There are 2 types of edges in the resources allocation graph:-
i. Pi Rj
It signifies that Pith process is requesting one unit of the resource type Rj. This edge is called as request
edge.
ii.
Rj Pj
It signifies that one unit of ith resource is held by the process j. This edge is also called as an
allocation edge or assignment edge.
The RAG is shown in fig below with following situation.
Set P, R and E :
P = { P1, P2, P3 }
R = { R1, R2, R3, R4 }
E = { P1 R1, P2 R3, R1 P2, R2 P2, R2 P1, R3 P3 }
Resource instances:
1- R1
2 - R2
1 - R3
3 R4
Process States:
Process P1 is holding an instance of resource type R2 and waiting for instance of resource of resource
type R1.
Process P2 is holding an instance of R1 and an instance of R2 and is waiting for an instance of R3.
Process P3 is holding an instance of R3.

By RAG. if a graph contain a cycle, then process in the system is deadlocked


If the graph contains no cycle, then no process in the system is deadlocked.

4.3 Methods for handling Deadlock:


Deadlock can be handled by any of the following methods.
1. we can use a protocol to prevent or avoid deadlocks, ensuring that the system will never enter a
deadlocked state.
2. Allow the system to enter a deadlocked state, detect it and recover
3. We can ignore & pretend that deadlocks never occur in the system.

To ensure that deadlock never occurs, deadlock prevention & deadlock avoidance scheme is used.

4.5 Deadlocks Prevention:


It provides a set of methods for ensuring that at ensuring that at least one of 4 necessary condition that
leads to deadlock, should not hold.
In method, we can prevent the occurrence of a deadlock by
Mutual exclusion:
A mutual exclusion must hold for non-sharable resource. Sharable resources donot require
mutual exclusion and thus cannot be involved in deadlocked.
Example: Printer shared by several processes requires mutual exclusion.
Read only file shared by several processes does not requires mutual exclusion.
Hold and Wait:
we need to ensure that hold and wait condition never occurs in the system. i.e.., when a process
requests a resource, it does not hold any other resource. This can be handled by using any one of these 2
protocols.
Here, each process must request and allocate all its resources before it begins its execution.
Example: process needs DVD drive, disk and printer to copy a file and print it. Hence it requests all
the 3 resources at the beginning only.
Here, process must request resources only when it has no resources hold.
Example: Process will request DVD drive and disk file to copy a file and release both the DVD drive
and Disk then, process will request the disk and the printer and performs the remaining task.

Both these 2 protocols have 2 main advantages:


1. Resource utilization is low.
2. Process may have to wait indefinite time, as it requires many resources. Hence starvation occurs.

No Preemption:
Here, we say that if a process is holding some resource and requests another resource that
cannot be immediately allocated to it, then all resources currently being held must be preempted.
The process will be restarted only when it can regain its old resources, as well as the new ones that it is
requesting.

Circular Wait:
To ensure that this condition never holds, we must order all resource types. It is required that
each process can only request the required resource in an increasing order.
Example: A set of resource type are assigned with following number.
F ( Tape Drive ) 1 R1
F ( Disk Drive ) 5 R2
F ( Printer ) 12 R3
Process can request R1, R2, R3 in order, but cant request as follow.
1. R1, R3, R2 or
2. R3, R2, R1 or
3. R2, R3, R1
i.e.., process can request resource only if F (R j) > F (R i)

Requested resource Allocated resource.


If F (Rj) < F (Ri) then release all allocated resource and restart from first.

4.5 Deadlock Avoidance:


Deadlock Avoidance requires that the system has some additional a priori information available.
Each process declare the maximum number of resources of each type that it may need.
The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure
that there can never be a circular-wait condition.
Resource-allocation state is defined by the number of available and allocated resources, and the
maximum demands of the processes

4.5.1 Safe State:


A state is safe if the system can allocate resources to each process in some order and still avoid a deadlock.
A system is in a safe state only if there exists a safe sequence. A sequence of processes < P1, P2,......... Pn>
is a safe sequence when each process are provided by there required resources without entering into deadlock.
If no such sequence exists, then the system state is said to be unsafe and may enter into deadlock as shown in
the fig below.
For example, we consider a system with twelve magnetic tape drives and 3 processes P0, P1, and P2.
The number of tape drives needed by each process and the number of type drives are been allocated at time T0
is shown below.
Max needs Current needs time T0
P0 10 5
P1 4 2
P2 9 2

At time To, the system is in a safe state. The sequence < P1, P0, P2 > satisfies the safety condition.
i.e.., After allocating tape drives for each process at time To. The remaining tape drives is 3. So at time T1 we
can immediately allocate the 2 tape drives for process P1, after P1 releases its tape drives i.e.., 4, we can
allocate tape drives to P0 which is 5 then finally when P0 releases its tape drives we can allocate tape drives to
P2. i.e.., 7
A system can go to unsafe state when process P2 allocates, tape drives at T1, along with P1. This is
because when P1 releases tape drive then there will be only 4 tape drive and not 5. Hence P0 and P1 will enter
in to a deadlock as they require more than 4 tape drive to release tape drive which they have holded.
Hence in safe state algorithm, the system will grant the request only if the allocation leaves the system
in a safe state.

4.5.2 RAG Algorithm:


The resource allocation graph algorithm (RAG) is applicable to a resource allocation system with
single instance of each resource type.
In addition to the request and the assignment edges, we introduce a new type edge called as a claim
edge.
A claim edge, Pi Rj indicates that a process Pi may request resource Rj at sometime in the future.
This edge resembles a request edge in direction but is represented by a dashed line.
Before process Pi starts executing, all its claim edges must already appear in the RAG. Now suppose that
process Pi requests resources 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 RAG. We check this for safety
by using a cycle detection algorithm.
If no cycle exists, then the allocation of the resources will be carried as it will make system in a safe state.
To illustrate this algorithm, we consider the RAG of fig 4.1(a). Suppose that P2 requests R2. Although R2 is
currently free, we cannot allocate it to P2, since this action will create a cycle has shown in the graph fig
4.1(b)
Fig 4.1(a) fig 4.1(b)

4.5.3 Bankers Algorithm:


Bankers algorithm is used to a resource allocation system with multiple instance of each resource type.
When a new process enters the system, it must declare the maximum number of instance 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 a safe state. If so, then the resources are allocated else the process must wait until some
other process releases enough resources.

Data Structures for the Bankers Algorithm


Let n = number of processes, and m = number of resources types.
Available: Vector of length m. If available [j] = k, there are k instances of resource type Rj
available.
Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances of resource
type Rj.
Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj.
Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task.
Need [i,j] = Max[i,j] Allocation [i,j].

Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively. Initialize:
Work = Available
Finish [i] = false for i - 1,3, , n.
2. Find and i such that both:
(a) Finish [i] = false
(b) Needi Work
If no such i exists, go to step 4.
3. Work = Work + Allocationi
Finish[i] = true
go to step 2.
4. If Finish [i] == true for all i, then the system is in a safe state.

Resource-Request Algorithm for Process Pi


Request = request vector for process Pi. If Requesti [j] = k then process Pi wants k instances of resource type
Rj.
1. If Requesti Needi go to step 2. Otherwise, raise error condition, since process has exceeded its
maximum claim.
2. If Requesti Available, go to step 3. Otherwise Pi must wait, since resources are not available.
3. Pretend to allocate requested resources to Pi by modifying the state as follows:
Available = Available = Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi Requesti;;
If safe the resources are allocated to Pi.
If unsafe Pi must wait, and the old resource-allocation state is restored

4.6 Deadlock Detection:


Another method of handling deadlocks is to allow the system to enter a deadlock state and then,
Use an algorithm that examines the state of the system to determine whether a deadlock has occurred.
And then
Use an algorithm to recover from the deadlock.
In deadlock detection technique, resources are granted to the requesting processes without any check. Then
the system state is examined periodically to determine if a set of processes is deadlocked.
There are 2 algorithms for deadlock detection:
1. Single Instance of each resource type
2. Several Instance of a resource type.
Single Instance Of Each Resource Type:
In this, wait for graph method is used. All resources only a single instance. This graph is obtain from the
resource allocation graph by removing the node of type resource and collapsing the appropriate edges.
A deadlock exists in the system if an only if the wait for graph contains a cycle. To detect deadlock the
system needs to maintain the wait for a graph and periodically to invoke an algorithm that searches for a
cycle in the graph.
An algorithm to detect a cycle in a graph requires an order of n2 operations where n is the number of
vertices in th graph.

Example: consider the following RAG and its corresponding wait for graph is:

Several Instances Of a Resource Type:


In this, we use algorithm i.e.., similar to bankers algorithm.
Data structures used:
1. Available: A vector of length m indicates the number of available resource of each type.
2. Allocation: An n x m matrix defines the number of resources of each type currently allocated to each
process.
3. Request: An n x m matrix indicates the current request of each process.
Then deadlock detection algorithm is developing which will simply investigates every possible allocation
sequence for the processes that remain to be completed.
Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively Initialize:
(a) Work = Available
(b) For i = 1,2, , n, if Allocationi 0, then
Finish[i] = false;otherwise, Finish[i] = true.
2. Find an index i such that both:
(a) Finish[i] == false
(b) Requesti Work
If no such i exists, go to step 4.
3. Work = Work + Allocationi
Finish[i] = true
go to step 2.
4. If Finish[i] == false, for some i, 1 i n, then the system is in deadlock state. Moreover, if Finish[i]
== false, then Pi is deadlocked.

This algorithm requires an order of m x n 2 operations to detect whether the system is in a deadlocked state.
Deadlock Algorithm Usage:
When, and how often, to invoke depends on:
o How often a deadlock is likely to occur?
o How many processes will need to be rolled back?
one for each disjoint cycle
If detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph and so we
would not be able to tell which of the many deadlocked processes caused the deadlock.

4.7 Recovery from Deadlock:


When a detection algorithm determines that a deadlock exists, the system must recover from the deadlock by
using any one of these 2 options by breaking a deadlock.
a. To abort one or more processes to break the circular wait. i.e.., Process Termination.
b. To pre-empt some resources from one or more of the deadlocked processes. i.e.., Resource Pre-
emption.

Process Termination:
In order to eliminate the deadlock by aborting a process, two methods can be utilized.
a. Abort all deadlocked processes:
This method clearly will break the deadlock cycle but at great expense, since these processes
may have computed for a long period of time.
b. Abort one process at a time until the deadlock cycle is eliminated:
This method is considered as overhead as detection algorithm must be invoked after each
process is aborted to determine whether deadlock is still exists.
c. In which order should we choose to abort?
a. Priority of the process.
b. How long process has computed, and how much longer to completion.
c. Resources the process has used.
d. Resources process needs to complete.
e. How many processes will need to be terminated?
f. Is process interactive or batch?

Resource Pre-emption:
We may pre-empt some of the resources for recovery from the deadlock state and allocate these resource to
other processes until the deadlock cycle is broken.
If pre-emption is required to deal with deadlocks, then 3 issues need to be addressed.
a. Selecting a Victim:
The selection of which resource and which processes are to be pre-empted, depends on cost
factors.
b. Rollback:
The simplest solution of resource pre-emption is total rollback, abort the process and then
restart it. However it is more effective to rollback. The process only as far as necessary to break the
deadlock. This method however, requires the system to keep more information about the state of all the
running processes.
c. Starvation:
Sometimes if selection of process is dependent upon the cost factor, then same process may be
selected again and again. This process never completes its designated task. This situation is called as
Starvation. So process can be picked up victim only for finite number of times.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy