107
1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and Science University

1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

  • View
    216

  • Download
    2

Embed Size (px)

Citation preview

Page 1: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

1

CSE 513Introduction to Operating Systems

Class 4 - IPC & Synchronization (2)Deadlock

Jonathan WalpoleDept. of Comp. Sci. and Eng.

Oregon Health and Science University

Page 2: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

2

Counting semaphores

A binary semaphore can only take on the values of [0, 1].

Class exercise: create a counting semaphore (generalized semaphore that we discussed previously) using just a binary semaphore!!

Page 3: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

3

Administrivia

Assignments 0 & 1 Next week

midterm exam project review & discussion (Chris Chambers)

Following week Memory Management (Wuchi Feng)

Page 4: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

4

Possible solution

Semaphore S1, S2, S3; // BINARY!!int C = N; // N is # locks

down_c(sem){ downB(S3); downB(S1); C = C – 1; if (C<0) { upB(S1); downB(S2); } else { upB(S1); } upB(S3); }

up_c(sem){ downB(S1); C = C + 1; if (C<=0) { upB(S2); } upB(S1); }

Page 5: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

5

Monitors

It is difficult to produce correct programs using semaphores

correct ordering of up and down is tricky! avoiding deadlock is tricky! boundary conditions are tricky!

Can we get the compiler to generate the correct semaphore code for us?

what are suitable higher level abstractions for synchronization?

Page 6: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

6

Monitors

Collect related shared objects together in a monitor

Characteristics Local data variables are accessible only via the

monitor’s procedures (encapsulation) Processes enter the monitor by invoking one of its

procedures Only one process may execute within the monitor at a

given time (mutual exclusion)

Condition variables (cv) Wait(cv) – process blocked (queued) until condition

holds Signal(cv) – signals the condition and unblocks

(dequeues) a process

Page 7: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

7

Monitor structures

initializationcode

monitor operations

y

x

shared data

condition queuesmonitor entry queue

Page 8: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

8

Monitor example for mutual exclusion

process Producerbegin loop <produce char “c”> BoundedBuffer.deposit(c) end loopend Producer

process Consumerbegin loop BoundedBuffer.remove(c) <consume char “c”> end loopend Consumer

monitor: BoundedBuffervar buffer : ...; nextIn, nextOut :... ;

entry deposit(c: char) begin ... end

entry remove(var c: char) begin ... end

end BoundedBuffer

Page 9: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

9

Monitor example with condition variables

monitor : BoundedBuffervar buffer : array[0..n-1] of char nextIn,nextOut : 0..n-1 := 0 fullCount : 0..n := 0 notEmpty, notFull : condition

entry deposit(c:char) entry remove(var c: char) begin begin if (fullCount = n) then if (fullCount = n) then wait(notFull) wait(notEmpty) end if end if

buffer[nextIn] := c c := buffer[nextOut] nextIn := nextIn+1 mod n nextOut := nextOut+1 mod n fullCount := fullCount+1 fullCount := fullCount-1 signal(notEmpty) signal(notFull) end deposit end removeend BoundedBuffer

Page 10: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

10

Monitor design choices

Condition variables introduce a problem for mutual exclusion

only one process active in the monitor at a time, so what to do when a process is unblocked on signal?

must not block holding the mutex, so what to do when a process blocks on wait?

Should signals be stored/remembered? signals are not stored if signal occurs before wait, signal is lost!

Should signals count?

Page 11: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

11

Monitor design choices

Choices when A signals a condition that unblocks B A waits for B to exit the monitor or blocks again B waits for A to exit the monitor or block Signal causes A to immediately exit the monitor or block

(on what condition?)

Choices when A signals a condition that unblocks B & C

B is unblocked, but C remains blocked C is unblocked, but B remains blocked

Choices when A calls wait and blocks a new external process is allowed to enter but which one?

Page 12: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

12

Common monitor semantics

Hoare semantics On signal, allow signaled process to run; upon

its exit from the monitor, signaler process continues

Brinch Hansen semantics signaler must immediately exit following signal

Page 13: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

13

Message Passing

Interprocess communication via shared memory across machine boundaries

Message passing can be used locally or remotely for synchronization or general communication

