Course: Operating Systems Instructor: M...

21
M Umair http://www.m-umair.com Course: Operating Systems Instructor: M Umair

Transcript of Course: Operating Systems Instructor: M...

Page 1: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

M Umair – http://www.m-umair.com

Course: Operating Systems Instructor: M Umair

Page 2: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

Deadlock

M Umair – http://www.m-umair.com

Page 3: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

M Umair – http://www.m-umair.com

Under normal operation, a resource allocations proceed like this

1. Request a resource (suspend until available if necessary ).

2. Use the resource. 3. Release the resource.

Introduction

{ Ref: http://web.cs.wpi.edu/~cs3013/c07/lectures/Section07-Deadlocks.pdf }

Page 4: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

M Umair – http://www.m-umair.com

A set of processes is in a deadlocked state when every process in the set is waiting for an event that can be caused only by another process in the set.

Introduction

{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }

Page 5: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

M Umair – http://www.m-umair.com

Mutual exclusion One or more than one resource must be held by a process in a non-sharable (exclusive) mode.

Hold and Wait A process holds a resource while waiting for another resource held by other process.

No Preemption There is only voluntary release of a resource - nobody else can make a process give up a resource.

Circular Wait Process A waits for Process B waits for Process C .... waits for Process A.

The 4 necessary conditions

{ Ref: http://web.cs.wpi.edu/~cs3013/c07/lectures/Section07-Deadlocks.pdf }

Page 6: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

M Umair – http://www.m-umair.com

Resource Allocation Graph

{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }

Page 7: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

M Umair – http://www.m-umair.com

We can deal with the deadlock problem in one of three ways:

• We can use a protocol to prevent or avoid deadlocks, ensuring that the system will never enter a deadlocked state. • We can allow the system to enter a deadlocked state, detect it, and recover. • We can ignore the problem altogether and pretend that deadlocks never occur in the system.

Handling deadlock

{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }

Page 8: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

M Umair – http://www.m-umair.com

Mutual Exclusion The mutual-exclusion condition must hold for non-sharable resources. For example, a printer cannot be simultaneously shared by several processes. Sharable resources, in contrast, do not require mutually exclusive access and thus cannot be involved in a deadlock. Read-only files are a good example of a sharable resource. We cannot prevent deadlocks by denying the mutual-exclusion condition, because some resources are intrinsically nonsharable.

Deadlock Prevention

{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }

Page 9: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

M Umair – http://www.m-umair.com

Hold and Wait To ensure that the hold-and-wait condition never occurs One protocol that can be used requires each process to request and be allocated all its resources before it begins execution. Problems?

Solution: A process may request some resources and use them. Before it can request any additional resources, however, it must release all the resources that it is currently allocated.

Deadlock Prevention

{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }

Page 10: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

M Umair – http://www.m-umair.com

Deadlock Prevention

DVD Drive Hard Drive Printer

DVD Drive Hard Drive Printer

{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }

Problems? >Device hold >Starvation

Page 11: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

M Umair – http://www.m-umair.com

No Preemption If a process is holding some resources and requests another resource that cannot be immediately allocated to it (that is, the process must wait), then all resources the process is currently holding are preempted.

Deadlock Prevention

{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }

Page 12: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

M Umair – http://www.m-umair.com

Circular Wait Let R = {R1, R2, ..., Rm} be the set of resource types

Define a one-to-one function F: R→N, where N is the set of natural numbers.

A process can initially request any number of instances of a resource type —say, Ri . After that, the process can request instances of resource type Rj if and only if F(Rj ) > F(Ri ).

Proof ?

Deadlock Prevention

{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }

Page 13: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

M Umair – http://www.m-umair.com

Safe State

A state is safe if the system can allocate resources to each process (up to its maximum) in some order and still avoid a deadlock.

A safe state is not a deadlocked state. Conversely, a deadlocked state is an unsafe state. Not all unsafe states are deadlocks.

An unsafe state may lead to a deadlock.

Deadlock Avoidance

{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }

Page 14: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

M Umair – http://www.m-umair.com

Deadlock Avoidance

{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }

Process Sequence: P1, P0, P2 (Is it safe ?)

Total Resources: 12

Process Maximum Need

Currently Held

Current Need

Currently Free

P0 10 5 5 -

P1 4 2 2 -

P2 9 2 5 -

Total - 9 - 3

Page 15: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

M Umair – http://www.m-umair.com

The Banker’s Algorithm

A new process must declare the maximum number of instances of each resource type that it may need.

This number may not exceed the total number of resources in

the system System grants request only if the request will result into a safe state.

Deadlock Avoidance

{ Ref: http://www.cs.colostate.edu/~cs551/CourseNotes/Bankers.html}

Page 16: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

M Umair – http://www.m-umair.com

Banker Algorithm's Example - 1

Deadlock Avoidance

{ Ref: Modern Operating Systems, Andrew S. Tanenbaum }

A: 6k, B:5K, C:4k, D:7K deposits in bank. Bank maintain 10K cash in the branch

Page 17: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

M Umair – http://www.m-umair.com

Banker Algorithm's Example -2

Assume we have the following resources 5 tape drives 2 graphic displays 4 printers 3 disks

A vector representing our total resources

Total = (5, 2, 4, 3)

Deadlock Avoidance

{ Ref: http://www.cs.colostate.edu/~cs551/CourseNotes/Bankers.html}

Page 18: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

M Umair – http://www.m-umair.com

Deadlock Avoidance

{ Ref: http://www.cs.colostate.edu/~cs551/CourseNotes/Bankers.html}

Allocation [4,2,2,3]

Need

Available [1,0,2,0]

Total = (5, 2, 4, 3)

Allocation [4,2,2,3]

Page 19: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

M Umair – http://www.m-umair.com

Process Termination Abort all deadlocked processes.

→Running for some time -> Resource/Time →Results of partial computations -> Lost →Re-compute later -> Resource/Time

Abort one process at a time until the deadlock cycle is eliminated.

→Considerable overhead →What if process is updating a file ? →Deadlock-detection algorithm must be invoked to check the possibility of reoccurring deadlock.

Deadlock Recovery

{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }

The Problems

Page 20: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

M Umair – http://www.m-umair.com

Considerations for process termination

1. Priority of the process

2. How long the process has computed and how much longer the process will compute before completing its designated task 3. How many and what types of resources the process has used (whether the resources are simple to preempt) 4. How many more resources the process needs in order to complete 5. How many processes will need to be terminated 6. Whether the process is interactive or batch

Deadlock Recovery

{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }

Page 21: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/01/6.OS_Deadlock.pdf · M Umair – A set of processes is in a deadlocked state when every process

M Umair – http://www.m-umair.com

Resource Preemption Selecting a victim

Determine the order of preemption to minimize cost

Rollback Safe state rollback Restart it from that state Difficult to determine what a safe state is Keep information about the state of all running processes?

Starvation

How to ensure that resources will not always be preempted from the same process?

Deadlock Recovery

{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }