Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Unit III CAL 817 Operating System

Unit 3 covers deadlocks in operating systems. It defines deadlocks as a situation where processes are blocked because each holds a resource waited by another. Four necessary conditions for deadlocks are discussed: mutual exclusion, hold and wait, no preemption, and circular wait. Deadlocks can be represented using a resource allocation graph - a directed graph with vertices for processes and resources. A cycle in the graph indicates a deadlock situation. Methods for handling deadlocks include prevention, avoidance, and recovery.

Uploaded by

RAZEB PATHAN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Unit III CAL 817 Operating System

Unit 3 covers deadlocks in operating systems. It defines deadlocks as a situation where processes are blocked because each holds a resource waited by another. Four necessary conditions for deadlocks are discussed: mutual exclusion, hold and wait, no preemption, and circular wait. Deadlocks can be represented using a resource allocation graph - a directed graph with vertices for processes and resources. A cycle in the graph indicates a deadlock situation. Methods for handling deadlocks include prevention, avoidance, and recovery.

Uploaded by

RAZEB PATHAN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Unit III

Subject: Operating System


Subject Code : CAL 817

Department of Computer Application

Dr. Amit Sharma


Associate Professor
School of Computer Application
CSL-628 Operating System
4 credits (3-1-0)
Unit-1:

Introduction to the Operating System (OS), Types of OS: Batch System, Time Sharing System, Real
Time System. Multi Programming, Distributed System, Functions and Services of OS.

Unit-2:

Process Management: Process Concept, Process State, Process Control Block, Process Scheduling, CPU
Scheduling - CPU Scheduling, Scheduling Criteria, Scheduling Algorithms, Preemptive & Non
Preemptive Scheduling.

Unit-3:

Deadlocks-System model, Characterization, Deadlock Prevention, Deadlock Avoidance and Detection,


Recovery from deadlock.

Unit-4:

Memory Management: Logical Address, Physical Address Contiguous Allocation, External and Internal
Fragmentation

Virtual Memory: Demand paging, page replacement, allocation of frames, thrasing.

Unit-5:

Information Management: File Concept, Access Methods, Directory Structure. Device Management:
Disk Structure, Disk Scheduling Algorithms.

Text books:

1. Silbershatz and Galvin," Operating System Concept", Addition We seley,

Reference books:

1. Tannenbaum,"Operating System Concept", Addition Weseley, 2002.


Unit-3:

Deadlocks-System model, Characterization, Deadlock Prevention, Deadlock Avoidance and Detection,


Recovery from deadlock.

Introduction

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 wait
state. It may happen that waiting processes will never again change state, because the resources they
have requested are held by other waiting processes. This situation is called deadlock.
If a process requests an instance of a resource type, the allocation of any instance of the type will
satisfy the request. If it will not, then the instances are not identical, and the resource type classes have
not been defined properly.

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.
Under the normal mode of operation, a process may utilize a resource in only the following sequence:

1. Request: If the request cannot be granted immediately, then the requesting process must wait until
itcan acquire the resource.
2. Use: The process can operate on the resource.
3. Release: The process releases the resource

Deadlocks-System model

3.1 Deadlocks
• Deadlock is a situation where a set of processes are blocked because each process is

→ holding a resource and


→ waiting for another resource held by some other process.

• Real life example:

When 2 trains are coming toward each other on same track and there is only one track, none of the trains
can move once they are in front of each other.
• Similar situation occurs in operating systems when there are two or more processes hold some

resources and wait for resources held by other(s).


• Here is an example of a situation where deadlock can occur (Figure 3.1).

Figure 3.1 Deadlock Situation

3.2 System Model


• A system consist of finite number of resources. (For ex: memory, printers, CPUs).
• These resources are distributed among number of processes.
• A process must

→ request a resource before using it and


→ release the resource after using it.

• The process can request any number of resources to carry out a given task.
• The total number of resource requested must not exceed the total number of resources available.
• In normal operation, a process must perform following tasks in sequence:

