Process Management-Deadlock
Process Management-Deadlock
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 in databases
Deadlocks in spooling
Deadlocks in a network
Case 1: Deadlocks on File Requests
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
processes: A
condition
resulting when
locking is not
used
•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
First Scenario
Scenario 2
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
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”
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