ICS 431 Ch8 Deadlocks
ICS 431 Ch8 Deadlocks
Weeks 8-9
Deadlock Handling
Dr. Tarek Helmy El-Basuny
Dr. Tarek Helmy, ICS-KFUPM
Reminder: Ch. 6 Process Synchronization
• To practice collaborative learning as we agreed, to foster self learning skill, and to help us
progress in the course.
• You need to study Ch. 6 “Process Synchronization” by yourself and it will be included in the
Major Exam II.
• Office hours can be used to answer any inquires about this chapter.
• Your objectives out of reading this chapter is to know:
– Why synchronization? Or the necessity of synchronization by the OS.
– How do processes work with resources that must be shared between them?
– What is a critical section?
– Dangers of handling the critical section without synchronization.
– How to ensure that only one process can get access to the critical section?
– What is atomic operation? It executes without interruptions, all or none.
– Different algorithms to synchronize processes entering into the critical section.
– Synchronization tools.
– Semaphores, and types of Semaphores.
– Incorrect usages of Semaphores.
– Monitors.
– Classical problems of synchronization.
– Synchronization methods in different OSs.
• Evaluating synchronization algorithms of handling a critical section where every algorithm
should allow Mutual Exclusion, Progress and bounded waiting.
Dr. Tarek Helmy, ICS-KFUPM 2
Deadlocks
– Deadlock Prevention
– Deadlock Avoidance
– Deadlock Detection
– Deadlock Recovery
• Reusable resources
A reusable resource is the one that can be safely used by the process at a
time and then reused by another process at different time.
The process requests the resource, acquires it, and then releases it.
o Examples: Processors, I/O channels, main and auxiliary memory blocks,
I/O devices, files (i.e. data bases), semaphores, etc…
• Consumable resources
A consumable resource is the one that will be created, used and destroyed.
It can not be reused by another process.
The number of consumable resources is usually unlimited.
o Examples: Interrupts, Signals, Messages, Information in I/O buffers, etc.
• Preemptable resources.
Can be taken away from a process with no ill effects (e.g. Memory, CPU,.).
• Nonpreemptable resources
Will cause the process to fail if it is taken away (e.g. CD, …)
• Deadlock is the permanent blocking of a set of processes that either compete
for system resources or communicate with each other.
P2 P1
Waits For Resource 2 Held By Disk
• 200K bytes of memory space is available for allocation, and the following
sequence of events occur.
P1 P2
... ...
Requests 80 Kbytes; Requests 70 Kbytes;
... ...
Requests 60 Kbytes; Requests 80 Kbytes;
• Suppose a server process and a client process run on two different machines.
– The server first sends an initialization message to the client “i.e. I am ready”, and
then waits for a request from the client.
– The client first waits for the initialization message, and then makes requests.
– What happen if the initialization message is lost?
– i.e. If the server is so fast so that the server’s initialization message arrives at the
client while it is still in its booting up stage, then the initialization message will be
lost.
– Thus, the client is waiting for the initialization message, whereas the server is
waiting for a request message from the client.
– A deadlock now occurs.
– If the server and client machines are of the same speed, and started
simultaneously, then the system runs smoothly without deadlock. Also, if both of
the server or the client periodically initiates messages then the system runs
smoothly as well. Network
MSG
Acknowledge
P1 MSG P2
Client Acknowledge Server
MSG
Acknowledge
(Lost)
P1 Read records at
I/O Disk
cylinder 20
Channel control
P2 Write to file at unit
cylinder 310
• E can be either:
– Request edge – directed edge P1 Rj
– Assignment edge – directed edge Rj Pi
• An arrow from the process to resource indicates the process is requesting
the resource.
• An arrow from a resource to a process means an instance of the resource
has been allocated/assigned to the process.
• A Process
• Pi requests instance of Rj
Pi Rj
• Pi is holding an instance of Rj
Pi Rj
• Dots represent number of instances of a resource type.
R3 Assigned to P3
Resource allocation graph:
P= {P1, P2, P3}
R={R1, R2, R3, R4}
E={P1→ R1, P2 → R3, R1→ P2, R2 →
P2, R2 → P1, R3 → P3}
Recourse instances:
One instance of resource type R1
Two instances of resource type R2
One instance of resource type R3 P2 Requests R3
Three instances of resource type R4
RAG with a deadlock: Two cycles: RAG with a cycle but no deadlock:
3. If we can not prevent its causes or avoid its occurrence. This requires using:
– Detection strategies: To know that deadlock has occurred, and which
processes are in deadlock.
– Recovery strategies: Abort a process or preempt some resources (using
some polices) when deadlock is detected, to recover it.
Conclusion:
Prevention not possible, since some resources are naturally
non-sharable.
Automatically holds for printers, CD-RW and other non-
sharable devices.
Solutions:
Make resources concurrently sharable wherever possible e.g.
read-only file access (don't need mutual exclusion and aren’t
easily to deadlock).
Better to have a spooler process to which print jobs are sent
(completed output file must be printed first).
OS avoids deadlock
Fig. 1
Fig. 2
1. Suppose that P2 requests R2, Fig. 1. because R2 has two
instances, we can allocate them to P1 and P2 even there will be a
cycle and indicates of safe state, Fig. 2.
2. That means the RAG is not applicable in case of having
recourses with many instances.
3. There should be another algorithm fro that situation.
• Examine each resource request and determine whether or not granting the
request can lead to deadlock.
• Define a set of vectors and matrices that characterize the current state of all
resources and processes.
R1 R2 R3 ... Rr
Maximum claim matrix P1 n1,1 n1,2 n1,3 ... n1,r
Maxij = the maximum number of units of
P2 n2,1 n2,2
resource j that the process i will ever require
P3 n3,1
...
simultaneously.
...
...
Available vector Pp np,1 ... np,r
Avaij = the number of units of resource j that
are available.
<n1, n2, n3, ..., nr>
Dr. Tarek Helmy, ICS-KFUPM 37
Data Structures for the Banker Algorithm
This algorithm for finding out whether or not the system is in safe state
1. Let Work and Finish be vectors of length m and n, respectively.
Initialize:
Work = Available
Finish [i] = false if there is a process i (i=1, 2, …, n) not yet finish.
2. Find a process i such that both:
(a) Finish [i] = false (not yet finish)
(b) Needi Work (needs less recourses than the available)
If no such i exists, go to step 4. (all processes have finished)
3. Work := Work + Allocationi (update the work and available)
Finish[i] = true
go to step 2.
4. If Finish [i] = true for all i, then the system is in a safe state.
The algorithm may require an order of m*n2 operation to decide.
• Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2>
satisfies safety requirement.
• Can request for (3,3,0) by P4 be granted?
• Can request for (0,2,0) by P0 be granted?
• The WFG algorithm is not applicable to systems with multiple instances of each
resource type.
• Another algorithm is applicable to such kind of systems, where the following data
structures will be created:
• Allocation: An n x m matrix defines the number of resources of each type currently
allocated to each process.
• Request: An n x m matrix indicates the current request of each process. If Request
[i, j] = k, then process Pi is requesting k more instances of resource type Rj.
• Available: A vector of length m indicates the number of available instances of each
resources type.
• When, and how often, to invoke the detection algorithm depends on:
– How often a deadlock is likely to occur?
– How many processes will be affected by the deadlock when it happens?
• If detection algorithm is invoked randomly, there may be many cycles in the
WFG and so we would not be able to tell which of the many deadlocked
processes “caused” the deadlock.
• In the extreme, we could invoke the detection algorithm every time a request
for allocation cannot be granted. But this makes an overhead computing.
• The detection algorithm can be invoked whenever the CPU utilization drops
below 40 % or once per hour.
• If deadlock has been detected by the OS, the OS should be able to recover it.
• Terminate all deadlocked processes. It is too expensive as these processes
may have computed for a long time and later will be repeated, if the process
was in the middle of updating or printing a file, terminating it makes an errors.
• Abort one process at a time until the deadlock cycle is eliminated. This means
the detection algorithm should be invoked many times to check and this
makes overhead computing.
• Partial termination means there should be a mechanism to select the process
to be terminated, like the CPU scheduling.
• In which order should we choose to abort?
– Priority of the process.
– How long process has computed, and how much longer to completion.
– Resources the process has used.
– Resources process needs to complete.
– Is the process interactive or batch?
– How many processes will need to be terminated.
• Starvation: Same process may always be picked as victim, we should include
number of rollback as a cost factor.
• Preempt some resources from the processes and give them to other
processes until the deadlock cycle is broken. Three issues need to
be addressed.
– Selecting a victim: we must determine the order of preemption to
minimize the cost. i.e. priority, age of the process with the
resource.
Parallel Processing
CPU
P1
P2 CPU
P3 CPU
time
Concurrent Processing
P1
P2 CPU
P3
time
Thank you
Any Questions?