You are on page 1of 17

Computer Architecture

Processes Threads Interprocess Communication (IPC) CPU Scheduling Deadlocks

Computer Architecture

WS 06/07

Dr.-Ing. Stefan Freinatis

Deadlocks
Consider two processes requiring exclusive access to some shared resources (e.g. file, tape-drive, printer, CD-Writer). { request(resource1); request(resource2); ... release(resource1); release(resource2); }
Process 1

{ request(resource2); request(resource1); ... release(resource2); release(resource1); }


Process 2

Fictitious system call for requesting exclusive access to a resource. When access cannot be granted, the call blocks until the resource is available.
Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Deadlocks
{ request(resource1); request(resource2); ... release(resource1); release(resource2);
time

} {

Process 1

request(resource2); request(resource1); ... release(resource2); release(resource1); }


Process 2

When the two processes are executed sequentially (one after the other), no problem arises.

Computer Architecture

WS 06/07

Dr.-Ing. Stefan Freinatis

Deadlocks
{ request(resource1); request(resource2); ... release(resource1); release(resource2);
time

{ request(resource2);
blocked

Process 1

request(resource1); ... release(resource2); release(resource1); }


Process 2

When process 1 has acquired the resources before process 2 starts trying the same, no problem arises. Process 2 just has to wait.

Computer Architecture

WS 06/07

Dr.-Ing. Stefan Freinatis

Deadlocks
{ request(resource1); request(resource2);
blocked

{ request(resource2); request(resource1);
blocked

time

Process 1

Process 2

Occasionally, when both processes are carried out in parallel as depicted above, both their attempts to acquire the missing resource will cause the processes to block. Since each process holds a resource that the other one needs, and since each process cannot release its resource, both processes do wait forever (deadlock).

Computer Architecture

WS 06/07

Dr.-Ing. Stefan Freinatis

Deadlocks
A set of processes is deadlocked when each process in the set is waiting for an event that only another process in the set can cause. Waiting for an event:
Waiting for the availability of a resource Waiting for some input Waiting for a message (IPC) or a signal or any other type of event that a process is waiting for in order to continue

Computer Architecture

WS 06/07

Dr.-Ing. Stefan Freinatis

Deadlocks
Classical deadlock problem from the non-computer world

Yields to car at right

Yields to car at right

Yields to car at right

Yields to car at right

Every car is ought to give way to the car on the right. None will proceed.

Figure from lecture slides Computer Architecture WS 05/06 (Basermann / Jungmaier) Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Resources
Anything a process / thread needs to continue Exclusive access
Only one process at a time can use the resource (e.g. printer or writing to a shared file). Deadlocks Examples: I/O-devices like printer, tape, CD-ROM, files, but also internal resources such as process table, thread table, file allocation table or semaphores / mutexes.

Non-exclusive access
More than one process can use the resource at the same time (e.g. reading from a shared file)

Preemptable resources
The resource can (with some non-zero cost) be temporarily taken away from a process and given to another process (e.g. memory swapping).

Non-preemptable resources
The resource cannot be temporarily assigned to another process (e.g. printer, CD-Writer) without leading to garbage.
Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Deadlocks
The following four conditions must be present for a deadlock to occur.

Mutual Exclusion
Each resource is either currently assigned to exactly one process or is available.

Hold and Wait


A process currently holding a resource can request new resources.

Non-preemptable resources
Resources previously granted cannot be forcibly taken away from a process.

Circular Wait
There must be a circular chain of processes, each of which is waiting for a resource held by another process in the chain.

If one of these conditions is absent, no deadlock is possible


Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Deadlock Modeling
Resource allocation graphs
Process Resource Deadlocks

a) Holding a resource (Process A holds resource R) b) Requesting a resource (Process B requests resource S) c) Deadlock situation: Process D requests U which is held by process C. Process C requests T which is held by D. Figure from [Ta01 p.165]
Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Deadlock Modeling
A B C

Deadlocks

time

Figure from [Ta01 p.166]

Resource allocation order leading to a deadlock


Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Deadlock Modeling
Deadlocks

Example of resource allocation not resulting in a deadlock

time

Figure from [Ta01 p.166]

(o)

(p)

(q)

Computer Architecture

WS 06/07

Dr.-Ing. Stefan Freinatis

Deadlocks
Strategies for dealing with deadlocks:

1. Ignore the problem


Sounds silly, but in fact many operating systems do exactly this assuming that deadlocks occur rarely.

2. Detection & Recovery


The OS tries to detect deadlocks and then takes some recovery action.

3. Avoidance
Resources are granted in such a way that deadlocks cannot occur.

4. Prevention
Trying to break at least one of the four conditions such that no deadlock can happen.

Computer Architecture

WS 06/07

Dr.-Ing. Stefan Freinatis

