Operating systems
Deadlocks
1
Deadlocks
• What is a deadlock?
• 2 approaches:
• “safe”: prevent and avoid deadlocks
• “dangerous”: it enables a deadlock appearance,
then detect it and solve the problem
2
Deadlocks
Examples:
•
“It takes money to make money".
•
You cannot have a job without experience; you cannot have experience
without a job.
Deadlock causes: each process needs an output form other process.
This fact has the result of resource sharing (memory, equipment, connection
links, etc).
In a normal situation, the operation of resource allocation is done like this:
1. The resource is demanded.
2. The resource is used.
3. The resource is freed-up.
3
Example: crossing a bridge
• One direction only traffic.
• Each section of the bridge can be considered as a resource.
• If a deadlock occurs, it can be solved if one of the cars goes back (resource
preemption).
• It can appear the phenomenon of “starvation”.
See also the 5 philosophers problem:
(http://en.wikipedia.org/wiki/Dining_philosophers_problem)
4
Deadlock characteristics
Necessary conditions
All 4 conditions must happen simultaneously in order to have a deadlock:
Mutual exclusion
One or more resources must be owned by a process in an exclusive way.
“Hold and Wait”
A process owns a resource while is waiting for another resource to be freed-up.
No preemption
We have only the possibility that a resource is freed-up voluntarily – nothing else can
force to freed-up the resource.
Circular waiting
The process A waits for process B that waits for process C .... that waits for A.
5
Resource allocation graph
A graph can be used to visually determine the appearance (or the possibility
of appearance) of a deadlock.
G = ( N, L ) nodes (vertices) and links (edges)
N
The nodes are processes= {P1, P2, P3, ...} and different types of
resources {R1, R2, ...}
M The links are ( Pi, Rj ) or ( Ri, Pj )
An arrow from one process to a resource means that the process needs that
resource.
An arrow from a resource to a process means that an instance of that
resource has been allocated to that process.
In the drawing, the process is a circle, the resource is a square; the dots are
representing the number of instances for that specific resource.
6
Resource allocation graph
• If the graph has no cycles, no process is blocked.
• If there is a cycle, then:
a) If the types of resources have more instances, then it MIGHT exist
a deadlock.
b) If every type of resource has a single instance, a deadlock will
appear.
R3 assigned to P3
Resource allocation graph
P2 requests R3
7
General strategies for approaching deadlocks
In general, there are three methods:
- Ignoring deadlocks.
- Assuring the fact that a deadlock can never appear:
Prevention
Preventing the appearance of one of the four conditions (Mutual
exclusion,“Hold and Wait”, No preemption, Circular
waiting).
Avoidance
The deadlock conditions are enabled but the appearance cycles
are computed and the dangerous operations are stopped.
- Enabling the deadlock appearance. In this case there are used the following:
Detection
The moment when a deadlock has appeared is known.
Recovery
The resources are “recovered”.
8
Preventing deadlocks
None of the four conditions cannot appear
Mutual exclusion:
a) It is automated for printers and other non-sharable devices
b) Entities like read only files don’t need mutual exclusion, because they cannot
lead to deadlocks.
“Hold and wait”:
a) All the resources are gathered before the execution
b) One particular resource can be demanded only when nobody holds it. In a
sequence of resources the first one is demanded first.
c) In this case “starvation” may occur.
9
Preventing deadlocks
No preemption:
a) Any assigned resource is freed-up if the process cannot obtain another
resource.
b) If a resource is assigned to a process involved in a waiting state for another
resource, this is “stealed”.
Circular waiting:
a) The resources are numbered and their demand is done in ascending order
b) Each of the preventing methods can lead to a low resource utilization. For this
reason, prevention is not necessarily the best method to implement.
c) Prevention is the easiest method to implement
10
Deadlock avoidance
If we can know from he beginning the way of using resources it is possible to determine if
an “unsafe” state can appear.
Possible states:
Deadlock
Nothing “constructive” can be done furthermore
Unsafe state
A state for which a deadlock may occur
Safe state
A state is safe if there is a sequence of processes such as there are
enough resources for the first process to finish and each process is
finishing and freeing the necessary resources for the next process to
finish.
A simple rule: “if a resource demand allocation may lead to an unsafe state, don’t satisfy
that demand”.
Obs. All the deadlocks are unsafe, but not all unsafe states are deadlocks
11
Process concurrency in Unix/Linux
UNIX offers many mechanisms for communication and synchronization between
processes:
• Pipes
• Messages
• Shared memory
• Semaphores
• Signals
Pipes – a pipe represents a circular buffer that enables the communication between
two processes using the “producer-consumer” pattern – a FIFO queue where a
process sends a message to another process. When a pipe is created, it has a fixed
dimension in bytes. When a process tries to write in the pipe the writing demand is
immediately executed (if there is enough space, otherwise the process is blocked). In
the same way, a process that needs a reading operation is blocked if it tries to read
more octets than exist in the pipe – on the contrary, the reading demand is
immediately executed.
The OS applies mutual exclusion in this case – only a single process may access the
pipe at one moment in time.
12
Process concurrency in Unix/Linux
Messages – A message is a block of text of a certain type. The UNIX
OS uses the system calls msgsnd and msgrcv to transfer messages.
Each process has a message queue that is working as a mailbox.
Shared memory – represents (in Unix) the quickest form of
communication between processes. There is a common virtual
memory block shared between multiple processes. The processes
are reading or writing in the common memory space using the
same instructions used for reading/writing from/to the virtual
memory. The permissions are read-only or read-write for each
process. The mutual exclusion is assured by the processes that are
using the shared memory.
13
Process concurrency in Unix/Linux
Semaphores – represents a generalization of the system primitives wait
and signal. The kernel is doing all the operations one by one
and no process cannot access the semaphore till all
operations are fulfilled.
Signals – a signal is a software mechanism that informs a process about the
appearance of an asynchronous event. A signal is similar with
a hardware interrupt but it doesn’t implies priorities (all
signals are considered “equal”). The signals that appear in the
same time are presented to a process one by one, without an
order based on priorities.
The processes can send signals to other processes or the signals are sent by
the kernel. A signal is delivered by updating a field in the
(receiving) process table.
14
Read InformIT articles
On InformIT you may find interesting articles from the IT field
http://www.informit.com/articles/article.aspx?p=24972
http://www.informit.com/articles/article.aspx?p=1339466&seqNum=2
15
© Copyright 2026 Paperzz