Op Sy 03 Ch 24

25
1 C eng 334 -O perating System s 2.4-1 Chapter 2.4 : Deadlocks • Processconcept • Processscheduling • Interprocesscom m unication • D eadlocks • Threads Chapter 2.4: Deadlocks

Transcript of Op Sy 03 Ch 24

Page 1: Op Sy 03 Ch 24

1

Ceng 334 - Operating Systems 2.4-1

Chapter 2.4 : Deadlocks

• Process concept • Process scheduling • Interprocess communication • Deadlocks

• Threads

Chapter 2.4: Deadlocks

Page 2: Op Sy 03 Ch 24

2

Ceng 334 - Operating Systems 2.4-2

What is Deadlock?

• Process Deadlock– A process is deadlocked when it is

waiting on an event which will never happen

• System Deadlock– A system is deadlocked when one or

more processes are deadlocked

What is Deadlock

Page 3: Op Sy 03 Ch 24

3

Ceng 334 - Operating Systems 2.4-3

Necessary Conditions for a Deadlock

• Mutual Exclusion– Shared resources are used in a

mutually exclusive manner

• Hold & Wait– Processes hold onto resources they

already have while waiting for the allocation of other resources

Conditions for Deadlocks

Page 4: Op Sy 03 Ch 24

4

Ceng 334 - Operating Systems 2.4-4

Necessary Conditions for a Deadlock (Cont.)

• No Preemption– Resources can not be preempted

until the process releases them

• Circular Wait – A circular chain of processes exists

in which each process holds resources wanted by the next process in the chain

Condistiond for Deadlocks

Page 5: Op Sy 03 Ch 24

5

Ceng 334 - Operating Systems 2.4-5

No Deadlock Situation

If you can prevent at least one of the necessary deadlock conditions then you won’t have a DEADLOCK

No Deadlock

Page 6: Op Sy 03 Ch 24

6

Ceng 334 - Operating Systems 2.4-6

Ways of Handling Deadlock

• Deadlock Prevention

• Deadlock Detection

• Deadlock Avoidance

• Deadlock Recovery

Ways of Handling Deadlocks

Page 7: Op Sy 03 Ch 24

7

Ceng 334 - Operating Systems 2.4-7

Deadlock Prevention

• Remove the possibility of deadlock occurring by denying one of the four necessary conditions:

– Mutual Exclusion (Can we share everything?)

– Hold & Wait

– No preemption

– Circular Wait

Deadlock Prevention

Page 8: Op Sy 03 Ch 24

8

Ceng 334 - Operating Systems 2.4-8

• Implementation

– A process is given its resources on a

"ALL or NONE" basis

– Either a process gets ALL its required

resources and proceeds or it gets

NONE of them and waits until it can

Denying the “Hold & Wait”

Denying Hold & Wait

Page 9: Op Sy 03 Ch 24

9

Ceng 334 - Operating Systems 2.4-9

• Advantages– It works

– Reasonably easy to code

• Problems– Resource wastage

– Possibility of starvation

Page 10: Op Sy 03 Ch 24

10

Ceng 334 - Operating Systems 2.4-10

Denying “No preemption”

• Implementation

– When a process is refused a resource request, it MUST release all other resources it holds

– Resources can be removed from a process before it is finished with them

Denying No Preemptions

Page 11: Op Sy 03 Ch 24

11

Ceng 334 - Operating Systems 2.4-11

• Advantages– It works– Possibly better resource utilisation

• Problems– The cost of removing a process's

resources– The process is likely to lose work it

has done. (How often does this occur?)

– Possibility of starvation

Page 12: Op Sy 03 Ch 24

12

Ceng 334 - Operating Systems 2.4-12

Denying “Circular Wait”

• Implementation

– Resources are uniquely numbered

– Processes can only request resources in linear ascending order

– Thus preventing the circular wait from occurring

Denying Circular Wait

Page 13: Op Sy 03 Ch 24

13

Ceng 334 - Operating Systems 2.4-13

• Advantages– It works– Has been implemented in some OSes

• Problems– Resources must be requested in ascending