processes use send and receive primitives receive can block like wait send unblocks a process blocked on receive (like

signal unblocking a waiting process)

Page 14: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

14

Producer consumer with message passing

Page 15: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

15

Design Choices for Message Passing

Mailboxes system maintains a buffer of sent, but not yet

received, messages Rendezvous

sender and receiver must be active at the same time

receive must be blocked before send occurs kernel does no buffering

Page 16: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

16

Barriers

Use of a barrier processes approaching a barrier all processes but one blocked at barrier last process arrives, all are let through

Page 17: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

17

Deadlock

Page 18: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

18

Resources and Deadlocks

Processes need access to resources in order to make progress

Examples of computer resources printers tape drives kernel data structures (process & file table entries …) locks/semaphores to protect critical sections

Suppose a process holds resource A and requests resource B

at same time another process holds B and requests A both are blocked and remain so … this is deadlock

Page 19: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

19

Resource Usage Model

Sequence of events required to use a resource

request the resource (like acquiring a mutex lock) use the resource release the resource (like releasing a mutex lock)

Must wait if request is denied block busy wait fail with error code

Page 20: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

20

Preemptable vs Nonpreemptable Resources

Preemptable resources can be taken away from a process with no ill effects

Nonpreemptable resources will cause the process to fail if taken away

Deadlocks occur when processes are granted exclusive access to non-preemptable resources

Page 21: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

21

Definition of Deadlock

A set of processes is deadlocked if eachprocess in the set is waiting for an event that only

another process in the set can cause

Usually the event is the release of a currently held resource

None of the processes can … be awakened run release resources

Page 22: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

22

Deadlock conditions

A deadlock situation can occur if and only if the following conditions hold simultaneously

Mutual exclusion condition – resource assigned to one process

Hold and wait condition – processes can get more than one resource

No preemption condition

Circular wait condition – chain of two or more processes (must be waiting for resource from next one in chain)

Page 23: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

23

Resource acquisition scenarios

down (resource_1); use resource_1;up (resource_1);

down (resource_1);down (resource_2); use both resources;up (resource_2);up (resource_1);

down (resource_2); use resource_2;up (resource_2);

down (resource_1);down (resource_2); use both resources;up (resource_2);up (resource_1);

Page 24: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

24

Resource acquisition scenarios

down (resource_1);down (resource_2); use resources;up (resource_2);up (resource_1);

down (resource_2);down (resource_1); use resources;up (resource_1);up (resource_2);

down (resource_1); use resource;up (resource_1);down (resource_2); use resource;up (resource_2);

down (resource_2); use resource;up (resource_2);down (resource_1); use resource;up (resource_1);

Page 25: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

25

Flavors of Deadlock

Not so bad Programmer creates a situation that deadlocks Kill the program and move on

Worse Spin locks and locking mechanisms within the

OS

Page 26: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

26

Other examples of deadlock

Page 27: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

27

Deadlock modeling

Resource Allocation Graphs (RAGs)

Resource R assigned to process A Process B waiting for resource S Process C and D are deadlocked over T & U

Page 28: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

28

Dealing with deadlock

Four general strategies Ignore the problem

• Hmm… advantages, disadvantages?

Detection and recovery

Dynamic avoidance through resource allocation

Prevention, by structurally negating one of the four conditions

Page 29: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

29

Deadlock detection (1 resource of each)

Let the problem happen, then recover

How do you know it happened?

Do a depth-first-search on the resource allocation graph

Page 30: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

30

Deadlock detection (1 resource of each)

Let the problem happen, then recover

How do you know it happened?

Do a depth-first-search on the resource allocation graph

Page 31: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

31

Deadlock detection (1 resource of each)

Let the problem happen, then recover

How do you know it happened?

Do a depth-first-search on the resource allocation graph

Page 32: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

32

Deadlock detection (1 resource of each)

Let the problem happen, then recover

How do you know it happened?

Do a depth-first-search on the resource allocation graph

Page 33: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

33

Deadlock detection (1 resource of each)

Let the problem happen, then recover

How do you know it happened?

Do a depth-first-search on the resource allocation graph

Page 34: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

34

Deadlock modeling with multiple resources

Theorem: If a graph does not contain a cycle then no processes are deadlocked

A cycle in a RAG is a necessary condition for deadlock

