Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

OPERATING SYSTEMS

UNIT III
.
Deadlocks- Objectives
• To develop a description of deadlocks, which
prevent sets of concurrent processes from
completing their tasks
• To present a number of different methods for
preventing or avoiding deadlocks in a
computer system
Deadlock Example
Deadlocks
• A set of processes is in a Deadlock state when
every process in the set is waiting for an event
that can be caused only by another process in
the set. The events with which we are mainly
concerned here are resource acquisition and
resource release.
System Model
• System consists of resources
• Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
• Each resource type Ri has Wi instances.
• Each process utilizes a resource as follows:
– request
– use
– release
System Model
• A system consists of a finite number of resources to be
distributed among a number of competing processes.
• Resources are categorized into two types: Physical resources
and Logical resources
• Physical resources: Printers, Tape drives, DVD drives, memory
space and CPU cycles
• Logical resources: Semaphores, Mutex locks and files.
System Model
• A process may utilize a resource in the following sequence
under normal mode of operation:
• Request: The process requests the resource. If the resource is
being used by another process then the request cannot be
granted immediately then the requesting process must wait
until it can acquire the resource.
• Use: The process can operate on the resource.
• Example: If the resource is a printer, the process can print on
the printer.
• Release: The process releases the resource.
System Model
System calls for requesting and releasing resources
• Device System calls: request( ) and release( )
• Semaphore System calls: wait( ), signal( )
• Mutex locks: acquire( ), release( ).
• Memory System Calls: allocate( ) and free( )
• File System calls: open( ), close( ).
System Model
• A System Table maintains the status of each resource
whether the resource is free or allocated.
• For each resource that is allocated, the table also records
the process to which it is allocated.
• If a process requests a resource that is currently
allocated to another process, it can be added to a queue
of processes waiting for this resource.
Deadlock Characterization
Deadlock can arise if four conditions hold
simultaneously:

• Mutual exclusion: only one process at a time can use a resource


• Hold and wait: a process holding at least one resource is waiting
to acquire additional resources held by other processes
• No preemption: a resource can be released only voluntarily by
the process holding it, after that process has completed its task
• Circular wait: there exists a set {P0, P1, …, Pn} of waiting processes
such that P0 is waiting for a 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
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 Pi  Rj

• assignment edge – directed edge Rj  Pi


Resource-Allocation Graph (Cont.)
• Process

• Resource Type with 4 instances

Pi
• Pi requests instance of Rj

Pi
• Pi is holding an instance of Rj
Example of a Resource Allocation Graph
Basic Facts
• If graph contains no cycles  no deadlock
• If graph contains a cycle 
– If only one instance per resource type, then deadlock exist
– If several instances per resource type, then possibility of deadlock
Example of a Resource Allocation Graph
• Process set P= {P1, P2, P3}.
• Resources set R= {R1, R2,
R3, R4}.
• Edge set E= E = {P1 → R1,
P2 → R3, R1 → P2, R2 →
P2, R2 → P1, R3 → P3}.
• Resource type R1 and R3
has only one instance and
R2 has two instances and
R4 has three instances.
Example of a Resource Allocation Graph
• The Resource Allocation Graph depicts that:
• R2 → P1, P1 → R1: P1 is holding an instance of resource
type R2 and is waiting for an instance of R1.
• R1 → P2, R2 → P2, P2 → R3: Process P2 is holding an
instance of R1 and an instance of R2 and is waiting for an
instance of R3.
• R3 → P3: Process P3 is holding an instance of R3.
• The above Resource allocation graph does not contain
any cycle then there is no process in the system is
deadlocked.
Resource Allocation Graph With a Cycle

Is there a deadlock?
Resource Allocation Graph with a Cycle and No Deadlock

• The graph has a cycle:


– P1 → R1 → P3 → R2 → P1.
• This cycle does not lead to deadlock,
because the process P4 and P2 is not
waiting for any resource.
• Process P4 may release its instance
of resource type R2. That resource
can then be allocated to P3,
breaking the cycle.
Resource Allocation Graph With a Cycle

Is there a deadlock?
Resource Allocation Graph with a Cycle and Deadlock

• Consider the above graph, with processes


and Resources and have some edges:
– P1 → R1 → P2 → R3 → P3 → R2 → P1
– P2 → R3 → P3 → R2 → P2
• Process P2 is waiting for the resource R3,
which is held by process P3.
• Process P3 is waiting for either process P1
or process P2 to release resource R2.
• Process P1 is waiting for process P2 to
release resource R1.
• Hence the Processes P1, P2 and P3 are
deadlocked.
Methods for Handling Deadlocks
• Ensure that the system will never enter a deadlock state:
– Deadlock prevention
– Deadlock avoidance
• Allow the system to enter a deadlock state and then recover
• Ignore the problem and pretend that deadlocks never occur in
the system ( used by most operating systems, including UNIX)

You might also like