order of resource number rather than as needed

– Resource numbering must be maintained by someone and must reflect every addition to the OS

– Difficult to sit down and write just write code

Page 14: Op Sy 03 Ch 24

14

Ceng 334 - Operating Systems 2.4-14

Deadlock Avoidance

• Allow the chance of deadlock occur

• But avoid it happening..

• Check whether the next state (change in system) may end up in a deadlock situation

Deadlock Aavoidance

Page 15: Op Sy 03 Ch 24

15

Ceng 334 - Operating Systems 2.4-15

Banker’s Problem

• Suppose total bank capital is 1000 MTL

• Current cash : 1000- (410+210) = 380 MTL

Customer

c1

c2

Max. Need

800

600

Present Loan

410

210

Claim

390

390

Banker’s Problem

Page 16: Op Sy 03 Ch 24

16

Ceng 334 - Operating Systems 2.4-16

Dijkstra's Banker's Algorithm

• Definitions

– Each process has a LOAN, CLAIM, MAXIMUM NEED• LOAN: current number of resources held

• MAXIMUM NEED: total number resources needed to complete

• CLAIM: = (MAXIMUM - LOAN)

Dijkstra’s Banker’s Algorithm

Page 17: Op Sy 03 Ch 24

17

Ceng 334 - Operating Systems 2.4-17

Assumptions

• Establish a LOAN ceiling (MAXIMUM NEED) for each process– MAXIMUM NEED < total number of resources

available (ie., capital)

• Total loans for a process must be less than or equal to MAXIMUM NEED

• Loaned resources must be returned back in finite time

Assumptions

Page 18: Op Sy 03 Ch 24

18

Ceng 334 - Operating Systems 2.4-18

Algorithm1. Search for a process with a claim that can

satisfied using the current number of remaining resources (ie., tentatively grant the claim)

2. If such a process is found then assume that it will return the loaned resources.

3. Update the number of remaining resources

4. Repeat steps 1-3 for all processes and mark them

Algorithms

Page 19: Op Sy 03 Ch 24

19

Ceng 334 - Operating Systems 2.4-19

• DO NOT GRANT THE CLAIM if at least one process can not be marked.

• Implementation

– A resource request is only allowed if it results in a SAFE state

– The system is always maintained in a SAFE state so eventually all requests will be filled

Page 20: Op Sy 03 Ch 24

20

Ceng 334 - Operating Systems 2.4-20

• Advantages– It works

– Allows jobs to proceed when a prevention algorithm wouldn't

• Problems– Requires there to be a fixed number of

resources

– What happens if a resource goes down?

– Does not allow the process to change its Maximum need while processing

Page 21: Op Sy 03 Ch 24

21

Ceng 334 - Operating Systems 2.4-21

Deadlock Detection

• Methods by which the occurrence of deadlock, the processes and resources involved are detected.

• Generally work by detecting a circular wait

• The cost of detection must be considered

• One method is resource allocation graphs

Deadlock Detection

Page 22: Op Sy 03 Ch 24

22

Ceng 334 - Operating Systems 2.4-22

Deadlock Recovery

• Recover from the deadlock by removing the offending processes

• The process being removed may lose work

Deadlock Recovery

Page 23: Op Sy 03 Ch 24

23

Ceng 334 - Operating Systems 2.4-23

Problems

• Most systems do not support the removal and then restarting of a process.

• Some processes should NOT be removed.

• It is possible to have deadlock involving tens or even hundreds of processes

Problems

Page 24: Op Sy 03 Ch 24

24

Ceng 334 - Operating Systems 2.4-24

• Implementation– Processes are simply killed off (lost

forever)– Usually some sort of priority order exists

for killing• Support for suspend/resume

– Some systems come with checkpoint/restart features

– Developers indicate a series of checkpoints when designing a software application

– So a process only need be rolled back to the last checkpoint, rather than back to the beginning

Page 25: Op Sy 03 Ch 24

25

Ceng 334 - Operating Systems 2.4-25

Question : What is the simplest and most used method to recover from a deadlock?

Re-Boot