Part II Part II Process Management Process Management Chapter 7: - - PowerPoint PPT Presentation

part ii part ii process management process management
SMART_READER_LITE
LIVE PREVIEW

Part II Part II Process Management Process Management Chapter 7: - - PowerPoint PPT Presentation

Part II Part II Process Management Process Management Chapter 7: Deadlocks Chapter 7: Deadlocks 1 Fall 2010 System Model System Model System resources are utilized in the following way: Request: If a process makes a request to use a


slide-1
SLIDE 1

Part II Part II Process Management Process Management

Chapter 7: Deadlocks Chapter 7: Deadlocks

1

Fall 2010

slide-2
SLIDE 2

System Model System Model

System resources are utilized in the following way:

Request: If a process makes a request to use a system resource which cannot be granted immediately, then the requesting process blocks until it can acquire the resource resource. Use: The process can operate on the resource. Release: The process releases the resource Release: The process releases the resource.

Deadlock Definition: A set of processes is in a d dl k t t h i th t i deadlock state when every process in the set is waiting for an event that can only be caused by th i th t

2

another process in the set.

slide-3
SLIDE 3

Deadlock: Deadlock: Necessary Necessary Conditions Conditions Deadlock: Deadlock: Necessary Necessary Conditions Conditions

For a deadlock to occur each of the following four For a deadlock to occur, each of the following four conditions must hold. M t l E l i At l t t Mutual Exclusion: At least one resource must be held in a non-sharable way. Hold and Wait: A process must be holding a resource and waiting for another. No Preemption: Resource cannot be preempted. Circular Wait: A waits for B, B waits for C, C Circular Wait: A waits for B, B waits for C, C waits for A.

3

slide-4
SLIDE 4

Handling Handling Deadlocks eadlocks Handling Handling Deadlocks Deadlocks

Deadlock Prevention and Avoidance: Making Deadlock Prevention and Avoidance: Making sure deadlocks will never happen.

Prevention: Ensuring one of the four conditions fails Prevention: Ensuring one of the four conditions fails. Avoidance: The OS needs more information so that it can determine if the current request can be q satisfied or delayed.

Deadlock : Allow a system to enter a deadlock situation, and then detect and recover. Ignore Deadlock: Pretend deadlocks never

  • ccur in the system.

4

slide-5
SLIDE 5

Deadlock Prevention: Deadlock Prevention: 1/4 1/4 Mutual Exclusion Mutual Exclusion

By ensuring that at least one of the four conditions cannot hold, we can prevent the

  • ccurrence of a deadlock.

Mutual Exclusion: Some sharable resources must be accessed exclusively (e.g., printer), y ( g , p ), which means we cannot deny the mutual exclusion condition.

5

slide-6
SLIDE 6

Deadlock Prevention: Deadlock Prevention: 2/4 2/4 Hold and Wait Hold and Wait

N h ld d h No process can hold some resources and then request for other resources. T i ibl Two strategies are possible:

A process must acquire all resources before it runs. Wh t f it t h ld When a process requests for resources, it must hold none (i.e., returning resources before requesting for more). )

Resource utilization may be low, since many resources will be held and unused for a long time. g Starvation is possible. A process that needs some popular resources my have to wait indefinitely.

6

p p y y

slide-7
SLIDE 7

Deadlock Prevention: Deadlock Prevention: 3/4 3/4 No No Preemption Preemption No No Preemption Preemption

Resources that are being held by the requesting d Th i process are preempted. There are two strategies:

If a process is holding some resources and requesting for some others that are being held by other processes the some others that are being held by other processes, the resources of the requesting process are preempted. The preempted resources become available. If the requested resources are not available: If they are being held by processes that are waiting for ddi i l h d additional resources, these resources are preempted and given to the requesting process. Otherwise the requesting process waits until the Otherwise, the requesting process waits until the requested resources become available. While it is waiting, its resources may be preempted.

7

This works only if the state of the process and resources can be saved and restored easily (e.g., CPU & memory).

slide-8
SLIDE 8

