chapter 6 concurrency: deadlock and starvationforsati/cse410s15/lectures/chapter 6.pdfedited by rana...

71
Seventh Edition By William Stallings Edited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles

Upload: lamkhanh

Post on 30-Apr-2018

256 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Seventh Edition

By William StallingsEdited by Rana Forsati CSE410

Chapter 6 Concurrency: Deadlock

and Starvation

Operating Systems: Internals

and Design Principles

Page 2: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Announcements

• HW#5 has been posted and due date is next Wednesday [ Please try to finish before exam though :-) ]

• Exam review questions will be posted on Piazza

• Sample questions will be solved on Monday

• Topics for presentation on Monday if you are interested

Page 3: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Presentation• Processes and Threads in Windows • Processes and Threads in Linux • Memory Management in Linux and Windows • Concurrency Control in Java • Concurrency Mechanisms in Linux [Pipes, Messages, Semaphores, Signals, Shared

Memory] • Concurrency Mechanisms in Windows • I/O Management and File Systems

• Linux File System • Windows File System

• Mobile Operating Systems • Android (based on the Linux Kernel) from Google • iOS (previously known as iPhone OS) from Apple • Windows Phone from Microsoft

10-15 minutes presentation time

Page 4: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Outline

• Principles of deadlock • Deadlock prevention • Deadlock avoidance • Deadlock detection

Page 5: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

The Deadlock Problem (I)• The permanent blocking of a set of processes that either compete

for a resource or communicate each other

• prevent sets of concurrent processes from completing their tasks

Page 6: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

The Deadlock Problem (II)• A set of blocked processes each holding a resource and waiting to

acquire a resource held by another process in the set

• Example 1– System has 2 disk drives– P1 and P2 each hold one disk drive and each needs another one

• Example 2– semaphores A and B, both initialized to 1

P0 P1

wait (A); wait(B)wait (B); wait(A)

Page 7: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Deadlock and Dining Philosophers

- Consider a proposal in which each philosopher is instructed to behave as follows:

๏ think until the left fork is available; when it is, pick it up;๏ think until the right fork is available; when it is, pick it up;๏ when both forks are held, eat for a fixed amount of time;๏ then, put the right fork down;๏ then, put the left fork down;๏ repeat from the beginning.

This attempted solution fails because it allows the system to reach a deadlock state, in which no progress is possible [ philosophers will die :-) ].

Wikipedia: https://en.wikipedia.org/wiki/Dining_philosophers_problem

Page 8: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Deadlock

■ Involve conflicting needs for resources by two or more processes

■ Permanent: none of the process can continue execution

■ No efficient solution

Page 9: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Resource Categories• Reusable resource • Consumable resource

Page 10: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Reusable Resources• Used by one process at a time and not depleted by that use

• Processes obtain resources that they later release for reuse by other processes

• Processors, I/O channels, main and secondary memory, files, databases, and semaphores

• Deadlock occurs if each process holds one resource and requests the other

• Main focus in this course

Page 11: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Reusable Resource Deadlock

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

■ Deadlock occurs if both processes progress to their second request

P1. . .

. . .Request 80 Kbytes;

Request 60 Kbytes;

P2 . . .

. . .Request 70 Kbytes;

Request 80 Kbytes;

Page 12: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Consumable Resources• Created (produced) and destroyed (consumed) by a

process

• No limit on the number of consumable resources of one

type

• Interrupts, signals, messages, and information in I/O

buffers

• Deadlock may occur if a Receive message is blocking

• May take a rare combination of events to cause deadlock

Page 13: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Resource Allocation Graphs

deadlock deadlock-free

Page 14: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Four Conditions for Deadlock

• Mutual exclusion – only one process may use a resource at a time

• Hold-and-wait – A process may hold allocated resources while awaiting assignment

of other resources • No preemption

– No resource can be forcibly removed from a process holding it • Circular waiting

- given that the first three conditions exist, a sequence of events may occur that lead to an unresolvable circular wait

