Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 30

CS 241 Spring 2007 System Programming

Introduction to Deadlock
Lecture 21 Klara Nahrstedt
1

CS241 Administrative

Read Stallings Chapter 6

Content of This Lecture


Goals:
Understanding deadlock How to avoid deadlocks

Things covered in this lecture


Deadlock concepts Deadlock conditions

Resource (1)
A resource is a commodity needed by a process. Resources can be either:
serially reusable: e.g., CPU, memory, disk space, I/O devices, files. acquire use release
Real life analogy?

consumable: produced by a process, needed by a process; e.g., messages, buffers of information, interrupts. create acquire use Resource ceases to exist after it has been used, so it is not released.
Real life analogy?
4

Resource (2)
Resources can also be either:
preemptible: e.g., CPU, central memory
What does it mean?

non-preemptible: e.g., tape drives.


Any other examples?

And resources can be either:


shared among several processes or dedicated exclusively to a single process.
5

Using Semaphore to Share Resource


Process P(); 0 { A.Down(); 6 B.Down(); use both resource B.Up(); A.Up(); }

2 3
4 5

Process Q(); { A.Down(); B.Down(); use both resource B.Up(); A.Up(); }

2 3 4 5

External Semaphore A(1), B(1); External Semaphore A(0), B(1); External Semaphore A(0), B(0); External Semaphore A(0), B(1); External Semaphore A(1), B(1);
6

But Deadlock can Happen!

1 2

Process P(); 1 { A.Down(); 3 B.Down(); use both resources B.Up(); A.Up(); }

Process Q(); { B.Down(); A.Down(); use both resources A.Up(); B.Up(); }

1 External Semaphore A(1), B(1); 2 External Semaphore A(0), B(1); 3 External Semaphore A(0), B(0);
7

Deadlock

Mechanism for Deadlock Control


8

Another Example - Remember Dining Philosophers?

Dining Philosopher Challenge


{ Think | Eat } N Philosophers circular table with N chopsticks To eat the Philosopher must first pickup two chopsticks ith Philosopher needs ith & i+1th chopstick Only put down chopstick when Philosopher has finished eating Devise a solution which satisfies mutual exclusion but avoids starvation and deadlock
10

Seems simple enough ?


while(true) { think() sem_wait(chopstick[i]) sem_wait(chopstick[(i+1) % N]) eat() sem_post(chopstick[(i+1) % N]) sem_post(chopstick[i]) }
11

Deadlock (Each P. holds left fork)


while(true) { think() sem_wait(chopstick[i]) sem_wait(chopstick[(i+1) % N]) eat() sem_post(chopstick[(i+1) % N]) sem_post(chopstick[i]) }
12

Deadlock Definition
What is a deadlock?
A process is deadlocked if it is waiting for an event that will never occur. Typically, but not necessarily, more than one process will be involved together in a deadlock (the deadly embrace).

Is deadlock the same as starvation (or indefinitely postponed)?


A process is indefinitely postponed if it is delayed repeatedly over a long period of time while the attention of the system is given to other processes. I.e., logically the process may proceed but the system never gives it the CPU.
13

Conditions for Deadlock


What conditions should exist in order to lead to a deadlock Real life analogy such as
You take the monitor, I grab the keyboard Cars in intersection

14

Necessary and Sufficient Conditions for Deadlock


Mutual exclusion
Processes claim exclusive control of the resources they require

Wait-for condition
Processes hold resources already allocated to them while waiting for additional resources

No preemption condition
Resources cannot be removed from the processes holding them until used to completion

Circular wait condition


A circular chain of processes exists in which each process holds one or more resources that are requested by the next process in the chain
15

Mutual Exclusion

Processes claim exclusive control of the resources they require

How to break it?

16

Wait-for Condition

Processes hold resources already allocated to them while waiting for additional resources

How to break it?

17

Process 1
P(s1) P(s2) Work V(s2) V(s1) Blocked

Process 2
P(s2) P(s3) Work V(s3) V(s2)

Process 3
P(s3) P(s1) Work V(s1) V(s3)

18

No Preemption Condition

Resources cannot be removed from the processes holding them until used to completion

How to break it?

19

Its difficult to take a tape drive away from a process that is busy writing a tape

20

Circular Wait Condition

A circular chain of processes exists in which each process holds one or more resources that are requested by the next process in the chain How to break it?

21

Process 1 P(s1) P(s2) Work V(s2) V(s1) Blocked

Process 2 P(s2) P(s3) Work V(s3) V(s2)

Process 3 P(s3) P(s1) Work V(s1) V(s3)

22

Deadlock Modeling (1)


Resource allocation graph Graph have two kinds of nodes
Processes Resources

Graphs arcs have two meanings:


Arc from resource node to process node = process holds resource (i.e., resource has been assigned to process and process is holding it) Arc from process node to resource node = process requests resource (i.e., process is currently blocked waiting for that resource)
23

Deadlock Modeling (2)


Modeled with directed graphs

assign

request

resource R assigned to process A process B is requesting/waiting for resource S process C and D are in deadlock over resources T and U
24

Deadlock Modeling (3)


A B C

How deadlock occurs


25

Deadlock Modeling (4)

(o)

(p)

(q)

How deadlock can be avoided


26

Resource Allocation Graph (multiple resource types)

27

Deadlock Model

28

Strategies
Strategies for dealing with Deadlocks
detection and recovery dynamic avoidance
careful resource allocation

prevention
negating one of the four necessary conditions

29

Summary
Principles of Deadlock Examples of Deadlock Deadlock Conditions Deadlock Modeling

30

You might also like