1) Request
t cannot be granted immediately (for ex: the resource is being used by another process),
then the requesting-process must wait for acquiring the resource.
2) Use
process uses the resource.

3) Release

elease( ).

• A set of processes is deadlocked when every process in the set is waiting for a resource that is currently
allocated to another process in the set.
• Deadlock may involve different types of resources.
• As shown in figure 3.2,

Figure 3.2

Both processes P1 & P2 need resources to continue execution.


P1 requires additional resource R1 and is in possession of resource R2.
P2 requires additional resource R2 and is in possession of R1.
• Thus, neither process can continue.
• Multithread programs are good candidates for deadlock because Figure 3.2 they compete for shared
resources.
3.3 Deadlock Characterization
� In a deadlock, processes never finish executing, and system resources are tied up, preventing other
jobs from starting.
3.3.1 Necessary Conditions
� There are four conditions that are necessary to achieve deadlock:
1) Mutual Exclusion
� At least one resource must be held in a non-sharable mode.
� If any other process requests this resource, then the requesting-process must wait for the resource to be
released.
2) Hold and Wait
� A process must be simultaneously
��holding at least one resource and
��waiting to acquire additional resources held by the other process.
3) No Preemption
� Once a process is holding a resource ( i.e. once its request has been granted ), then that resource cannot
be taken away from that process until the process voluntarily releases it.
4) Circular Wait
� A set of processes { P0, P1, P2, . . ., PN } must exist such that P0 is waiting for a resource that is held
by P1
P1 is waiting for a resource that is held by P2, and so on

Objectives

After going through this unit, you will be able to:


□ To develop a description of deadlocks, which prevent sets of concurrent processes from
completingtheir tasks.
□ To present number of different methods for preventing or avoiding deadlocks in a computer system.

Deadlock Characterization
In deadlock, processes never finish executing and system resources are tied up, preventing other
jobsfrom ever starting.

Necessary Conditions
A deadlock situation can arise if the following four
conditionshold simultaneously in a system:
1. Mutual exclusion: At least one resource must be held in a non-sharable 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.
2. Hold and wait : There must exist a process that is holding at least one resource and is waiting to
acquire additional resources that are currently being held by other processes.
3. No preemption : Resources cannot be preempted; that is, a resource can be released only
voluntarilyby the process holding it, after that process, has completed its task.
4. Circular wait: There must exist a set {P0, P1, ..., Pn } of waiting processes such that P0 is waiting
fora resource that is held by P1, P1 is waiting for a resource that is held by P2, …., Pn-1 is waiting
for a resource that is held by Pn, and Pn is waiting for a resource that is held by P0.

Resource-Allocation Graph

Deadlocks can be described more precisely in terms of a directed graph called a system resource-
allocation graph. The set of vertices V is 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, …, R1}, the set
consisting ofall resource types in the system.
A directed edge from process Pi to resource type Rj is denoted by Pi → Rj, it signifies that process Pi
requested an instance of resource type Rj and is currently waiting for that resource. A directed edge
from resource type Rj toprocess Pi is denoted by Rj_ Pi it signifies that an instance of resource type Rj
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.
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,
and asa result the assignment edge is deleted.
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, on the other hand, the graph contains the cycle, then a
deadlock must exist.
If each resource type has several instances, then a cycle implies that a deadlock has occurred. If the
cycle involves only a set of resources types, each of which has only a single instance, then a deadlock
has occurred. Each process involved in the cycle is deadlocked. In this case, a cycle in the graph is
both a necessary and a sufficient condition for the existence of deadlock.
A set of vertices V and a set of edges E.

V is partitioned into two types:


 P = {P1, P2, …, Pn}, the set consisting of all the processes in the system.
 R = {R1, R2, …, Rm}, the set consisting of all resource types in the system.
□ request edge – directed edge P1 → Rj
□ assignment edge – directed edge Rj → Pi
Fig. Resource Allocation Graph