Deadlocks can arise if four conditions hold simultaneously:

The first three conditions are necessary but not sufficient for a deadlock to exist. For deadlock to actually take place, circular waiting is required!

Page 15: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Circular Waiting– The circular waiting condition is, actually, a

potential consequence of the first three, and taken together, constitute necessary and sufficient conditions for deadlock

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

– Circular wait: there exists a chain {P0, P1, …,PN, P0} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource that is held by P0.

Page 16: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Ensure that the system will never enter a deadlock state

• Deadlock Prevention methods• Deadlock Avoidance methods

Allow the system to enter a deadlock state and then recover

• Deadlock Detection method

Dealing with Deadlock

Page 17: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Dealing with Deadlock• Deadlock Prevention

– Adopting a policy that eliminates one of the conditions [aggressive]

• Deadlock Avoidance – Making appropriate dynamic choices based on the

current state of resource allocation [conservative]

• Deadlock Detection – Detect deadlock and take action to recover [reasonable]

Page 18: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Outline

• Principles of deadlock • Deadlock prevention • Deadlock avoidance • Deadlock detection

Page 19: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Deadlock Prevention Strategy■ Design a system in such a way that the possibility of deadlock is

excluded.

■ Two main methods:■ Indirect■ prevent the occurrence of one of the three necessary

conditions■ Direct■ prevent the occurrence of a circular wait

Page 20: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Deadlock Prevention

• Elimination of Mutual Exclusion: if access to a resource requires mutual exclusion then it must be supported by the OS. – Cannot be disallowed

• Elimination of Hold and Wait – Can be prevented by

• A process requests all the required resources at one time • Blocking the process until all requests can be granted simultaneously

– Problems: • Inefficient • A process maynot know in advance all resources it will require.

Page 21: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

■ Elimination of No Preemption■ If a process holding certain resources is denied a further

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

■ If a process requests a resource held by another process, OS may preempt the second process and require it to release its resources

■ Only works when resource state can be saved and restored later.

Deadlock Condition Prevention

Page 22: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

• Elimination of 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.

– May be inefficient, slowing down processes and denying resource access unnecessarily

Deadlock Condition PreventionWith this rule, the

resource allocation graph can never have a cycle.

1 ≡ Card reader 2 ≡ Printer 3 ≡ Plotter 4 ≡ Tape drive 5 ≡ Card punch

Processes can request resources whenever they want to, but all requests must be made in numerical order.

aggressive and is inefficient.

Page 23: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Outline

• Principles of deadlock • Deadlock prevention • Deadlock avoidance • Deadlock detection

Page 24: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Deadlock Avoidance■ Can we guarantee that deadlocks will never occur?

■ Allow the three necessary conditions but makes judicious choices to assure that the deadlock point is never reached

■ A decision is made dynamically whether the current resource allocation request will, if granted, potentially lead to a deadlock

■ Avoidance allows more concurrency than prevention.

■ Requires knowledge of future process requests– only works when resource state can be saved and restored

later.

Page 25: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

■ Referred to as the banker’s algorithm

■ Consider a system with a fixed number of processes and a fixed number of resources. At any time a process may have zero or more resources allocated to it.

■ When a process requests an available resource, system must decide if immediate allocation leaves the system in a 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.

■ Safe state is where there is at least one sequence of resource allocations to all processes that does not result in a deadlock

■ We are considering a worst-case situation here. Even in the worst case (process requests up their maximum at the moment), we don’t have deadlock in a safe state.

■ Unsafe state is a state that is not safe

Deadlock Avoidance

Page 26: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Safe State• More formally: A system state is safe if there exists a safe sequence of all

processes (<P1, P2, …, Pn>) such that for each Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j < i

• That is:– If Pi resource needs are not immediately available, then Pi can wait until

all Pj have finished

– When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate

– When Pi terminates, Pi +1 can obtain its needed resources, and so on.

Page 27: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Resource Allocation Denial

