Top Banner
CSE 325 : Operating Systems Spring 2014 Lecture 6 Spin-Lock and Deadlock 1 Md. Shamsujjoha
69

Lecture 6 deadlock

Jun 15, 2015

Download

Engineering

turjo987

for the students of CE325 in Eastwest university , and others if YOU need
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture 6 deadlock

CSE 325 :

Operating

Systems

Spring 2014

Lecture 6

Spin-Lock and

Deadlock

Page 2: Lecture 6 deadlock

Md. Shamsujjoha 2

Page 3: Lecture 6 deadlock

Md. Shamsujjoha 3

Definition

• A thread is deadlocked when it’s waiting for an event that can never occur– I’m waiting for you to clear the intersection, so I can proceed

• but you can’t move until he moves, and he can’t move until she moves, and she can’t move until I move

– Thread A is in critical section 1, waiting for access to critical section 2; thread B is in critical section 2, waiting for access to critical section 1

– I’m trying to book a vacation package to Tahiti – air transportation, ground transportation, hotel, side-trips. It’s all-or-nothing – one high-level transaction – with the four databases locked in that order. You’re trying to do the same thing in the opposite order.

Page 4: Lecture 6 deadlock

Md. Shamsujjoha 4

Four conditions must existfor deadlock to be possible

1. Mutual Exclusion

2. Hold and Wait

3. No Preemption

4. Circular Wait

We’ll see that deadlocks can be addressed by attacking any of these four conditions.

Page 5: Lecture 6 deadlock

Md. Shamsujjoha 5

Resource Graphs

• Resource graphs are a way to visualize the (deadlock-related) state of the threads, and to reason about deadlock

T1 T2 T3

Resources

Threads

• 1 or more identical units of a resource are available• A thread may hold resources (arrows to threads)• A thread may request resources (arrows from threads)

T4

Page 6: Lecture 6 deadlock

Md. Shamsujjoha 6

Deadlock

• A deadlock exists if there is an irreducible cycle in the resource graph (such as the one above)

Page 7: Lecture 6 deadlock

Md. Shamsujjoha 7

Graph reduction

• A graph can be reduced by a thread if all of that thread’s requests can be granted– in this case, the thread eventually will terminate – all

resources are freed – all arcs (allocations) to/from it in the graph are deleted

• Miscellaneous theorems (Holt, Havender):– There are no deadlocked threads iff the graph is completely

reducible– The order of reductions is irrelevant

Page 8: Lecture 6 deadlock

Md. Shamsujjoha 8

Resource allocation graph with no cycle

Silberschatz, Galvin and Gagne 2002

What would cause a deadlock?

Page 9: Lecture 6 deadlock

Md. Shamsujjoha 9

Resource allocation graph with a deadlock

Silberschatz, Galvin and Gagne 2002

Page 10: Lecture 6 deadlock

Md. Shamsujjoha 10

Resource allocation graph with a cyclebut no deadlock

Silberschatz, Galvin and Gagne 2002

Page 11: Lecture 6 deadlock

Md. Shamsujjoha 11

Handling Deadlock

• Eliminate one of the four required conditions– Mutual Exclusion

• Clearly we’re not going to eliminate this one!– Hold and Wait– No Preemption– Circular Wait

• Broadly classified as:– Prevention, or– Avoidance, or– Detection (and recovery)

Page 12: Lecture 6 deadlock

Md. Shamsujjoha 12

Prevention

Applications must conform to behaviors guaranteed not to deadlock

• Eliminating hold and wait• each thread obtains all resources at the beginning• blocks until all are available

• drawback?

• Eliminating circular wait• resources are numbered• each thread obtains resources in sequence order (which

could require acquiring some before they are actually needed)• why does this work?• pros and cons?

Page 13: Lecture 6 deadlock

Md. Shamsujjoha 13

Avoidance

Less severe restrictions on program behavior

• Eliminating circular wait– each thread states its maximum claim for every resource

type– system runs the Banker’s Algorithm at each allocation

request• Banker incredibly conservative• if I were to allocate you that resource, and then everyone were

to request their maximum claim for every resource, could I find a way to allocate remaining resources so that everyone finished?

– More on this in a moment…

Page 14: Lecture 6 deadlock

Md. Shamsujjoha 14

• Every once in a while, check to see if there’s a deadlock– how?

• If so, eliminate it– how?

Detect and recover

Page 15: Lecture 6 deadlock

Md. Shamsujjoha 15

Avoidance: Banker’s Algorithm example

• Background– The set of controlled resources is known to the system– The number of units of each resource is known to the

system– Each application must declare its maximum possible

requirement of each resource type

• Then, the system can do the following:– When a request is made

