0% found this document useful (0 votes)
18 views35 pages

Process Management-Deadlock

Deadlock

Uploaded by

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

Process Management-Deadlock

Deadlock

Uploaded by

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

Process

Management
Deadlock
Deadlock
 Occurs when a process or thread enters a
waiting state because a requested system
resource is held by another waiting process,
which in turn is waiting for another resource
held by another waiting process.
 It is a situation where a set of processes
are blocked because each process is
holding a resource and waiting for another
resource acquired by some other process.
 Affects more than one job, hence more
serious than starvation
Resources
• Examples of computer resources
• printers
• tape drives
• tables
• Processes need access to resources in reasonable order
• Suppose a process holds resource A and requests resource B
• at same time another process holds B and requests A
• both are blocked and remain so
Resources
• Deadlocks occur when …
• processes are granted exclusive access to devices
• we refer to these devices generally as resources
• Preemptable resources
• can be taken away from a process with no ill effects
• Nonpreemptable resources
• will cause the process to fail if taken away

4
Resources (2)
• Sequence of events required to use a
resource
1. request the resource
2. use the resource
3. release the resource
• Must wait if request is denied
• requesting process may be blocked
• may fail with error code

5
Different Cases of Deadlock:

 Deadlocks on file requests

 Deadlocks in databases

 Deadlocks in dedicated device

 Deadlocks in multiple device

 Deadlocks in spooling

 Deadlocks in disk sharing

 Deadlocks in a network
Case 1: Deadlocks on File Requests

• Occurs if jobs are allowed to request and


hold files for the duration of their execution

 Example: Refer to image below


- P1 has access to F1 but requires F2 also
- P2 has access to F2 but requires F1 also
- Deadlock remains until a program is
withdrawn or forcibly removed and its file is
released
- Any other programs that require F1 or F2
are put on hold as long as this situation
continues
A case of deadlock on file requests
Case 2:
Deadlocks in Databases
• Occurs if two processes access and lock records in a
database

 Example: Two processes (P1 and P2), each of which

needs to update two records (R1 and R2)

 Following sequence leads to a deadlock:

- P1 accesses R1 and locks it

- P2 accesses R2 and locks it

- P1 requests R2, which is locked by P2

- P2 requests R1, which is locked by P1


 Occurs if two processes access and lock records in a database
 Example: Two processes (P1 and P2), each of which needs to update
two records (R1 and R2)
 Following sequence leads to a deadlock:
- P1 accesses R1 and locks it
- P2 accesses R2 and locks it
- P1 requests R2, which is locked by P2
- P2 requests R1, which is locked by P1

 Locking: A technique through which the user locks out all other users
while working with the database. Can be done at three different
levels:
- Entire database for the duration of the request
- A subsection of the database
- Individual record until the request is completed
 Causes incorrect final version of data

Race between  Depends on order in which each process executes it

processes: A
condition
resulting when
locking is not
used

A case of “race between processes”


Case 3: Deadlocks in
Dedicated Device Allocation

 Occurs when there is a limited number of dedicated


devices

 Example: Each program (P1 and P2) needs two


tape drives (only available).

Soon the following sequence transpires:

- P1 requests tape drive 1 and gets it

- P2 requests tape drive 2 and gets it

- P1 requests tape drive 2 but is blocked

- P2 requests tape drive 1 but is blocked


Case 4: Deadlocks in Multiple Device Allocation

• Occurs when several processes


request, and hold on to, dedicated
devices while other processes act in a similar
manner
 Example: Three programs and three
dedicated devices: tape drive,
printer, and plotter
- P1 requests and gets the tape drive
- P2 requests and gets the printer
- P3 requests and gets the plotter
- P1 requests the printer but is blocked
Deadlock in multiple device allocation
- P2 requests the plotter but is blocked
- P3 requests the tape drive but is blocked
Case 5: Deadlocks in Spooling

 Virtual device: Sharable device—e.g., a printer transformed by installing a


high-speed device, a disk, between it and the CPU
 Spooling: Disk accepts output from several users and acts as a temporary
storage area for all output until printer is ready to accept it
 Deadlock in spooling: If printer needs all of a job's output before it will begin
printing, but spooling system fills available disk space with only partially
completed output
Case 6: Deadlocks in a Network

•Deadlock in a network

 Occurs when the network doesn’t have protocols to control the flow of
messages through the network
 Example: Refer to the image
- Consider seven computers on a network, each on different nodes
- Direction of the arrows indicates the flow of messages
- Deadlock occurs if all the available buffer space fills
Case 7: Deadlocks in Disk Sharing

•Deadlock in disk sharing

 Occurs when competing processes send conflicting commands to access a


disk
 Example: Refer to the image below
- Two processes are each waiting for an I/O request to be filled:
o One at cylinder 20 and one at cylinder 310
- Neither can be satisfied because the device puts each request on hold
when it tries to fulfill the other
Conditions for
Deadlock
Deadlock preceded by simultaneous occurrence
of the following four conditions:

