cis 5512 - operating systems deadlock professor qiang zeng fall 2015

33
CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Upload: daniel-oconnor

Post on 18-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

CIS 5512 - Operating SystemsDeadlock

Professor Qiang Zeng

Fall 2015

Page 2: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

CIS 5512 - Operating Systems 2

Previous class…

• Mutex• Semaphore• Concurrency problems

Page 3: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Deadlock

• A set of processes is deadlocked when each process in the set is blocked awaiting an event that can only be triggered by another blocked process in the set

CIS 5512 - Operating Systems 3

Some Slides Courtesy of Dr. William Stallings

Page 4: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Potential Deadlock

CIS 5512 - Operating Systems 4

I need quad A and B

I need quad B and C

I need quad C and D

I need quad D and A

Page 5: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Actual Deadlock

CIS 5512 - Operating Systems 5

HALT until B is free

HALT until C is free

HALT until D is free

HALT until A is free

Page 6: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Resource Categories

CIS 5512 - Operating Systems 6

Reusable• can be safely used by only one

process at a time and is not depleted by that use• processors, I/O channels, main and

secondary memory, devices, and data structures such as files, databases, and semaphores

Consumable• one that can be created

(produced) and destroyed (consumed)• interrupts, signals, messages,

and information in I/O buffers

Page 7: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Memory Request

• Space is available for allocation of 200Kbytes, and the following sequence of events occur:

• Deadlock occurs if both processes progress to their second request

CIS 5512 - Operating Systems 7

P1. . .

. . .Request 80 Kbytes;

Request 60 Kbytes;

P2 . . .

. . .Request 70 Kbytes;

Request 80 Kbytes;

Page 8: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Consumable Resources Deadlock

• Consider a pair of processes, in which each process attempts to receive a message from the other process and then send a message to the other process:

• Deadlock occurs if the Receive is blocking

CIS 5512 - Operating Systems 8

Page 9: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Resource Allocation Graphs

CIS 5512 - Operating Systems 9

There is a circle in the graph, which

indicates deadlock

Page 10: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Resource Allocation Graph describing the traffic jam

CIS 5512 - Operating Systems 10

Page 11: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Conditions for Deadlock

CIS 5512 - Operating Systems 11

Mutual Exclusion

• A process cannot access a resource that has been allocated to another process

Hold-and-Wait

• a process may hold allocated resources while awaiting assignment of others

No Pre-emption

• no resource can be forcibly removed from a process holding it

Circular Wait

• a closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain

Page 12: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Dealing with Deadlock

• Three general approaches exist for dealing with deadlock:

CIS 5512 - Operating Systems 12

• adopt a policy that eliminates one of the conditions

Prevent Deadlock

• make the appropriate dynamic choices based on the current state of resource allocation

Avoid Deadlock

• attempt to detect the presence of deadlock and take action to recover

Detect Deadlock

Page 13: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Deadlock Condition Prevention

CIS 5512 - Operating Systems 13

Mutual Exclusion

if access to a resource requires mutual exclusion then it must be

unlikely to share

Hold and Wait

require that a process request all of its

required resources at one time and blocking

the process until all requests can be

granted simultaneously

Page 14: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Deadlock Condition Prevention

• No Preemption– if a process holding certain resources is denied a further

request, that process must release its original resources and request them again

• Circular Wait– define a linear ordering of resource types– If a process has been allocated resources of type R , then it

may subsequently request only those resources of types following R in the ordering.

CIS 5512 - Operating Systems 14

Page 15: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Deadlock Avoidance

• A decision is made dynamically whether the current resource allocation request will lead to an unsafe state

• Requires knowledge of future process requests

CIS 5512 - Operating Systems 15

Page 16: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Example

CIS 5512 - Operating Systems 16

• State of a system consisting of 4 processes and 3 resources• Allocations have been made as follows

Page 17: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Determination of a Safe State

• P2 requests one 1 of R1 and 1 unit of R3 • Should this request be granted?• Banker’s algorithm: assume this request is granted, then check