• pretend you granted it• pretend all other legal requests were made• can the graph be reduced?

– if so, allocate the requested resource– if not, block the thread until some thread releases resources, and

then try pretending again

Page 16: Lecture 6 deadlock

Md. Shamsujjoha 16

Pots

Pans

Me You

Max: 1 pot 2 pans

Max: 2 pots 1 pan

1. I request a pot

Page 17: Lecture 6 deadlock

Md. Shamsujjoha 17

Pots

Pans

Me You

Max: 1 pot 2 pans

Max: 2 pots 1 pan

Suppose we allocate, and then everyone requests their max? It’s OK; there is a way for me to complete, and then you can complete

pretend

Page 18: Lecture 6 deadlock

Md. Shamsujjoha 18

Pots

Pans

Me You

Max: 1 pot 2 pans

Max: 2 pots 1 pan

2. You request a pot

Page 19: Lecture 6 deadlock

Md. Shamsujjoha 19

Pots

Pans

Me You

Max: 1 pot 2 pans

Max: 2 pots 1 pan

Suppose we allocate, and then everyone requests their max? It’s OK; there is a way for me to complete, and then you can complete

pretend

Page 20: Lecture 6 deadlock

Md. Shamsujjoha 20

Pots

Pans

Me You

Max: 1 pot 2 pans

Max: 2 pots 1 pan

3a. You request a pan

Page 21: Lecture 6 deadlock

Md. Shamsujjoha 21

Pots

Pans

Me You

Max: 1 pot 2 pans

Max: 2 pots 1 pan

Suppose we allocate, and then everyone requests their max? NO! Both of us might be unable to complete!

pretend

Page 22: Lecture 6 deadlock

Md. Shamsujjoha 22

Pots

Pans

Me You

Max: 1 pot 2 pans

Max: 2 pots 1 pan

3b. I request a pan

Page 23: Lecture 6 deadlock

Md. Shamsujjoha 23

Pots

Pans

Me You

Max: 1 pot 2 pans

Max: 2 pots 1 pan

Suppose we allocate, and then everyone requests their max? It’s OK; there is a way for me to complete, and then you can complete

pretend

Page 24: Lecture 6 deadlock

Determination of a Safe State

Page 25: Lecture 6 deadlock

Determination of a Safe State

Page 26: Lecture 6 deadlock

Determination of a Safe State

Page 27: Lecture 6 deadlock

Safe to Grant P1’s Request?

• P1 requests one unit each of R1 and R3

Page 28: Lecture 6 deadlock

Safe to Grant P1’s Request?

Page 29: Lecture 6 deadlock

Determination of an Unsafe State

Page 30: Lecture 6 deadlock

In-Class Exercise

• Prob 6.15: consider a system consisting of four processes and a single resource. The current state of the claim and allocation matrices are– C = [3, 2, 9, 7]T

– A = [1, 1, 3, 2]T

What is the minimum number of units of the resource needed to be available for this state to be safe?

Page 31: Lecture 6 deadlock

• C = [3, 2, 9, 7]T

• A = [1, 1, 3, 2]T

• V = [?]

• C = [3, 2, 9, 7]T

• A = [1, 1, 3, 2]T

• R = [7] V = [0]• X Cannot make progress on any process! need at least 1 extra

instance of resource

Page 32: Lecture 6 deadlock

• C = [3, 2, 9, 7]T

• A = [1, 1, 3, 2]T

• R = [8] V = [1]

• Finish P2:• C = [3, 2, 9, 7]T

• A = [1, 0, 3, 2]T

• V = [2]

• Finish P1:• C = [3, 2, 9, 7]T

• A = [0, 0, 3, 2]T

• V = [3]• X Cannot make progress on any process!• need at least 2 extra instances of resource

Page 33: Lecture 6 deadlock

• C = [3, 2, 9, 7]T

• A = [0, 0, 3, 2]T

• R = [10] V = [5]

• Finish P4:• C = [3, 2, 9, 7]T

• A = [0, 0, 3, 0]T

• V = [7]

• Finish P3:• C = [3, 2, 9, 7]T

• A = [0, 0, 0, 0]T

• V = [10]

Page 34: Lecture 6 deadlock

Deadlock Avoidance

• Maximum resource requirement must be stated in advance• Processes under consideration must be independent; their

execution order must be unconstrained by any synchronization requirements

• There must be a fixed number of resources to allocate• No process may exit while holding resources

Page 35: Lecture 6 deadlock

Deadlock Detection

• Available vector

Page 36: Lecture 6 deadlock

Md. Shamsujjoha 36

Current practice

• Microsoft SQL Server– “The SQL Server Database Engine automatically detects