Deadlock Prevention: Deadlock Prevention: 4/4 4/4 Circular Circular Waiting Waiting Circular Circular Waiting Waiting

To break the circular waiting condition, we can To break the circular waiting condition, we can

  • rder all resource types (e.g., tapes, printers).

A process can only request resources higher than the A process can only request resources higher than the resource types it holds. Suppose the ordering of tapes, disks, and printers Suppose e o de g o pes, d s s, d p e s are 2, 4, and 8. If a process holds a disk (4), it can ask for a printer (8) and cannot request a tape (2). A process must release some lower order resources to request a lower order resource. To get tapes (1), a process must release its disk (4). In this way, no deadlock is possible. Why?

8

slide-9
SLIDE 9

Deadlock Deadlock Avoidance: Avoidance: 1/5 1/5 Deadlock Deadlock Avoidance: Avoidance: 1/5 1/5

Each process provides the maximum number of Each process provides the maximum number of resources of each type it needs. With these information there are algorithms that With these information, there are algorithms that can ensure the system will never enter a deadlock state This is deadlock avoidance

  • state. This is deadlock avoidance.

A sequence of processes <P1, P2, …, Pn> is a safe sequence if for each process Pi in the sequence its sequence if for each process Pi in the sequence, its resource requests can be satisfied by the remaining resources and the sum of all resources that are being held by P1, P2, …, Pi-1. This means we can suspend Pi and run P1, P2, …, Pi-1 until they

9

  • complete. Then, Pi will have all resources to run.
slide-10
SLIDE 10

Deadlock Deadlock Avoidance: Avoidance: 2/5 2/5 Deadlock Deadlock Avoidance: Avoidance: 2/5 2/5

A state is safe if the system can allocate resources to each allocate resources to each process (up to its maximum, of course) in some order and still

deadlock

avoid a deadlock. Thus, a state is safe if there is a safe seq ence Other ise if no

unsafe

safe sequence. Otherwise, if no safe sequence exists, the system state is unsafe.

unsafe

f An unsafe state is not necessarily a deadlock state. O th th h d d dl k

safe

On the other hand, a deadlock state is an unsafe state.

10

slide-11
SLIDE 11

Deadlock Deadlock Avoidance: Avoidance: 3/5 3/5 Deadlock Deadlock Avoidance: Avoidance: 3/5 3/5

A system has 12 tapes and three processes A, B, C. A system has 12 tapes and three processes A, B, C. At time t0, we have:

Max needs Current holding Will need

3 free tapes

Max needs Current holding Will need A 10 5 5 B 4 2 2

Then <B A C> is a safe seq ence (safe state)

C 9 2 7

Then, <B, A, C> is a safe sequence (safe state). The system has 12-(5+2+2)=3 free tapes. Since B needs 2 tapes, it can take 2, run, and return 4. After B completes, the system has (3- 2)+4=5 tapes A now can take all 5 tapes and run

11

2)+4=5 tapes. A now can take all 5 tapes and run. Finally, A returns 10 tapes for C to take 7 of them.

slide-12
SLIDE 12

Deadlock Deadlock Avoidance: Avoidance: 4/5 4/5 Deadlock Deadlock Avoidance: Avoidance: 4/5 4/5

A system has 12 tapes and three processes A, B, C. At y p p , , time t1, C has one more tape:

Max needs Current holding Will need Max needs Current holding Will need A 10 5 5 B 4 2 2

The system has 12-(5+2+3)=2 free tapes.

C 9 3 6

At this point, only B can take these 2 and run. It returns 4, making 4 free tapes available. , g p But, none of A and C can run, and a deadlock occurs. The problem is due to granting C one more tape

12

The problem is due to granting C one more tape.

slide-13
SLIDE 13

Deadlock Deadlock Avoidance: Avoidance: 5/5 5/5 Deadlock Deadlock Avoidance: Avoidance: 5/5 5/5

A deadlock avoidance algorithm ensures that A deadlock avoidance algorithm ensures that the system is always in a safe state. Therefore, no deadlock can occur no deadlock can occur. Resource requests are granted only if in doing th t i till i f t t so the system is still in a safe state. Consequently, resource utilization may be lower than those systems without using a deadlock avoidance algorithm.