Mutual exclusion - Act of allowing Resource holding - Act of holding a


only one process to have access to a resource and not releasing it; waiting
dedicated resource. At least one for the other job to retreat. A
resource must be held in a non- process must be simultaneously
sharable mode; If any other process holding at least one resource and
requests this resource, then that waiting for at least one resource that
process must wait for the resource is currently being held by some
to be released. other process.
- No pre-emption - Lack of temporary reallocation of resources. 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.
- Circular wait - Each process involved in impasse is waiting for
another to voluntarily release the resource so that at least one will
be able to continue. A set of processes { P0, P1, P2, . . ., PN } must
exist such that every P[ i ] is waiting for P[ ( i + 1 ) % ( N + 1 ) ]. (
Note that this condition implies the hold-and-wait condition, but it is
easier to deal with the conditions if the four are considered
separately. )

• Removal of only one condition can resolve the deadlock


Four Conditions for Deadlock
1. Mutual exclusion condition

each resource assigned to 1 process or is available
2. Hold and wait condition

process holding resources can request additional
3. No preemption condition

previously granted resources cannot forcibly taken away
4. Circular wait condition
• must be a circular chain of 2 or more processes
• each is waiting for resource held by next member of the chain
19
Modeling
Deadlocks
 Directed graphs:
- Processes represented by circles
- Resources represented by squares
- Solid arrow from a resource to a process means that process is
holding that resource
- Solid arrow from a process to a resource means that process is
waiting for that resource
- Direction of arrow indicates flow
- If there’s a cycle in the graph then there’s a deadlock involving the
processes and the resources in the cycle
Scenario 1

First scenario’s sequence of events is shown in the


directed graph as shown in the image below
The system will stay free of deadlocks if all resources are released before they’re requested by the next
process

First Scenario
Scenario 2

Second scenario’s sequence of events is shown in the


directed graph in the image below
Deadlock occurs because every process is waiting for a resource being held by another process, but
none will be released without operator intervention

Second scenario
Deadlock Modeling
Strategies for dealing with Deadlocks
1. just ignore the problem altogether
2. detection and recovery
3. dynamic avoidance
• careful resource allocation
4. prevention
• negating one of the four necessary conditions
24
Deadlock Modeling

How deadlock occurs 25


Deadlock Modeling

(o) (p) (q)

How deadlock can be avoided 26


Methods for
Handling
Deadlocks
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
Deadlock Prevention
 Restrain the ways request can be made
 Mutual Exclusion – not required for sharable resources (e.g., read-only
files); must hold for non-sharable resources
 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 allocated to it.
- Low resource utilization; starvation 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 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
 Circular Wait – impose a total ordering of all resource types, and
require that each process requests resources in an increasing order of
enumeration
Deadlock Avoidance
Deadlock can be avoided if system knows ahead of time the sequence of
requests associated with each of the active processes. Requires that the
system has some additional a priori information available.

 Simplest and most useful model requires that 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
 Dijkstra’s Bankers Algorithm (1965) regulates resources allocation to avoid
deadlock
- No customer granted loan exceeding bank’s total capital
- All customers given a maximum credit limit
- No customer allowed to borrow over the limit
- The sum of all loans won’t exceed bank’s total capital
Safe State
 When a process requests an available resource, system must decide if
immediate allocation leaves the system in a safe state
 System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL
the processes in the systems such that for each Pi, the resources that Pi
can still request can be satisfied by currently available resources +
resources held by all the Pj, with j < I
 That is:
- If Pi resource needs are not immediately available, then Pi can wait
until all Pj have finished
- When Pj is finished, Pi can obtain needed resources, execute,
return allocated resources, and terminate
- When Pi terminates, Pi +1 can obtain its needed resources, and so
on
Safe state: Bank still has enough money left after loans to satisfy the maximum
requests of C1, C2, or C3

The bank started with $10,000 and has remaining capital of $4,000 after these
loans
Unsafe state: Bank has not enough money left after loans to satisfy the
maximum requests of C1, C2, or C3

The bank has remaining capital of only $1,000 after these loans and
therefore is in an “unsafe state”

Basic Facts about deadlocks


 If a system is in safe state  no deadlocks
 If a system is in unsafe state  possibility of deadlock
 Avoidance  ensure that a system will never enter an unsafe state.
Starvation
 Job is prevented from execution as it’s kept waiting for resources that
never become available
- Result of conservative allocation of resources

Example:
- “The dining philosophers table” Dijkstra (1968)
To avoid starvation:
- Implement algorithm to track how long each job has been
waiting for resources (aging)
- Block new jobs until the starving jobs have been satisfied
Starvation
• Algorithm to allocate a resource
• may be to give to shortest job first
• Works great for multiple short jobs in a system
• May cause long job to be postponed indefinitely
• even though not blocked
• Solution:
• First-come, first-serve policy
35

You might also like