Is the existence of a cycle a sufficient condition?

Page 35: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

35

Deadlock modeling with multiple resources

Theorem: If a graph does not contain a cycle then no processes are deadlocked

A cycle in a RAG is a necessary condition for deadlock

Is the existence of a cycle a sufficient condition?

Page 36: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

36

Deadlock detection (multiple resources)

Page 37: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

37

Deadlock detection (multiple resources)

Available resource vectorTotal resource vector

Page 38: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

38

Deadlock detection (multiple resources)

Available resource vectorTotal resource vector

What I am requesting nowWhat I have (now!)

Page 39: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

39

Detection algorithm

Is there a sequence of running process such that all the resources will be returned?

Page 40: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

40

Detection algorithm

1. Look for an unmarked process Pi, for which the ith row of R is less than or equal to A

2. If such a process is found, add the i-th row of C to A, mark the process and go back to step 1

3. If no such process exists the algorithm terminates

If all marked, no deadlock

Page 41: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

41

Detection algorithm

Page 42: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

42

Detection algorithm

Page 43: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

43

Detection algorithm

Page 44: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

44

Detection algorithm

2 2 2 0

Page 45: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

45

Detection algorithm

2 2 2 0

Page 46: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

46

Detection algorithm

4 2 2 1 2 2 2 0

Page 47: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

47

Detection algorithm

4 2 2 1 2 2 2 0

No deadlock!

Page 48: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

48

Detection algorithms

How often should the algorithm run?

After every resource request?• How many requests types are there?

Periodically?

When CPU utilization is low?

When we suspect deadlock?

Page 49: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

49

Recovery from deadlock

What should be done to recover? Abort deadlocked processes and reclaim

resources Temporarily reclaim resource, if possible Abort one process at a time until deadlock cycle

is eliminated Where to start?

• Low priority process• How long process has been executing• How many resources a process holds• Batch or interactive• Number of processes that must be

terminated

Page 50: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

50

Other deadlock recovery techniques

Recovery through rollback

Save state periodically Start computation again from “checkpoint”

Done for large computation systems

Page 51: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

51

Deadlock avoidance

Detection vs. avoidance…

Detection – “optimistic” approach• Allocate resources• “Break” system to fix it

Avoidance – “pessimistic” approach• Don’t allocate resource if it may lead to

deadlock

Which one to use depends upon the application

Page 52: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

52

Resource allocation plot

?

Page 53: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

53

Resource allocation graph

Page 54: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

54

Safe states

Safe state – “when system is not deadlocked and there is some scheduling order in which every process can run to completion even if all of them suddenly request their maximum number of resource immediately”

6

2

5

10 total

3

Page 55: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

55

Unsafe states

6

2

5

10 total

3

5

2

5

2

unsafe

Page 56: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

56

Banker’s algorithm for multiple resources

Look for a row, R, whose unmet resource needs are all smaller than or equal to A. If no such row exists, the system will eventually deadlock since no process can run to completion

Assume the process of the row chosen requests all the resources that it needs (which is guaranteed to be possible) and finishes. Mark that process as terminated and add all its resources to A vector

Repeat steps 1 and 2, until either all process are marked terminated, in which case the initial state was safe, or until deadlock occurs, in which case it was not

Page 57: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

57

Avoidance modeling

Available resource vectorTotal resource vector

Maximum Request Vector

Row 2 is what process 2 might need

RUN ALGORITHM ON EVERY RESOURCE REQUEST

Page 58: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

58

Avoidance algorithm

Max request matrix

Page 59: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

59

Avoidance algorithm

Max request matrix

Page 60: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

60

Avoidance algorithm

Max request matrix

Page 61: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

61

Avoidance algorithm

2 2 2 0

Max request matrix

Page 62: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

62

Avoidance algorithm

2 2 2 0

Max request matrix

Page 63: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

63

Avoidance algorithm

4 2 2 1 2 2 2 0

Max request matrix

Page 64: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

64

Deadlock avoidance

Deadlock avoidance is usually impossible because you don’t know in advance what

resources a process will need!

Alternative approach “deadlock prevention”

Prevent the situation in which deadlock might occur for all time!

Attack one of the four conditions that are necessary for deadlock to be possible