deadlock cycles within SQL Server. The Database Engine chooses one of the sessions as a deadlock victim and the current transaction is terminated with an error to break the deadlock.”

• Oracle– As Microsoft SQL Server, plus “Multitable deadlocks can

usually be avoided if transactions accessing the same tables lock those tables in the same order... For example, all application developers might follow the rule that when both a master and detail table are updated, the master table is locked first and then the detail table.”

Page 37: Lecture 6 deadlock

Md. Shamsujjoha 37

• Windows internals (Linux no different)– “Unless they did a huge change in Vista (and from what I've

heard they haven't modified this area), the NT kernel architecture is a deadlock minefield. With the multi-threaded re-entrant kernel there is plenty of deadlock potential.”

– “Lock ordering is great in theory, and NT was originally designed with mutex levels, but they had to be abandoned. Inside the NT kernel there is a lot of interaction between memory management, the cache manager, and the file systems, and plenty of situations where memory management (maybe under the guise of its modified page writer) acquires its lock and then calls the cache manager. This happens while the file system calls the cache manager to fill the cache which in turn goes through the memory manager to fault in its page. And the list goes on.”

Page 38: Lecture 6 deadlock

Md. Shamsujjoha 38

Summary

• Deadlock is bad!

• We can deal with it either statically (prevention) or dynamically (avoidance and/or detection)

• In practice, you’ll encounter lock ordering, periodic deadlock detection/correction, and minefields

Page 39: Lecture 6 deadlock

OVER VIEWINTRODUCTION OF DEADLOCK

• WHERE DEAD LOCK OCCURS • REASONS FOR DEADLOCK• DEADLOCK REPRESENTED BY GRAPH• ALOGORITHM FOR SOLVING DEADLOCK

Page 40: Lecture 6 deadlock

INTRODUCTON

• A deadlock consists of a set of blocked processes• each process holding a resource and waiting to

acquire a resource held by another process in the set• Example #1

– A system has 2 disk drives

– P1 and P2 each hold one disk drive and each needs the other one

Page 41: Lecture 6 deadlock

WHERE DEADLOCK OCCURES

• REAL LIFE• VIRTUAL LIFE

Page 42: Lecture 6 deadlock

DEAD LOCK IN REAL LIFE

Page 43: Lecture 6 deadlock

DEAD LOCK IN VIRTUAL LIFE

• @ r1

• @ r2

• p1

• p2

• p3

• @ @

• R3

Page 44: Lecture 6 deadlock

REASONS FOR DEADLOCK

• GENERALLY FOUR REASONS FOR DEADLOCK• 1. Mutual exclusion• 2.Hold and wait• 3. No preemption• 4. Circular wait

Page 45: Lecture 6 deadlock

MUTUAL EXCLUSION

• Mutual exclusion: At least one resource must be held in a non sharable mode;

• that is, only one process at a time can use the resource.

• If another process requests that resource, the requesting process must be delayed until the resource has been released.

Page 46: Lecture 6 deadlock

Mutual exclusion

• process

• resources• bloc

ked

• next

• request

• held by

• token

• release

Page 47: Lecture 6 deadlock

Hold and wait

• Hold and wait: • a process holding at least one resource is waiting to

acquire additional resources held by other processes

Page 48: Lecture 6 deadlock

HOLD AND WAIT

• Task

• A

• Resource• 1

• Resource• 2

• Task

• B

• Is waiting for

• holding

• Is waiting

for

• Is holding

Page 49: Lecture 6 deadlock

No preemption

• No preemption: Resources cannot be preempted.; • that is, a resource can be released only voluntarily by

the process holding it, after that process has completed its task.

Page 50: Lecture 6 deadlock

No preemption

• p1

• p2

• @ @ @

• resource1

• @ @resource

2

Page 51: Lecture 6 deadlock

Circular wait

• Circular wait: there exists a set {P0, P1, …, 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 52: Lecture 6 deadlock

Circular wait

• Resource x

• Resource y

• Process a

• Process b

• Waiting for x

• Waiting for y

Page 53: Lecture 6 deadlock

Discuss about reasons

• We emphasize that all four conditions must hold for a deadlock to occur.

• The circular-wait condition implies the hold-and-wait condition,

• so the four conditions are not completely independent

Page 54: Lecture 6 deadlock

Resource-Allocation Graph

A resource allocation graph contains a set of vertices V and a

set of edges E. • V is partitioned into two types:P = {P1, P2, …, Pn}, the set consisting of all

the processes in the system.R = {R1, R2, …, Rm}, the set consisting of all

resource types in the system.request edge – directed edge P1 Rj

assignment edge – directed edge Rj Pi

Page 55: Lecture 6 deadlock

•Resource-Allocation Graph (cont.)

• Process•  Pi

• Resource Type with 2 instances

• Pi requests instance of Rj

•  Pi

• Pi is holding an instance of Rj

• Pi

Page 56: Lecture 6 deadlock

•Example of a Resource Allocation Graph

• P2

• P1

• P3

• R1

• R2

• R3

Page 57: Lecture 6 deadlock

•Resource Allocation Graph With A Deadlock

• P3

• P2

• P1

• R1

• R2

• R3

Page 58: Lecture 6 deadlock

• Resource Allocation Graph With A Cycle But No Deadlock

• P4

• P3

• P2

• P1

• R1

• R2

• R3

Page 59: Lecture 6 deadlock

•Single process in a deadlock

• P1

• R1

• R

Page 60: Lecture 6 deadlock

Basic Facts

• If graph contains no cycles no deadlock.

• If graph contains a cycle – if only one instance per resource type, then

deadlock.– if several instances per resource type,

possibility of deadlock.

Page 61: Lecture 6 deadlock

Dead LocksStrategy

There are three methods to

Ignore Deadlocks:• Ensure deadlock never occurs using either Prevention• Prevent any one of the 4 conditions from happening.• Avoidance: Allow all deadlock conditions, but calculate

cycles about to happen and stop dangerous operations.• Allow deadlock to happen. This requires using both:• Detection: Know a deadlock has occurred.• Recovery: Regain the resources.

• HOW TO HANDLE DEADLOCKS – GENERAL STRATEGIES

• Most Operating system do this.

Page 62: Lecture 6 deadlock

Avoidance: Banker’s Algorithm example

• Background:– The set of controlled resources is known to the system– The number of units of each resource is known to the system– Each application must declare its maximum possible requirement

of each resource type

• Then, the system can do the following:– When a request is made

• The rule is simple: If a request allocation would cause an unsafe state, do not honor that request.

Page 63: Lecture 6 deadlock

DeadlockAvoidance

• NOTE: All deadlocks are unsafe, but all unsafe are NOT deadlocks.

• Only with luck will O.S. can avoid• processes avoid deadlock.

Deadlock.

Page 64: Lecture 6 deadlock

Safety Algorithm

• A method used to determine if a particular state is safe. • The algorithm uses these variables:• Need[I] – the remaining resource needs of each process.

• Work - Temporary variable – how many of the resource are currently available.

• Finish[I] – flag for each process showing we’ve analyzed that process or not.

• need <= available + allocated[0] + .. + allocated[I-1] <- Sign of success• Let work and finish be vectors of length m and n respectively.

Page 65: Lecture 6 deadlock

Safety Algorithm• 1. Initialize work = available• Initialize finish[i] = false, for i = 1,2,3,..n• 2. Find an i such that:• finish[i] == false and need[i] <= work• If no such i exists, go to step 4.• 3. work = work + allocation[i]• finish[i] = true• goto step 2• 4. if finish[i] == true for all i, then the

system is in a safe state.

Page 66: Lecture 6 deadlock

Example: Safety Algorithm• Max need= Allocated + can-be-

requested

• Is the

system

• in a safe

state?Alloc Req Avail

A B C A B C A B C

P0 0 1 0 7 4 3 3 3 2

P1 2 0 0 0 2 0

P2 3 0 2 6 0 0

P3 2 1 1 0 1 1

P4 0 0 2 4 3 1

Page 67: Lecture 6 deadlock

Example: Safety Algorithm• Produce the

state• chart as if the• request is

Granted• and see if it’s

safe.• (We’ve drawn

the• chart as if it’s

• Can the

request

• be grante

d?

Alloc Req Avail

A B C A B C A B C

P0 0 1 0 7 4 3 1# 3 0#

P1 3# 0 2# 0 2 0

P2 3 0 2 6 0 0

P3 2 1 1 0 1 1

P4 0 0 2 4 3 1

Page 68: Lecture 6 deadlock

Deadlock Recovery

• So, the deadlock has occurred. Now, how do we get the resources back and gain forward progress?

• PROCESS TERMINATION:• · Could delete all the processes in the deadlock -- this is expensive.• · Delete one at a time until deadlock is broken ( time consuming ).

• · In general, it's easier to preempt the resource, than to terminate the process.• RESOURCE PREEMPTION:• · Select a victim - which process and which resource to preempt.• · Rollback to previously defined "safe" state.• · Prevent one process from always being the one preempted ( starvation ).

Page 69: Lecture 6 deadlock

Dead LockSummary

• In this section we have:• Deadlock is bad!• Looked at necessary conditions for a deadlock to occur.• Determined how to prevent, avoid, detect and recover from

deadlocks.