If each resource type has several instance, then a cycle does not necessarily imply that a deadlock
incurred. In this case, a cycle in the graph is a necessary but not a sufficient condition for the existence
of deadlock.

Suppose that process P3requests an instance of resource type R2 Since no resource instance is
currently available, a request edge P3 cycles exist in the system:

Fig. 1 Resource Allocation Graph with Deadlock


Processes P1, P2, and P3 are deadlocked. Process P2 is waiting for the resource R3, which is held by
process P3. Process P3, on the other hand, is waiting for either process P1 or process P2 to release
resource R2. In addition, process PI is waiting for process P2 to release resource R1.

METHOD FOR HANDLING DEADLOCK /DETECTION

There are three different methods for dealing with the deadlock problem:
•We can use a protocol to ensure that the system will never enter a deadlock state.
•We can allow the system to enter a deadlock state and then recover.
•We can ignore the problem all together, and pretend that deadlocks never occur in the system.
Thissolution is the one used by most operating systems, including UNIX.

Deadlock avoidance, on the other hand, 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, we can decide for each request whether or not the process should wait.
Each request requires that the system consider the resources currently available, the resources currently
allocated to each process, and the future requests and releases of each process, to decide whether the
current request can be satisfied or must be delayed. If a system does not employ either a deadlock-
prevention or a deadlock avoidance algorithm, then a deadlock situation may occur If a system does not
ensure that a deadlock will never occur, and also does not provide a mechanism for deadlock detection
and recovery, then we may arrive at a situation where the system is in a deadlock state yet has no way
of recognizing what has happened.

DEADLOCK PREVENTION

For a deadlock to occur, each of the four necessary-conditions must hold. By ensuring that at least on
onethese conditions cannot hold, we can prevent the occurrence of a deadlock.

Mutual Exclusion
The mutual-exclusion condition must hold for non-sharable resources. For example, a printer cannot be
simultaneously shared by several processes. Sharable resources, on the other hand, do not require
mutually exclusive access, and thus cannot be involved in a deadlock.

Hold and Wait


1. When whenever a process requests a resource, it does not hold any other resources. One
protocol thatbe used requires each process to request and be allocated all its resources before it
begins execution.
2. An alternative protocol allows a process to request resources only when the process 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 here are two main
disadvantages to these protocols.

First, resource utilization may be low, since many of the resources may be allocated but unused
for a long period. In the example given, for instance, we can release the tape drive and disk file,
and then again request the disk file and printer, only if we can be sure that our data will remain on
the disk file. If we cannot be assured that they will, then we must request all resources at the
beginning for both protocols. Second, starvation is possible.

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 preempted. That is this resources are
implicitly released. The 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.

Circular Wait
Circular-wait condition never holds is to impose a total ordering of all resource types, and to require
that each process requests resources in an increasing order of enumeration.
Let R = {R1, R2, ..., Rn} 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. Formally, we define a one-to-one function F: R _ N, where N is the set of natural
numbers.

DEADLOCK AVOIDANCE

Prevent deadlocks requests can be made. The restraints ensure that at least one of the necessary
conditions for deadlock cannot occur, and, hence, that deadlocks cannot hold. Possible side effects of
preventing deadlocks by this, melted, however, are Tow device utilization and reduced system
throughput.
An alternative method for avoiding deadlocks is to require additional information about how
resources are to be requested. For example, in a system with one tape drive and one printer, we might
be told that process P will request first the tape drive, and later the printer, before releasing both
resources. Process Q on the other hand, will request first the printer, and then the tape drive. With this
knowledge of the complete sequence of requests and releases for each process we can decide for each
request whether or not the process should wait.

A deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that


there can never be a circular wait condition. The resource allocation state is defined by the number of
available and allocated resources, and the maximum demands of the processes.

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 <P1, P2, .. Pn> is a safe sequence for the current allocation state if,
for each Pi the resources that Pj can still request can be satisfied by the currently available resources
plus the resources held by all the Pj, with j < i. In this situation, if the resources that process Pi needs
are not immediately available, then Pi can wait until all Pj have finished. When they have finished, Pi
can obtain all of its needed resources, complete its designated task return its allocated resources, and
terminate. When Pi terminates, Pi + 1 can obtain its needed resources, and so on.