Page 65: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

65

Attacking the conditions

Attacking mutual exclusion? a bad idea for some resource types may work for others

Attacking no preemption? a bad idea for some resource types may work for others

Page 66: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

66

Attacking the conditions

Attacking hold and wait have processes request all resources before

beginning– Underallocation of resources– Unknown requests

if new request, deallocate and reallocate!

Page 67: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

67

Attacking the conditions

Attacking circular wait same problem/solution in the dining philosophers number all resources and acquire in the same

order may be hard to get an ordering that everyone

likes

1 2 3 4 5 6 7

Page 68: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

68

Attacking the conditions

Attacking circular wait Same problem in the dining philosophers Number all resources

Typically hard to get an ordering that everyone likes

1 2 3 4 5 6 7

Page 69: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

69

Attacking the conditions

Attacking circular wait Same problem in the dining philosophers Number all resources

Typically hard to get an ordering that everyone likes

1 2 3 4 5 6 7

Page 70: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

70

A word on starvation

Starvation and deadlock are two different things

With deadlock – no work is being accomplished for the processes that are deadlocked, because processes are waiting for each other

With starvation – work (progress) is getting done, however, a particular set of processes may not be getting any work done because they cannot obtain the resource they are trying to get

Page 71: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

71

Summary

What is deadlock?

Deadlock detection algorithms

Read Chapter 3

Sample problems• Chap 3: 15, 20, 21

Page 72: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

72

Spare slides

Solution to sleeping barber problem.

Page 73: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

73

Deadlocks

Chapter 3

3.1. Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance 3.6. Deadlock prevention 3.7. Other issues

Page 74: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

74

Deadlock Modeling (2)

Modeled with directed graphs

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

Page 75: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

75

Deadlock Modeling (3)

Strategies for dealing with Deadlocks just ignore the problem altogether detection and recovery dynamic avoidance

• careful resource allocation prevention

• negating one of the four necessary conditions

Page 76: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

76

A B C

How Deadlock Occurs

Page 77: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

77

How Deadlock Can be Avoided

(o) (p) (q)

Page 78: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

78

The Ostrich Algorithm

Pretend there is no problem Reasonable if

deadlocks occur very rarely cost of prevention is high

UNIX and Windows takes this approach It is a trade off between

convenience correctness

Page 79: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

79

Detection with One Resource of Each Type

Note the resource ownership and requests A cycle can be found within the graph, denoting

deadlock

Page 80: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

80

Detection with One Resource of Each Type

Data structures needed by deadlock detection algorithm

Page 81: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

81

Example Deadlock Detection Algorithm

Page 82: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

82

Recovery from Deadlock (1)

Recovery through preemption take a resource from some other process depends on nature of the resource

Recovery through rollback checkpoint a process periodically use this saved state restart the process if it is found deadlocked

Page 83: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

83

Recovery from Deadlock (2)

Recovery through killing processes crudest but simplest way to break a deadlock kill one of the processes in the deadlock cycle the other processes get its resources choose process that can be rerun from the beginning

Page 84: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

84

Deadlock AvoidanceResource Trajectories

Two process resource trajectories

Page 85: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

85

Safe and Unsafe States (1)

Demonstration that the state in (a) is safe

(a) (b) (c) (d) (e)

Page 86: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

86

Safe and Unsafe States (2)

Demonstration that the sate in b is not safe

(a) (b) (c) (d)

Page 87: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

87

The Banker's Algorithm for a Single Resource

Three resource allocation states safe safe unsafe

(a) (b) (c)

Page 88: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

88

Banker's Algorithm for Multiple Resources

Example of banker's algorithm with multiple resources

Page 89: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

89

Deadlock PreventionAttacking the Mutual Exclusion Condition

Some devices (such as printer) can be spooled

only the printer daemon uses printer resource thus deadlock for printer eliminated

Not all devices can be spooled Principle:

avoid assigning resource when not absolutely necessary

as few processes as possible actually claim the resource

Page 90: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

90

Attacking the Hold and Wait Condition

Require processes to request resources before starting a process never has to wait for what it needs

Problems may not know required resources at start of run also ties up resources other processes could be using

Variation: process must give up all resources then request all immediately needed

Page 91: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