13

slide-14
SLIDE 14

Banker’s Algorithm: Banker’s Algorithm: 1/4 1/4

The system has m resource types and n processes. E h t d l it i d Each process must declare its maximum needs. The following arrays are used:

A il bl [1 ] f h A il bl [i] k Available[1..m]: one entry for each resource. Available[i]=k means resource type i has k units available. Max[1 n 1 m]: maximum demand of each process Max[1..n,1..m]: maximum demand of each process. Max[i,j]=k means process i needs k units of resource j. Allocation[1..n,1..m]: resources allocated to each process. p Allocation[i,j]=k means process i is currently allocated k units of resource j. N d[1 1 ] th i i d f h Need[1..n,1..m]: the remaining resource need of each

  • process. Need[i,j]=k means process i needs k more units of

resource j. Thus, Max = Allocation + Need.

14

j ,

slide-15
SLIDE 15

Banker’s Banker’s Algorithm: Algorithm: 2/4 2/4 Banker’s Banker’s Algorithm: Algorithm: 2/4 2/4

We will use A[i *] to indicate the i-th row of We will use A[i, ] to indicate the i-th row of matrix A. Given two arrays A[1 m] and B[1 m] A ≤ B if A[i] Given two arrays A[1..m] and B[1..m], A ≤ B if A[i] ≤ B[i] for all i. Given two matrices A[1..n,1..m] and B[1..n,1..m], A[i,*] ≤ B[i,*] if A[i,j] ≤ B[i,j] for [ , ], [ , ] [ , ] [ ,j] [ ,j] all j. When a resource request is made by process i, this q y p , algorithm calls the Resource-Request algorithm to determine if the request can be granted. The Resource-Request algorithm calls the Safety Algorithm to determine if a state is safe.

15

slide-16
SLIDE 16

Banker’s Algorithm: Banker’s Algorithm: 3/4 3/4 S f t S f t Al ith Safety y Al Algor gorith ithm

1. Let Work[1..m] and Finish[1..n] be two working 1. Let Work[1..m] and Finish[1..n] be two working arrays. 2. Work := Available and Finish[i]=FALSE for all i 2. Work : Available and Finish[i] FALSE for all i 3. Find an i such that both

  • Finish[i] = FALSE

// process i is not yet done

  • Finish[i]

FALSE // process i is not yet done

  • Need[i,*] ≤ Work // its need can be satisfied

If no such i exists, go to Step 5 If no such i exists, go to Step 5 4. Work = Work + Allocation[i,*] // run it and reclaim Finish[i] = TRUE

// process i completes

Finish[i] = TRUE // process i completes go to Step 3 5 If Fi i h[i] TRUE f ll i th t i i

16

5. If Finish[i] = TRUE for all i, the system is in a safe state.

slide-17
SLIDE 17

Banker’s Algorithm: Banker’s Algorithm: 4/4 4/4 R R t R t Al ith Resource- esource-Reques equest Al Algor gorith ithm

1 Let Request[1 n 1 m] be the request matrix Request[i j]=k 1. Let Request[1..n,1..m] be the request matrix. Request[i,j]=k means process i requests k units of resource j. 2. If Request[i,*]≤Need[i,*], go to Step 3. Otherwise, it is an q [ , ] [ , ], g p , error. 3. If Request[i,*]≤Available, go to Step 4. Otherwise, process i i waits. 4. Do the following: Available Available Request[i *] Available = Available – Request[i,*] Allocation[i,*] = Allocation[i,*]+Request[i,*] Need[i *] = Need[i *] Request[i *] Need[i, ] = Need[i, ] – Request[i, ] If the result is a safe state (Safety Algorithm), the request is

  • granted. Otherwise, process i waits and the

17

g , p resource-allocation tables are restored back to the original.

slide-18
SLIDE 18

Example: Example: 1/4 1/4

Consider a system of 5 processes A, B, C, D and E, and 3 resource types (X=10, Y=5, Z=7). At time t0, we have