Deadlocks
Strategy 1 (Ignoring the problem)
Most operating systems, including UNIX and Windows, just ignore the problem on the assumption that most users would prefer an occasional deadlock to a rule restricting all users to one process, one open file, and one of everything. If deadlocks could be eliminated for free, there would not be much discussion. But the price is high. If deadlocks occur on the average once a year, but system crashes owing to hardware failures and software errors occur once a week, nobody would be willing to pay a large penalty in performance or convenience to eliminate deadlocks (After Ta01 p.167 ). For that, the deadlock problem often is disregarded.
Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Deadlocks
Strategy 2 (Detection & Recovery)
The operating system tries to detect deadlocks and to recover.
Example DL.1 : Consider the following system state: Process A holds R and wants S Process B holds nothing and wants T Process C holds nothing and wants S Process D holds U and wants S and T Process E holds T and wants V Process F holds W and wants S Process G holds V and wants U. Is the system deadlocked, and if so, which processes are involved?
Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Strategy 2
Continuation of example DL.1 (deadlock detection) Constructing the resource allocation graph (a):
Figure from [Ta01 p.169]

Deadlocks

deadlock

The extracted cycle (b) shows the processes and resources involved in a deadlock.
Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Strategy 2
Deadlock detection with multiple instances of a resource type We have (respectively we define):
Deadlocks

n processes: P1 ... Pn m resource classes


Ei = the number of existing resource instances of resource class i, 1 i m. E is the existing resource vector, E = (E1 ... Em). A is the available resource vector. Each Ai in A gives the number of currently available resource instances. A = (A1 ... Am). Relation X Y is defined to be true if each Xi Yi.
Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Strategy 2
Deadlock detection with multiple instances of a resource type Definition of current allocation matrix and request matrix:
Deadlocks

P1 P2

Figure from [Ta01 p.171] Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Strategy 2
Deadlock detection with multiple instances of a resource type Deadlock detection algorithm: 1. All processes are initially unmarked 2. Look for an unmarked process Pi for which row Ri A
Here the algorithm is looking for a process that can be run to completion (the resource demands of the process can be satisfied immediately). Deadlocks

3. If such a Pi is found, add row Ci to A and mark Pi. Go to step 2.


After Pi is (or would have) finished, its resources are given back to the pool. The process is marked (in the sense of successful completion).

4. If no such process exists, terminate. All unmarked processes, if any, are deadlocked!
Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Strategy 2
Example DL.2 (deadlock detection algorithm): Consider the following system state:
Deadlocks

Figure from [Ta01 p.173]

Is there (or will there be) a deadlock in the system?


Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Strategy 2
Continuation of example DL.2 (deadlock detection algorithm)
Deadlocks Checking P1: R1 is not A (CD-ROM is missing). P1 cannot run and is not marked. Checking P2: R2 is not A (Scanner is missing). P2 cannot run and is not marked. Checking P3: R3 is A, thus P3 can run and is marked. The resources are given back to the pool. A = (2 2 2 0). Checking P1: R1 still is A (CD-Rom still not available). Checking P2: R2 now is A, thus P2 can run and is marked. The resources are given back to the pool. A = (4 2 2 1). Checking P1: R1 now is A. P1 can run and is marked. The resources are given back to the pool. A = (4 2 3 1) = E. No more unmarked processes: termination.

No deadlocks.
Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Strategy 2
Example DL.3 (deadlock detection algorithm):
Deadlocks

Same as DL.2 but now C2 = (2 1 0 1) and thus A = (2 0 0 0).

Checking P1: R1 is not A (CD-ROM is missing). P1 cannot run and is not marked. Checking P2: R2 is not A (Scanner is missing). P2 cannot run and is not marked. Checking P3: R3 is not A (Plotter is missing). P3 cannot run and is not marked. All processes checked. Nothing will change: termination.

The entire system is deadlocked!

Computer Architecture

WS 06/07

Dr.-Ing. Stefan Freinatis

Strategy 2
Detection & Recovery
Deadlocks

Resource Preemption
Forcibly taking away a resource from a process. May have ill side effects. Difficult or even impossible in many cases.

Process Rollback
A process periodically writes its complete state to file (checkpointing). In case of a deadlock, the process is rolled back to an earlier state in which it occupied lesser resources. Program(ming) overhead!

Killing Processes
Crudest but simplest method. One or more processes from the chain are terminated and must be started all over again at some later point in time. May also cause ill effects consider a process updating a data base twice instead of once.

Computer Architecture

WS 06/07

Dr.-Ing. Stefan Freinatis

Deadlocks
Strategy 3 (Avoidance)
Do not allow system states that may result in a deadlock.
A state is said to be safe when it is not deadlocked and there exists some scheduling order in which every process can run to completion even if all of them request their maximum number of resources. An unsafe state may result in a deadlock, but does not have to. maximum number of resource instances needed (requests) number of resource instances currently held (allocation)

Assume there is a total number of 10 instances available. Then the state is a safe state since there is a way to run all processes.
Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Strategy 3
Deadlocks

(a)

(b)

(c)

(d)

(e)
Figure from [Ta01 p.177]

a) starting situation (question: is this a safe state?). There are 3 resources left in the pool. b) B is granted 2 additional resources. c) B has finished. Now 5 resources are free. d) C is granted another 5 resources.

e) C has finished. Now 7 resources are free. Process A can be run without problems. Thus (a) is a safe state.
Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Strategy 3
Deadlocks