91

Attacking the No Preemption Condition

This is not a viable option Consider a process given the printer

halfway through its job now forcibly take away printer !!??

Page 92: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

92

Attacking the Circular Wait Condition (1)

Normally ordered resources A resource graph

(a) (b)

Page 93: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

93

Attacking the Circular Wait Condition (1)

Summary of approaches to deadlock prevention

Page 94: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

94

Other IssuesTwo-Phase Locking

Phase One process tries to lock all records it needs, one at a

time if needed record found locked, start over (no real work done in phase one)

If phase one succeeds, it starts second phase, performing updates releasing locks

Note similarity to requesting all resources at once Algorithm works where programmer can arrange

program can be stopped, restarted

Page 95: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

95

Nonresource Deadlocks

Possible for two processes to deadlock each is waiting for the other to do some task

Can happen with semaphores each process required to do a down() on two

semaphores (mutex and another) if done in wrong order, deadlock results

Page 96: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

96

Starvation

Algorithm to allocate a resource may be to give to shortest job first

Works great for multiple short jobs in a system

May cause long job to be postponed indefinitely

even though not blocked

Solution: First-come, first-serve policy

Page 97: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

97

Spare Slides

Page 98: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

98

System calls

Three main parts Providing hooks to “register” the system call Adding the actual system call code Compiling the kernel Making it available to programs

Page 99: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

99

System calls: header files

/usr/src/linux/include/new_sys_call.h

/usr/src/linux/include/asm-i386/unistd.h

/usr/src/linux/include/arch/i386/kernel/entry.S

#include <linux/linkage.h>#include <linux/unistd.h>_syscall0(int, new_sys_call);

...#define __NR_sys_new_sys_call 243 ...

...

.long SYMBOL_NAME(sys_new_sys_call)

...

Page 100: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

100

System calls: adding code

/usr/src/linux/kernel/new_sys_call.c

Modify Makefile to update with new files (if any).

Compile and install the kernel

#include <linux/new_sys_call.h>

asmlinkage int sys_new_sys_call(void){ return(222);}

Page 101: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

101

System calls: creating a program

The most important thing is to have the header file around (as well as the new kernel).

#include <linux/new_sys_call.h>

int main(){

printf(“return val %d\n”,new_sys_call());

}

Page 102: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

102

Monitors (3)

Solution to producer-consumer problem in Java (part 1)

Page 103: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

103

Monitors (4)

Solution to producer-consumer problem in Java (part 2)

Page 104: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

104

Semantics of monitors

What is the strongest statement we can make about the state of the monitor after a waiter wakes up?

entry deposit(c:char) entry remove(var c: char) begin begin if (fullCount = n) then : : wait(notFull) c := buffer[nextOut] fullCount := fullCount-1 end if signal(notFull) : : end deposit end remove

Page 105: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

105

Synchronization problems with Mesa P1 P2 P3

/* fullCount=n */

if (fullCount==n)

wait(notFull);

remove()

...

fullCount--;

signal(notFull);

...

/* exit monitor */

/* fullCount=n-1*/

deposit()

...

fullCount++;

...

/* exit monitor */

...

fullCount++;

Page 106: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

106

Mesa semantics

monitor : BoundedBuffervar buffer : array[0..n-1] of char nextIn,nextOut : 0..n-1 := 0 fullCount : 0..n := 0 notEmpty, notFull : condition

entry deposit(c:char) entry remove(var c: char) begin begin while (fullCount = n) then while (fullCount = n) then wait(notFull) wait(notEmpty) end while end while

buffer[nextIn] := c c := buffer[nextOut] nextIn := nextIn+1 mod n nextOut := nextOut+1 mod n fullCount := fullCount+1 fullCount := fullCount-1 signal(notEmpty) signal(notFull) end deposit end removeend BoundedBuffer

Page 107: 1 CSE 513 Introduction to Operating Systems Class 4 - IPC & Synchronization (2) Deadlock Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and

107

Another deadlock model

Model the state of the computer system as a directed graph G = (V,E)

V = set of vertices = {P1,…,Pn} u {R1,…,Rm}

E = set of edges = {edges from resource to process} u {edges from process to resource}

Pi Ri

Rj

Request edge Allocation edgePi Pk