SAFEDEADLOCK

UNSAFE

Only with luck will processes avoid deadlock.

O.S. can avoid deadlock.

• If system is in safe state ⇒ no deadlock• If system is in unsafe state ⇒ possibility of

deadlock• Avoidance ⇒ ensure that system will never

enter an unsafe state

Page 28: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

The State of System• There are n processes and m resources, and OS knows:

๏ Number of available instances of each resource ๏ For each process, current amount of each resource it owns๏ For each process, maximum amount of each resource it might ever need

declared in advance

Page 29: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Deadlock Avoidance:Assumptions

■ When a request is done by a process for some resource(s): check before allocating resource(s); if it will leave the system in an unsafe state, then do not allocate the resource(s); process is waited and resources are not allocated to that process.

■ The system knows the complete sequence of requests and releases for each process

■ The system decides for each request whether or not the process should wait in order to avoid a deadlock

■ Each process declare the maximum number of resources of each type that it may need in future

■ The system should always be at a safe state

■ Safe state → no deadlock

Page 30: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

■ Key idea: ensure that the system of processes and resources is always in a safe state

■ Mechanism: when a process makes a request for a set of resources■ Assume that the request is granted■ Update the system state accordingly■ Determine if the result is a safe state■ If so, grant he request; if not, block the process until it is safe to

grant the request

Banker’s Algorithm

Page 31: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Date Structure for Banker’s Algorithm

• Resource or Existing-- vector representing the total number of each type of resource in the system

• Available -- vector representing the number of each type of resource not allocated to any process

• Claim or Max -- matrix representing the maximum number of each type of resource that each process may request

• Allocation -- matrix representing the number of each type of resource currently allocated to each process

• Need -- matrix representing the difference between the Claim and Allocation matrices

• Request -- vector representing a request for additional resources from a given process

Page 32: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

An example system state

AllocationA B C

P0 0 1 0

P1 2 0 0

P2 3 0 2

P3 2 1 1

P4 0 0 2

Need A B C

P0 7 4 3

P1 1 2 2

P2 6 0 0

P3 0 1 1

P4 4 3 1

AvailableA B C

3 3 2

MaxA B C

P0 7 5 3

P1 3 2 2

P2 9 0 2

P3 2 2 2

P4 4 3 3

Need = Max - Allocation

Existing or Resource

A B C

10 5 7 system state at some t (may change)

All resourcesin the system

Page 33: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

NotationX

A B C

P0 0 1 0

P1 2 0 0

P2 3 0 2

P3 2 1 1

P4 0 0 2

X is a matrix.

Xi is the ith row of the matrix: it is a vector. For example, X3 = [2 1 1]

Ex: compare V with Xi

V

Xi

VA B C

3 3 2V is a vector; V = [3 3 2]

V == Xi ? V <= Xi ? Xi <= V ? ….

Ex: Compare [3 3 2] with [2 2 1][2 2 1] <= [3 3 2]

Compare two vectors:

Page 34: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Resource-Request Algorithm for Process Pi

Request: request vector for process Pi. If Requesti[j] == k, then process Pi wants k instances of resource type Rj

Algorithm

1. If Requesti ≤ Needi go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim

2. If Requesti ≤ Available, go to step 3. Otherwise Pi must wait, since resources are not available

Page 35: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Resource-Request Algorithm for Process Pi

• 3. Pretend to allocate requested resources to Pi by modifying the state as follows:

Available = Available – Requesti;Allocationi = Allocationi + Requesti;

Needi = Needi – Requesti;

Run the Safety Check Algorithm:• If safe ⇒ the requested resources are allocated to Pi • If unsafe ⇒ The requested resources are not allocated to Pi. Pi must wait. The old resource-allocation state is restored.

Page 36: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Determination of a Safe StateInitial State

■ State of a system consisting of four processes and three resources

■ Allocations have been made to the four processes

Page 37: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

P2 Runs to Completion

Page 38: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