(a)

(b)

(c)

(d)
Figure from [Ta01 p.177]

a) starting situation as before (this is a safe state) b) A is granted one additional resource. c) B is granted the remaining 2 resources. d) B has finished. A and C cannot run because each of them needs 5 resources to complete. Deadlock.

Any other sequence starting from (b) also ends up in a deadlock. Therefore state (b) is an unsafe state. The move from (a) to (b) was bringing the system from a safe state to an unsafe state.
Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Strategy 3
Bankers Algorithm (Dijkstra 1965)
Deadlocks

Think of a small-town banker who deals with a group of customers to whom he has granted lines of credit. If granting a request leads to an unsafe state, the request is denied. If a request leads to a safe state, the request is granted. Knowing that not all customers need their credit line immediately, the banker has reserved 10 money units instead of 22 to service them. Initial state There are four customers (processes) demanding for a total of 22 money units (resources). The banker (operating system) has provided 10 money units in total.

Computer Architecture

WS 06/07

Dr.-Ing. Stefan Freinatis

Strategy 3
Continuation Bankers Algorithm
Deadlocks

The bankers algorithm considers each request as it occurs. A request is granted when the state remains safe, otherwise the request is postponed until later.

(a)

(b)

(c)

a) Initial state (safe) b) Safe state: Cs maximum request can be satisfied. When C has paid back the 4 money units, Bs request (or Ds) can be satisfied. ... c) Unsafe state: If any of the customers requests the maximum, the banker would be stuck (deadlock). Figure from [Ta01 p.178]
Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Strategy 3
Bankers Algorithm for multiple resource instances
Deadlocks

Existing

Available Possessed (allocated)

Current allocation matrix C

Request matrix R
Figure from [Ta01 p.179] Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Strategy 3
Bankers Algorithm for multiple resource instances
Deadlocks

1. Look for a row Ri whose unmet requirements are smaller than (or equal) to A. If no such row exists, the system will deadlock sooner or later since no process can run to completion. 2. Assume the process of the row chosen requests its maximum resources (which is guaranteed to be possible) and finishes. Mark the process as terminated and add its resources to the pool A. 3. Repeat steps 1 and 2 until either all processes are marked (in which case the initial state was safe), or until a deadlock occurs (in which case the initial state was unsafe).
Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Strategy 3
Bankers Algorithm for multiple resource instances
The pool is A = (1 0 2 0). Process D can be scheduled next because (0 0 1 0) < (1 0 2 0). When finished, the pool is A = (1 0 1 0) + (1 1 1 1) = (2 1 2 1) . Process A can be scheduled because (1 1 0 0) < ( 2 1 2 1). When finished, the pool is A = (1 0 2 1) + (4 1 1 1) = (5 1 3 2). Process B can be scheduled because (0 1 1 2) < (5 1 3 2). When finished, the pool is A = (5 0 2 0) + (0 2 1 2) = (5 2 3 2). Process C can be scheduled because (3 1 0 0) < (5 2 3 2). When finished, the pool is A = (2 1 3 2) + (4 2 1 0) = (6 3 4 2). Process E can be scheduled because (2 1 1 0) < (6 3 4 2). When finished, the pool is A = (4 2 3 2) + (2 1 1 0) = (6 3 4 2).
Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Deadlocks

Strategy 3
Bankers Algorithm for multiple resource instances
No more processes. All processes have successfully completed.
Deadlocks

The state shown is a safe state since we have found at least one way to complete all processes. Other sequences are possible.

In practice the bankers algorithm is of minor use, because processes rarely know in advance the maximum number of resources needed, the number of processes is not constant over time as users log in and out (or other events require computational attention).
Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Deadlocks
Strategy 4 (Deadlock Prevention)
Break (at least) one of the four conditions for a deadlock.

Avoiding mutual exclusion


Sometimes possible. Instead of using a printer exclusively, the processes write into a print spooler directory. This way several processes can use the printer at the same time. However, an internal system table (e.g. process table) cannot be spooled. Similar applies to a CD-Writer.

Breaking the hold and wait


Processes request all their resources at once (either all or none). However, not all processes know their demand from the beginning. Moreover, the resources are not optimally used then (degradation in multi-programming). Variation: each time an additional resource is needed, the process releases all its resources first and then tries to acquire all of them at once. This way a process does not occupy resources while waiting for a new one.
Computer Architecture WS 06/07 Dr.-Ing. Stefan Freinatis

Strategy 4
Deadlocks

Attacking the no preemption condition


Forcibly removing a resource from a process is barely possible.

Breaking circular wait


Provide a global numbering of all resources (ranking). Resource requests must be made in ascending order. This way a resource allocation graph can have no cycles. In the figure, B cannot request the scanner even if it would be available.

1. 2. 3. 4. 5.

Imagesetter Scanner Plotter Tape drive CD-Rom drive

Scanner

Plotter

However, not all resources allow for a reasonable order. How to order table slots, disk spooling space, locked database records?

Computer Architecture

WS 06/07

Dr.-Ing. Stefan Freinatis