0% found this document useful (0 votes)
11 views7 pages

Deadlock

Operating system deadlock and prevention chapter 5 diploma cs 5 sem

Uploaded by

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

Deadlock

Operating system deadlock and prevention chapter 5 diploma cs 5 sem

Uploaded by

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

In a multiprogramming environment, several processes may compete for a finite number of resources.

A
process requests resources; if the resources are not available at that time, the process enters a waiting
state. Sometimes, a waiting process is never again able to change state, because the resources it has
requested are held by other waiting processes. This situation is called a deadlock

System Model

A system consists of a finite number of resources to be distributed among a number of competing


processes. If a process requests an instance of a resource type, the allocation of any instance of the type
will satisfy the request. 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.
Obviously, 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. The process requests the resource. If the request cannot be granted immediately (for example,
if the resource is being used by another process), then the requesting process must wait until it can
acquire the resource.

Use. The process can operate on the resource (for example, if the resource is a printer, the process can
print on the printer).

Release. The process releases the resource.

Necessary Conditions
Mutual exclusion. At least one resource must be held in a nonsharable mode; that is, 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.

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.

No preemption. Resources cannot be preempted; that is, a resource can be released only voluntarily by
the process holding it, after that process has completed its task

Circular wait. A set { P0 , Pl, ... , P11 } of waiting processes must exist such that Po is waiting for a
resource held by P1, P1 is waiting for a resource held by P2, ... , Pn-1 is waiting for a resource held by P,v
and P11 is waiting for a resource held by Po.

Resource-Allocation Graph
Deadlocks can be described more precisely in terms of a directed graph called a System Resource
Allocation Graph. This graph consists of a set of vertices V and a set of edges E. The set of vertices Vis
partitioned into two different types of nodes: P == { P1, P2, ... , Pn}, the set consisting of all the active
processes in the system, and R == {R1, R2, ... , R} the set consisting of all resource types in the system.

A directed edge from process pi to resource type Rj is denoted by Pi -+ Rj; it signifies that process P; has
requested an instance of resource type Rj and is currently waiting for that resource. A directed edge
from resource type Rj to process Pi is denoted by Rj -+ Pi it signifies that an instance of resource type R1
has been allocated to process Pi. A directed edge Pi -+ Rj is called a request edge a directed edge Rj -+ Pi
is called an assignment edge

we represent each process Pi as a circle and each resource type Rj as a rectangle. Since resource type Rj
may have more than one instance, we represent each such instance as a dot within the rectangle.
Suppose that process P3 requests an instance of resource type R2. Since no resource instance is
currently available, a request edge P3 ---+ R2 is added to the graph . At this point, two minimal cycles
exist in the system:
Processes P1, Pz, and P3 are deadlocked. Process Pz is waiting for the resource R3, which is held by
process P3. Process P3 is waiting for either process P1 or process Pz to release resource R2. In addition,

process P1 is waiting for process Pz to release resource R1.

METHODS FOR HANDLING DEADLOCKS

we can deal with the deadlock problem in one of three ways:

We can use a protocol to prevent or avoid deadlocks, ensuring that the system will never enter a
deadlocked state.

We can allow the system to enter a deadlocked state, detect it, and recover.

We can ignore the problem altogether and pretend that deadlocks never occur in the system

Deadlock Prevention provides a set of methods for ensuring that at least one of the necessary conditions
cannot hold. These methods prevent deadlocks by constraining how requests for resources can be
made.

Deadlock Avoidance requires that the operating system be given in advance additional information
concerning which resources a process will request and use during its lifetime. With this additional
knowledge, it can decide for each request whether or not the process should wait.
Deadlock Prevention
Mutual Exclusion

The mutual-exclusion condition must hold for non-sharable resources. Read-only files are a good
example of a sharable resource. If several processes attempt to open a read-only file at the same time,
they can be granted simultaneous access to the file.

Hold and Wait

To ensure that the hold-and-wait condition never occurs in the system, we must guarantee that,
whenever a process requests a resource, it does not hold any other resources. One protocol that can be
used requires each process to request and be allocated all its resources before it begins execution.

An alternative protocol allows a process to request resources only when it has none. A process may
request some resources and use them. Before it can request any additional resources, however, it must
release all the resources that it is currently allocated.

No Preemption

If a process is holding some resources and requests another resource that cannot be immediately
allocated to it (that is, the process must wait), then all resources the process is currently holding are
preempted. In other words, these resources are implicitly released. The preempted resources are added
to the list of resources for which the process is waiting. The process will be restarted only when it can
regain its old resources, as well as the new ones that it is requesting.

Alternatively, if a process requests some resources, we first check whether they are available. If they
are, we allocate them. If they are not, we check whether they are allocated to some other process that
is waiting for additional resources. If so, we preempt the desired resources from the waiting process and
allocate them to the requesting process. If the resources are neither available nor held by a waiting
process, the requesting process must wait. While it is waiting, some of its resources may be preempted,
but only if another process requests them.

Circular Wait

In this condition it is require that each process requests resources in an increasing order of
enumeration. To illustrate, we let R = { R1, R2, ... , Rm} be the set of resource types. We assign to each
resource type a unique integer number, which allows us to compare two resources and to determine
whether one precedes another in our ordering. Define function F: R ___,. N, where N is the set of natural
numbers. For example, if the set of resource types R includes tape drives, disk drives, and printers, then
the function F might be defined as follows:

F (tape drive) = 1
F (disk drive) = 5

F (printer) = 12

A process can initially request any number of instances of a resource type -say, Rj. After that, the
process can request instances of resource type Rj if and only if F(Rj) > F(Ri).

DEADLOCK AVOIDANCE

Safe State

A state is safe if the system can allocate resources to each process (up to its maximum) in some order
and still avoid a deadlock. More formally, a system is in a safe state only if there exists a safe sequence.
A sequence of processes is a safe sequence for the current 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.

consider a system with twelve magnetic tape drives and three processes: Po, P1, and P2. Process Po
requires ten tape drives, process P1 may need as many as four tape drives, and process P2 may need up
to nine tape drives. Suppose that, at time to, process Po is holding five tape drives, process P1 is holding
two tape drives, and process P2 is holding two tape drives. (Thus, there are three free tape drives.)

At time t0, the system is in a safe state. The sequence satisfies the safety condition. Process P1 can
immediately be allocated all its tape drives and then return them (the system will then have five
available tape drives); then process Po can get all its tape drives and return them (the system will then
have ten available tape drives); and finally process P2 can get all its tape drives and return them (the
system will then have all twelve tape drives available).

Banker's Algorithm
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 a safe state. If it will, the resources are allocated; otherwise, the
process must wait until some other process releases enough resources.

data structures must be maintained to implement the banker's algorithm is


Available.: A vector of length m indicates the number of available resources of each type.

Max. An n x m matrix defines the maximum demand of each process.

Allocation. An 11 x m matrix defines the number of resources of each type currently allocated to each
process.

Need. An n x m matrix indicates the remaining resource need of each process.

You might also like