whether the resulted state is safe• A state is safe if there is at least one sequence of resource

allocations that satisfies all the processes’ needs

CIS 5512 - Operating Systems 17

Is this a safe state?

Page 18: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

P2 Runs to Completion

CIS 5512 - Operating Systems 18

Available vector (0, 1, 1) + Allocated for P2 (6, 1, 2) = Updated available vector(6, 2, 3)

Page 19: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

P1 Runs to Completion

CIS 5512 - Operating Systems 19

Available vector (6, 2, 3) + Allocated for P1 (1, 0, 0) = Updated available vector(7, 2, 3)

Page 20: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

P3 Runs to Completion

CIS 5512 - Operating Systems 20

Thus, the state defined originally is safe

Page 21: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Determination of an Unsafe State

CIS 5512 - Operating Systems 21

P1 requests for one more R1 and one more R3

The request should not be granted, because it leads to an unsafe state

Page 22: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Deadlock detection

CIS 5512 - Operating Systems 22

Page 23: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Recovery strategies

– Kill all deadlocked processes– Kill one deadlocked process at a time and release its

resources– Steal one resource at a time– Roll back all or one of the processes to a checkpoint

that occurred before they requested any resources, then continue

• Difficult to prevent indefinite postponement

CIS 5512 - Operating Systems 23

Page 24: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Recovery by killing processes

CIS 5512 - Operating Systems 24

Page 25: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

CIS 5512 - Operating Systems 25

Page 26: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

# define N 5

void philosopher (int i) { while (TRUE) { think(); take_fork(i); take_fork((i+1)%N); eat(); /* yummy */ put_fork(i); put_fork((i+1)%N); }}

Dining Philosophers solution with unnumbered resources

Page 27: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

# define N 5

void philosopher (int i) { while (TRUE) { think(); take_fork(i); take_fork((i+1)%N); eat(); /* yummy */ put_fork(i); put_fork((i+1)%N); }}

Dining Philosophers solution with unnumbered resources

Page 28: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Dining Philosophers solution with unnumbered resources

# define N 5

void philosopher (int i) { while (TRUE) { think(); take_fork(i); take_fork((i+1)%N); eat(); /* yummy */ put_fork(i); put_fork((i+1)%N); }}

Page 29: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Dining Philosophers solution with numbered resources

Instead, number resources

First request lower numbered fork

# define N 5

void philosopher (int i) { while (TRUE) { think(); take_fork(LOWER(i)); take_fork(HIGHER(i)); eat(); /* yummy */ put_fork(LOWER(i)); put_fork(HIGHER(i)); }}

1

2

3

4

5

Page 30: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Dining Philosophers solution with numbered resources

Instead, number resources...

Then request higher numbered fork

# define N 5

void philosopher (int i) { while (TRUE) { think(); take_fork(LOWER(i)); take_fork(HIGHER(i)); eat(); /* yummy */ put_fork(LOWER(i)); put_fork(HIGHER(i)); }}

1

2

3

4

5

Page 31: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Dining Philosophers solution with numbered resources

Instead, number resources...

Then request higher numbered fork

# define N 5

void philosopher (int i) { while (TRUE) { think(); take_fork(LOWER(i)); take_fork(HIGHER(i)); eat(); /* yummy */ put_fork(LOWER(i)); put_fork(HIGHER(i)); }}

1

2

3

4

5

Page 32: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Dining Philosophers solution with numbered resources

Instead, number resources...

One philosopher can eat!

# define N 5

void philosopher (int i) { while (TRUE) { think(); take_fork(LOWER(i)); take_fork(HIGHER(i)); eat(); /* yummy */ put_fork(LOWER(i)); put_fork(HIGHER(i)); }}

1

2

3

4

5

Page 33: CIS 5512 - Operating Systems Deadlock Professor Qiang Zeng Fall 2015

Summary

• Deadlock:• Dealing with deadlock:

– Prevention – Detection – Avoidance

CIS 5512 - Operating Systems 33