P1 Runs to Completion

Page 39: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

P3 Runs to Completion

Thus, the state defined originally is a safe state

Page 40: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Example (I) of Banker’s Algorithm

• We have the following system state:

AllocationA B C

P0 0 1 0

P1 2 0 0

P2 3 0 2

P3 2 1 1

P4 0 0 2

Need A B C

P0 7 4 3

P1 1 2 2

P2 6 0 0

P3 0 1 1

P4 4 3 1

AvailableA B C

3 3 2

MaxA B C

P0 7 5 3

P1 3 2 2

P2 9 0 2

P3 2 2 2

P4 4 3 3

Is it a safe state?

Need = Max - Allocation

Page 41: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

AllocationA B C

P0 0 1 0

P1 2 0 0

P2 3 0 2

P3 2 1 1

P4 0 0 2

Need A B C

P0 7 4 3

P1 1 2 2

P2 6 0 0

P3 0 1 1

P4 4 3 1

AvailableA B C

3 3 2

Try to find a row in Needi that is <= Available. P1. run completion. Available becomes = [3 3 2] + [2 0 0] = [5 3 2] P3. run completion. Available becomes = [5 3 2] + [2 1 1] = [7 4 3] P4. run completion. Available becomes = [7 4 3] + [0 0 2] = [7 4 5] P2. run completion. Available becomes = [7 4 5] + [3 0 2] = [10 4 7] P0. run completion. Available becomes = [10 4 7] + [0 1 0] = [10 5 7]

We found a sequence of execution: P1, P3, P4, P2, P0. State is safe

Example (I) of Banker’s Algorithm

Page 42: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Example: P1 requests (1,0,2)