Allocation Max Need=Max Alloc Available X Y Z X Y Z X Y Z X Y Z A 1 7 5 3 7 4 3 3 3 2 Allocation Max Need=Max-Alloc Available A 1 7 5 3 7 4 3 3 3 2 B 2 3 2 2 1 2 2 C 3 2 9 2 6 D 2 1 1 2 2 2 1 1 E 2 4 3 3 4 3 1

A safe sequence is <B,D,E,C,A>. Since B’s [1,2,2]≤ Avail’s [3,3,2], B runs. Then, Avail=[2,0,0]+[3,3,2]=[5,3,2]. D runs next After this Avail=[5 3 2]+[2 1 1]=[7 4 3] E runs next

  • next. After this, Avail=[5,3,2]+[2,1,1]=[7,4,3]. E runs next.

Avail=[7,4,3]+[0,0,2]=[7,4,5]. Since C’s [6,0,0]≤Avail=[7,4,5], C

  • runs. After this, Avail=[7,4,5]+[3,0,2]=[10,4,7] and A runs.

18

  • runs. After this, Avail [7,4,5] [3,0,2] [10,4,7] and A runs.

There are other safe sequences: <D,E,B,A,C>, <D,B,A,E,C>, …

slide-19
SLIDE 19

Example: Example: 2/4 2/4

Now suppose process B asks for 1 X and 2 Zs. More precisely, RequestB = [1,0,2]. Is the system still in a safe state if this request is granted? if this request is granted? Since RequestB = [1,0,2] ≤ Available = [3,3,2], this request may be granted as long as the system is safe may be granted as long as the system is safe. If this request is actually granted, we have the following:

X Y Z X Y Z X Y Z X Y Z Allocation Max Need=Max-Alloc Available A 1 7 5 3 7 4 3 2 3 B 3 2 3 2 2 2 C 3 2 9 2 6 C 3 2 9 2 6 D 2 1 1 2 2 2 1 1 E 2 4 3 3 4 3 1

19

E 2 4 3 3 4 3 1

[3,0,2]=[2,0,0]+[1,0,2] [0,2,0]=[1,2,2]-[1,0,2] [2,3,0]=[3,3,2]-[1,0,2]

slide-20
SLIDE 20

Example: Example: 3/4 3/4

All ti M N d M All A il bl X Y Z X Y Z X Y Z X Y Z A 1 7 5 3 7 4 3 2 3 Allocation Max Need=Max-Alloc Available A 1 7 5 3 7 4 3 2 3 B 3 2 3 2 2 2 C 3 2 9 2 6 D 2 1 1 2 2 2 1 1 E 2 4 3 3 4 3 1

Is the system in a safe state after this allocation? Yes, because the safety algorithm will provide a safe sequence <B,D,E,A,C>. Verify it yourself. Hence, B’s request of [1,0,2] can safely be made.

20

slide-21
SLIDE 21

Example: Example: 4/4 4/4

All ti M N d M All A il bl X Y Z X Y Z X Y Z X Y Z A 1 7 5 3 7 4 3 2 3 Allocation Max Need=Max-Alloc Available A 1 7 5 3 7 4 3 2 3 B 3 2 3 2 2 2 C 3 2 9 2 6 D 2 1 1 2 2 2 1 1 E 2 4 3 3 4 3 1

After this allocation, E’s request RequestE=[3,3,0] cannot be granted since RequestE=[3,3,0] ≤[2,3,0] is

E

false. A’s request RequestA=[0,2,0] cannot be granted because the system will be unsafe. the system will be unsafe. If RequestA=[0,2,0] is granted, Available=[2,1,0]. None of the five processes can finish and the system is f

21

unsafe.

slide-22
SLIDE 22

Deadlock Deadlock Detection Detection Deadlock Deadlock Detection Detection

If a system does not use a deadlock prevention

  • r a deadlock avoidance algorithm then a
  • r a deadlock avoidance algorithm, then a