Fig. Safe, Unsafe & Deadlock State

If no such sequence exists, then the system state is said to be unsafe.

Resource-Allocation Graph Algorithm

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.

Banker's Algorithm

The resource-allocation graph algorithm is not applicable to a resource-allocation system with multiple
instances of each resource type. The deadlock-avoidance algorithm that we describe next is applicable
to such a system, but is less efficient than the resource-allocation graph scheme. This algorithm is
commonly known as the banker's algorithm.

DEADLOCK DETECTION

If a system does not employ either a deadlock-prevention or a deadlock avoidance algorithm, then a
deadlock situation may occur.

• An algorithm that examines the state of the system to determine whether a deadlock has occurred.
• An algorithm to recover from the deadlock.

Single Instance of Each Resource Type


If all resources have only a single instance, then we can define a deadlock detection algorithm that
uses a variant of the resource-allocation graph, called a wait-for graph. We obtain this graph from the
resource- allocation graph by removing the nodes of type resource and collapsing the appropriate
edges.

Several Instances of a Resource Type


The wait-for graph scheme is not applicable to a resource allocation system with multiple instances
ofeach resource type. The algorithm used are :
• Available: A vector of length m indicates the number of available resources of each type.
• Allocation: An n x m matrix defines the number of resources of each type currently allocated to
eachprocess.
• Request: An n x m matrix indicates the current request of each process. If Request [i, j] = k,
thenprocess P, is requesting k more instances of resource type Rj.

Detection-Algorithm Usage

If deadlocks occur frequently, then the detection algorithm should be invoked frequently.
Resourcesallocated to deadlocked processes will be idle until the deadlock can be broken.

RECOVERY FROM DEADLOCK

When a detection algorithm determines that a deadlock exists, several alternatives exist. One
possibilityis to inform the operator that a deadlock has spurred, and to let the operator deal with
the deadlockmanually. The other possibility is to let the system recover from the deadlock
automatically. There aretwo options for breaking a deadlock. One solution is simply to abort one or
more processes to break thecircular wait. The second option is to preempt some resources from one or
more of the deadlocked processes.
Process Termination

To eliminate deadlocks by aborting a process, we use one of two methods. In both methods, the system
reclaims all resources allocated to the terminated processes.
• Abort all deadlocked processes: This method clearly will break the dead – lock cycle, but at a great
expense, since these processes may have computed for a long time, and the results of these partial
computations must be discarded, and probably must be recomputed.

• Abort one process at a time until the deadlock cycle is eliminated: This method incurs
considerable overhead, since after each process is aborted a deadlock-detection algorithm must be
invoked to determine whether a processes are still deadlocked.
Resource Preemption
To eliminate deadlocks using resource preemption, we successively preempt some resources from
processes and give these resources to other processes until he deadlock cycle is broken.

The three issues are considered to recover from deadlock

1. Selecting a victim
2. Rollback
3. Starvation

SUMMARY
A deadlocked state occurs when two or more processes are waiting indefinitely for an event that can be
caused only one of the waiting processes. There are three principal methods for dealing with deadlocks:
☐ Use some protocol to prevent or avoid deadlocks, entering that the system will never enter a
deadlocked state.
☐ Allow the system to enter a deadlocked state, detect it, and then recover.
☐ Ignore the problem altogether and pretend that deadlocks never occur in the system.
Deadlock prevention is a set of methods for ensuring that at least one of the necessary condition
cannot hold. Deadlock avoidance requires additional information about how resources are to be
requested. Deadlock avoidance algorithm dynamically examines the resource allocation state to ensure
that a circular wait condition can never exist. Deadlock occur only when some process makes a
request that cannot e granted immediately.

You might also like