• At that time Available is [3 3 2] • First check that Request ≤ Available (that is, (1,0,2) ≤ (3,3,2) ⇒ true. • Then check the new state for safety:

AllocationA B C

P0 0 1 0

P1 3 0 2

P2 3 0 2

P3 2 1 1

P4 0 0 2

Need A B C

P0 7 4 3

P1 0 2 0

P2 6 0 0

P3 0 1 1

P4 4 3 1

AvailableA B C

2 3 0

MaxA B C

P0 7 5 3

P1 3 2 2

P2 9 0 2

P3 2 2 2

P4 4 3 3

new state (we did not go to that state yet; we are just checking)

Page 43: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Example: P1 requests (1,0,2)

AllocationA B C

P0 0 1 0

P1 3 0 2

P2 3 0 2

P3 2 1 1

P4 0 0 2

Need A B C

P0 7 4 3

P1 0 2 0

P2 6 0 0

P3 0 1 1

P4 4 3 1

AvailableA B C

2 3 0

new state

Can we find a sequence? Run P1. Available becomes = [5 3 2] Run P3. Available becomes = [7 4 3] Run P4. Available becomes = [7 4 5] Run P0. Available becomes = [7 5 5] Run P2. Available becomes = [10 5 7]

Sequence is: P1, P3, P4, P0, P2 Yes, New State is safe. We can grant the request. Allocate desired resourcesto process P1.

Page 44: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

P4 requests (3,3,0)?

AllocationA B C

P0 0 1 0

P1 3 0 2

P2 3 0 2

P3 2 1 1

P4 0 0 2

Need A B C

P0 7 4 3

P1 0 2 0

P2 6 0 0

P3 0 1 1

P4 4 3 1

AvailableA B C

2 3 0

Current state

If this is current state, what happens if P4 requests (3 3 0)?

There is no available resource to satisfy the request. P4 will be waited.

Page 45: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

P0 requests (0,2,0)? Should we grant?

AllocationA B C

P0 0 1 0

P1 3 0 2

P2 3 0 2

P3 2 1 1

P4 0 0 2

Need A B C

P0 7 4 3

P1 0 2 0

P2 6 0 0

P3 0 1 1

P4 4 3 1

AvailableA B C

2 3 0

Current state

System is in this state. P0 makes a request: [0, 2, 0]. Should we grant.

Page 46: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

P0 requests (0,2,0)? Should we grant?

Assume we allocate 0,2,0 to P0. The new state will be as follows.

AllocationA B C

P0 0 3 0

P1 3 0 2

P2 3 0 2

P3 2 1 1

P4 0 0 2

Need A B C

P0 7 2 3

P1 0 2 0

P2 6 0 0

P3 0 1 1

P4 4 3 1

AvailableA B C

2 1 0

New stateIs it safe?

No process has a row in Need matrix that is less than or equal to Available. Therefore, the new state would be UNSAFE. Hence we should not go to the new state. The request is not granted. P0 is waited.

Page 47: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Example(II) of Banker’s Algorithm

Page 48: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Example(II) of Banker’s Algorithm

Page 49: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Example(II) of Banker’s Algorithm

Page 50: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Example(II) of Banker’s Algorithm

Page 51: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Banker’s Algorithm

n: integer # of processes m: integer # of resources

available[1..m] - available[i] is # of avail resources of type i claim[1..n,1..m] - max demand of each Pi for each Ri allocation[1..n,1..m] - current allocation of resource Rj to Pi need[1..n,1..m] max # resource Rj that Pi may still request

let request[i] be vector of # of resource Rj Process Pi wants

Page 52: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Banker’s Algorithm1. If request[i] > need[i] then

error (asked for too much)

2. If request[i] > available[i] then wait (can’t supply it now)

3. Resources are available to satisfy the request Let’s assume that we satisfy the request. Then we would have: available = available - request[i] allocation[i] = allocation [i] + request[i] need[i] = need [i] - request [i] Now, check if this would leave us in a safe state: if yes, grant the request, if no, then leave the state as is and cause process to wait.

Page 53: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Deadlock Avoidance Advantages

■ It is not necessary to preempt and rollback processes, as in deadlock detection

■ It is less restrictive than deadlock prevention

Page 54: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Assume we have the following resources:

5 tape drives 2 graphic displays 4 printers 3 disks

Example (III)

We can create a vector representing our total resources: Total = (5, 2, 4, 3).

Page 55: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Example(III)

Process Name Tape Drives Graphics Printers Disk Drives Process A 2 0 1 1 Process B 0 1 0 0 Process C 1 0 1 1 Process D 1 1 0 1

Consider we have already allocated these resources among four processes as demonstrated by the following matrix

Process Name Tape Drives Graphics Printers Disk Drives Process A 1 1 0 0 Process B 0 1 1 2 Process C 3 1 0 0 Process D 0 0 1 0

We also need a matrix to show the number of each resource still needed for each process;

Available = (1, 0, 2, 0).

Question: Show that the sequence <D, A, B, C> safe state?

Page 56: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Deadlock Avoidance ?

Restrictions on its use: • Maximum resource requirement must be stated in

advance • Processes under consideration must be

independent; no synchronization requirements • There must be a fixed number of resources to

allocate • No process may exit while holding resources

Page 57: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Outline

• Principles of deadlock • Deadlock prevention • Deadlock avoidance • Deadlock detection

Page 58: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Deadlock Strategies

Deadlock prevention strategies are very conservative • limit access to resources by imposing restrictions on

processes

Deadlock detection strategies do the opposite• resource requests are granted whenever possible

Page 59: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Deadline Detection Algorithms

• Do not limit resource access or restrict process actions

• Requested resources are granted to processes whenever possible

• OS periodically performs detection algorithm – Algorithm similar to that for detecting safe state can be used

for deadlock detection as well. – If deadlock is detected, it is fixed by OS!

Page 60: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Deadline Detection Algorithms

▪ A check for deadlock can be made as frequently as each resource request or, less frequently, depending on how likely it is for a deadlock to occur

▪Advantages:▪ it leads to early detection▪ the algorithm is relatively simple

▪Disadvantage▪ frequent checks consume considerable processor time

Page 61: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Date Structure for Detection Algorithm

• Resource -- vector representing the total number of each type of resource in the system

• Available -- vector representing the number of each type of resource not allocated to any process

• Allocation -- matrix representing the number of each type of resource currently allocated to each process

• Request – matrix representing the current request of each process

Page 62: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Example

Marked = {F, F, F, F}; Available = (0, 0, 1); W is initialized to (0, 0, 1)

R1 R2 R3

P1 1 1 1

P2 2 1 2

P3 1 1 0

P4 1 1 1

R1 R2 R3

P1 3 2 1

P2 2 2 1

P3 0 0 1

P4 1 1 1

Allocation A Request Q

Q(P3) = (0, 0, 1) <= (0, 0, 1) = W

F: unmarked T: marked

Page 63: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Example

Marked = {F, F, T, F}; W = (1, 1, 1) = (0, 0, 1) + (1, 1, 0)

R1 R2 R3

P1 1 1 1

P2 2 1 2

P3 1 1 0

P4 1 1 1

R1 R2 R3

P1 3 2 1

P2 2 2 1

P3

P4 1 1 1

Allocation A Request Q

Page 64: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Example

Marked = {F, F, T, T}; W = (2, 2, 2) = (1, 1, 1) + (1, 1, 1);

R1 R2 R3

P1 1 1 1

P2 2 1 2

P3 1 1 0

P4 1 1 1

R1 R2 R3

P1 3 2 1

P2 2 2 1

P3

P4

Allocation A Request Q

Page 65: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Example

Marked = {F, T, T, T}; W = (4, 3, 2) = (2, 2, 2) + (2, 1, 2);

R1 R2 R3

P1 1 1 1

P2 2 1 2

P3 1 1 0

P4 1 1 1

R1 R2 R3

P1 3 2 1

P2

P3

P4

Allocation A Request Q

Page 66: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Example

Marked = {T, T, T, T}; W = (5, 4, 3) = (4, 3, 2) + (1, 1, 1);

R1 R2 R3

P1 1 1 1

P2 2 1 2

P3 1 1 0

P4 1 1 1

R1 R2 R3

P1

P2

P3

P4

Allocation A Request Q

Page 67: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Detection Algorithm

Page 68: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

■ Abort all deadlocked processes

■ Back up each deadlocked process to some previously defined checkpoint and restart all processes

■ Successively abort deadlocked processes until deadlock no longer exists

■ Successively preempt resources/processes until deadlock no longer exists

“Successively” means an order is followed: least amount of CPU time consumed, lowest priority, least total resources allocated so far, etc.

Recovery StrategiesOnce deadlock has been detected, some strategy is needed for recovery. The following are possible approaches, listed in order of increasing sophistication:

Page 69: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

Selection Criteria Deadlocked Processes

• Least amount of processor time consumed so far • Least number of lines of output produced so far • Most estimated time remaining • Least total resources allocated so far • Lowest priority

Page 70: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

An Integrated Deadlock Strategy

It might be more efficient for OS to use different strategies in different situations. One approach:

• Group resources into a number of different resource classes • Use linear ordering strategy defined previously for the prevention

of circular wait to prevent deadlocks between resources classes • Within a resource class, use the algorithm that is most appropriate

for that class

Page 71: Chapter 6 Concurrency: Deadlock and Starvationforsati/cse410s15/Lectures/Chapter 6.pdfEdited by Rana Forsati CSE410 Chapter 6 Concurrency: Deadlock and Starvation ... • Exam review

■ Deadlock:■ the blocking of a set of processes that either compete for

system resources or communicate with each other■ blockage is permanent unless OS takes action■ may involve reusable or consumable resources

■ Consumable = destroyed when acquired by a process■ Reusable = not depleted/destroyed by use

■ Dealing with deadlock:■ prevention – guarantees that deadlock will not occur■ detection – OS checks for deadlock and takes action■ avoidance – analyzes each new resource request

Summary