deadlock situation may occur. Thus, we need An algorithm that can examine the system An algorithm that can examine the system state to determine if a deadlock has occurred. This is a deadlock detection algorithm This is a deadlock detection algorithm. An algorithm that can help recover from a deadlock This is a recovery algorithm

  • deadlock. This is a recovery algorithm.

A deadlock detection algorithm does not have to know the maximum need Max and the to know the maximum need Max and the current need Need. It uses only Available, Allocation and Request.

22

q

slide-23
SLIDE 23

Deadlock Detection Algorithm Deadlock Detection Algorithm

1. Let Work[1..m] and Finish[1..n] be two working arrays. 2 Work := Available and Finish[i]=FALSE for all i 2. Work := Available and Finish[i]=FALSE for all i 3. Find an i such that both

  • Fi i h[i]

FALSE // i i t t d

  • Finish[i] = FALSE // process i is not yet done
  • Request[i,*] ≤ Work // its request can be satisfied

If h i i t t St 5 If no such i exists, go to Step 5 4. Work = Work + Allocation[i,*] // run it and reclaim Fi i h[i] TRUE // i l Finish[i] = TRUE // process i completes go to Step 3 5. If Finish[i] = TRUE for all i, the system is in a safe state. If Finish[i] = FALSE, then process Pi is deadlocked.

23

Use Request here rather than Need as in the safety algorithm

slide-24
SLIDE 24

Example: Example: 1/2 1/2

X Y Z X Y Z X Y Z Allocation Request Available A 1 B 2 2 2 C 3 3 D 2 1 1 1 Suppose maximum available resource is [7 2 6] and the E 2 2 Suppose maximum available resource is [7,2,6] and the current state of resource allocation is shown above. Is the system deadlocked? No. We can run A first, making Is the system deadlocked? No. We can run A first, making Available=[0,1,0]. Then, we run C, making Available=[3,1,3]. This is followed

24

, , g [ , , ] by D, making Available=[5,2,4], and followed by B and E.

slide-25
SLIDE 25

Example: Example: 2/2 2/2

X Y Z X Y Z X Y Z Allocation Request Available A 1 B 2 2 2 C 3 3 1 D 2 1 1 1 E 2 2

Suppose C requests for one more resource Z. Now A can run making Available=[0 1 0] Now, A can run, making Available=[0,1,0]. However, none of B, C, D and E can run. Th f B C D d E d dl k d!

25

Therefore, B, C, D and E are deadlocked!

slide-26
SLIDE 26

The Use of a Detection Al The Use of a Detection Algorithm

  • rithm

g

Frequency If deadlocks occur frequently, the detection algorithm should be invoked frequently. Once per hour or whenever CPU utilization becomes low (i.e., below 40%). Low CPU utilization usually means more processes are waiting.

26

slide-27
SLIDE 27

How to Recover: How to Recover: 1/3 1/3

When a detection algorithm determines a d dl k h d th l ith deadlock has occurred, the algorithm may inform the system administrator to deal with it. Of allo the s stem to from a deadlock Of, allow the system to recover from a deadlock. There are two options. Process Termination Resource Preemption p These two options are not mutually exclusive (i.e., can have both if needed) can have both if needed).

27

slide-28
SLIDE 28

Recovery: Process Termination: Recovery: Process Termination: 2/3 2/3

Abort all deadlocked processes Abort one process at a time until the deadlock cycle is eliminated Problems: Aborting a process may not be easy. What if a bo g p ocess y

  • be e sy. W

process is updating or printing a large file? The system must find some way to maintain the states

  • f the files and printer before they can be reused.

Termination may be determined by the priority/importance of a process.

28

slide-29
SLIDE 29

Recovery: Resource Preemption: Recovery: Resource Preemption: 3/3 3/3

Selecting a victim: which resources and which Selecting a victim: which resources and which processes are to be preempted? R llb k If t f Rollback: If we preempt a resource from a process, what should be done with that process?

  • i

Total Rollback: abort the process and restart it Partial Rollback: rollback the process only as far as t b k th d dl k necessary to break the deadlock.

Starvation: We cannot always pick the same i i S li i b process as a victim. Some limit must be set.

29

slide-30
SLIDE 30

Th E d The End

30