Operating Systems COMP 4850/CISG 5550 Deadlock Avoidance Dr. James Money.

32
Operating Systems Operating Systems COMP 4850/CISG 5550 COMP 4850/CISG 5550 Deadlock Avoidance Deadlock Avoidance Dr. James Money Dr. James Money

Transcript of Operating Systems COMP 4850/CISG 5550 Deadlock Avoidance Dr. James Money.

Operating SystemsOperating SystemsCOMP 4850/CISG 5550COMP 4850/CISG 5550

Deadlock AvoidanceDeadlock Avoidance

Dr. James MoneyDr. James Money

Introduction to DeadlocksIntroduction to Deadlocks

• Deadlocks are formally defined byDeadlocks are formally defined by– A set of processes is deadlocked if each A set of processes is deadlocked if each

process in the set is waiting for an event process in the set is waiting for an event that only another process in the set can that only another process in the set can causecause

• Since they are all waiting, none of them Since they are all waiting, none of them will wake upwill wake up

• Assumption of no interrupts and single Assumption of no interrupts and single threadsthreads

Conditions for DeadlockConditions for Deadlock

1.1. Mutual Exclusion – each resource is either Mutual Exclusion – each resource is either currently assigned to one process or is currently assigned to one process or is availableavailable

2.2. Hold and Wait – processes currently holding Hold and Wait – processes currently holding resources can request new resourcesresources can request new resources

3.3. No preemption – Resources previously granted No preemption – Resources previously granted cannot forcibly be taken away from the cannot forcibly be taken away from the process. They must be released by the processprocess. They must be released by the process

4.4. Circular Wait – there must be a circular chain Circular Wait – there must be a circular chain of 2+ processes, each whom is waiting for a of 2+ processes, each whom is waiting for a resource held by the next member of the resource held by the next member of the chainchain

Conditions for DeadlockConditions for Deadlock

• All four conditions must exist for a All four conditions must exist for a deadlock to occurdeadlock to occur

• If one is absent, deadlock cannot If one is absent, deadlock cannot occuroccur

• Many of these are related to system Many of these are related to system policy choicespolicy choices

Dealing with DeadlocksDealing with Deadlocks

• Ignore the problem, maybe it will Ignore the problem, maybe it will ignore you?ignore you?– Used by UNIX and WindowsUsed by UNIX and Windows

• Detection and RecoveryDetection and Recovery• Dynamic avoidance by careful Dynamic avoidance by careful

resource allocationresource allocation• Prevention by structurally negating Prevention by structurally negating

one of the four conditions for one of the four conditions for deadlocksdeadlocks

Deadlock AvoidanceDeadlock Avoidance

• In deadlock detection, we assumed all In deadlock detection, we assumed all resources were requested resources were requested simultaneouslysimultaneously

• However, in reality, we request them However, in reality, we request them one at a timeone at a time

• The system must decide if granting The system must decide if granting the resource is safe or notthe resource is safe or not

• We consider careful resource We consider careful resource allocation nowallocation now

Resource TrajectoriesResource Trajectories

• The main algorithm is based on the The main algorithm is based on the idea of safe statesidea of safe states

• We first consider a graphic version of We first consider a graphic version of this model firstthis model first

• The does not immediately turn into The does not immediately turn into an algorithm, but provide a good an algorithm, but provide a good intuition into the problemintuition into the problem

Resource TrajectoriesResource Trajectories

• The following slide shows a model for The following slide shows a model for dealing with two processes and two dealing with two processes and two resourcesresources

• The horizontal axis represents number The horizontal axis represents number of instructions executed for process Aof instructions executed for process A

• The vertical axis represents number of The vertical axis represents number of instructions executed for process Binstructions executed for process B

Resource TrajectoriesResource Trajectories

• At IAt I11, A requests a printer and at I, A requests a printer and at I22, A , A requests a plotterrequests a plotter

• The printer and plotter are released The printer and plotter are released at Iat I33 and I and I44, respectively, respectively

• Process B needs the plotter from IProcess B needs the plotter from I55 to to II77 and the printer from I and the printer from I66 to I to I88

Resource TrajectoriesResource Trajectories

Resource TrajectoriesResource Trajectories

• Every point in the picture represents Every point in the picture represents a joint state of the two processesa joint state of the two processes

• Initially, the state is Initially, the state is pp, with nothing , with nothing having been executedhaving been executed

• If the scheduler runs A first, then we If the scheduler runs A first, then we get to point get to point qq

• Then process B runs, and we get to Then process B runs, and we get to rr

Resource TrajectoriesResource Trajectories

• When A cross the line for IWhen A cross the line for I11, it , it requests and it granted the printerrequests and it granted the printer

• When B reaches When B reaches tt, it requests the , it requests the plotterplotter

• The shaded regions are of particular The shaded regions are of particular interest for deadlocksinterest for deadlocks

Resource TrajectoriesResource Trajectories

• The slanted lines from southwest to The slanted lines from southwest to northeast is when both processes northeast is when both processes have the printerhave the printer

• The slanted lines from northwest to The slanted lines from northwest to southeast is when both processes southeast is when both processes have the plotterhave the plotter

• Both of these are deadlock regions Both of these are deadlock regions because of mutual exclusionbecause of mutual exclusion

Resource TrajectoriesResource Trajectories

• If the system enters the box bounded by IIf the system enters the box bounded by I11, , II22, I, I55, and I, and I66, it will eventually deadlock , it will eventually deadlock when it reaches the intersection of Iwhen it reaches the intersection of I22 and I and I66

• The entire box is unsafeThe entire box is unsafe

• At point At point tt, the only safe course of action is , the only safe course of action is to run process A until it gets to Ito run process A until it gets to I44

• Any trajectory outside of this box to Any trajectory outside of this box to uu will will dodo

Resource TrajectoriesResource Trajectories

• The important thing to notice at point The important thing to notice at point tt, is , is that process B is requesting a resourcethat process B is requesting a resource

• The system must decide to grant it or notThe system must decide to grant it or not

• If it is granted, it enters an unsafe region If it is granted, it enters an unsafe region and a possible deadlockand a possible deadlock

• To avoid this, we should suspect process A To avoid this, we should suspect process A until is requests and releases the plotteruntil is requests and releases the plotter

Safe and Unsafe StatesSafe and Unsafe States

• We will use the vectors and matrices We will use the vectors and matrices from deadlock detectionfrom deadlock detection

• A state is said to be A state is said to be safesafe if it is not if it is not deadlocked and there is some deadlocked and there is some scheduling order so that each scheduling order so that each process can run to completion even if process can run to completion even if they requests their maximum they requests their maximum number of resources immediatelynumber of resources immediately

Safe and Unsafe StatesSafe and Unsafe States

Safe and Unsafe StatesSafe and Unsafe States

Safe and Unsafe StatesSafe and Unsafe States

• The prior example is safe since there The prior example is safe since there is a sequence of allocations that is a sequence of allocations that allows the processes to completeallows the processes to complete

• Now, let us consider an unsafe Now, let us consider an unsafe exampleexample

Safe and Unsafe StatesSafe and Unsafe States

Safe and Unsafe StatesSafe and Unsafe States

• So, the decision to go from (a) to (b) in the So, the decision to go from (a) to (b) in the prior slide moves us from a safe state to prior slide moves us from a safe state to an unsafe statean unsafe state

• We should have not granted process A’s We should have not granted process A’s request to prevent a possible deadlockrequest to prevent a possible deadlock

• Note: An unsafe state is not necessarily a Note: An unsafe state is not necessarily a deadlock!deadlock!

• Only a safe state guarantees all processes Only a safe state guarantees all processes will finishwill finish

• In an unsafe state, it may or may not finishIn an unsafe state, it may or may not finish

Banker’s Algorithm for Single Banker’s Algorithm for Single ResourceResource

• The scheduling algorithm for handling The scheduling algorithm for handling single resources is due to Dijkstra(1965) single resources is due to Dijkstra(1965) and is known as the and is known as the banker’s algorithmbanker’s algorithm

• It is an extension of the deadlock It is an extension of the deadlock detection algorithmdetection algorithm

• It is modeled similar to the way a small It is modeled similar to the way a small town banker deals with customers town banker deals with customers whom he has given a line of creditwhom he has given a line of credit

Banker’s Algorithm for Single Banker’s Algorithm for Single ResourceResource

• The algorithm checks to see if granting a The algorithm checks to see if granting a resource leads to a safe or unsafe stateresource leads to a safe or unsafe state

• The banker gives out the various credit The banker gives out the various credit limits, which add up to 22limits, which add up to 22

• However, s/he can only lend out 10 units However, s/he can only lend out 10 units at a timeat a time

• The units can be tape drives, the The units can be tape drives, the customers are processes and the banker is customers are processes and the banker is the OSthe OS

Banker’s Algorithm for Single Banker’s Algorithm for Single ResourceResource

Banker’s Algorithm for Single Banker’s Algorithm for Single ResourceResource

• In (b), the state is safeIn (b), the state is safe

• In (c) is unsafeIn (c) is unsafe

• In (b), if anyone but C requests a In (b), if anyone but C requests a resource, it can be delayed until C is resource, it can be delayed until C is finishedfinished

• (c) does not have to result in a (c) does not have to result in a deadlock, but we want to avoid this deadlock, but we want to avoid this statestate

Banker’s Algorithm for Single Banker’s Algorithm for Single ResourceResource• The algorithm considers each request as it The algorithm considers each request as it

occurs and checks to see if it leads to a safe occurs and checks to see if it leads to a safe statestate

• If it does, the request is grantedIf it does, the request is granted• If it does not, the request is postponedIf it does not, the request is postponed• To check safety, we see if we have enough To check safety, we see if we have enough

resources to satisfy some processresources to satisfy some process• The resources are released, and the next The resources are released, and the next

closest customer of the limit is checked, and closest customer of the limit is checked, and so onso on

• All processes must be able to finish to be safeAll processes must be able to finish to be safe

Banker’s Algorithm for Multiple Banker’s Algorithm for Multiple ResourcesResources

• We can now generalize the banker’s We can now generalize the banker’s algorithm for multiple resourcesalgorithm for multiple resources

• This time we use a matrix of This time we use a matrix of assigned and request resources assigned and request resources similar to beforesimilar to before

Banker’s Algorithm for Multiple Banker’s Algorithm for Multiple ResourcesResources

Banker’s Algorithm for Multiple Banker’s Algorithm for Multiple ResourcesResources1.1. Look at a row, Look at a row, RR, whose unmet resource , whose unmet resource

needs are smaller than or equal to needs are smaller than or equal to A(R<=A). If no row exists, the system will A(R<=A). If no row exists, the system will eventually deadlockeventually deadlock

2.2. Assume the process of the chosen row Assume the process of the chosen row requests its resources and finishes. Mark requests its resources and finishes. Mark the process as terminated and add its the process as terminated and add its resources to vector Aresources to vector A

3.3. Repeat 1 and 2 until either all the processes Repeat 1 and 2 until either all the processes are marked as terminated, which means the are marked as terminated, which means the state is safe, or until a deadlock occurs, state is safe, or until a deadlock occurs, which means the state is unsafewhich means the state is unsafe

Banker’s Algorithm for Multiple Banker’s Algorithm for Multiple ResourcesResources

• The current state is safe in the figureThe current state is safe in the figure

• Suppose process B requests a Suppose process B requests a scannerscanner– This is granted since the resulting state This is granted since the resulting state

is safeis safe– Process D, then process A or E finishes, Process D, then process A or E finishes,

followed by the restfollowed by the rest

Banker’s Algorithm for Multiple Banker’s Algorithm for Multiple ResourcesResources

• After B is granted one of the two After B is granted one of the two remaining scanners, suppose E wants remaining scanners, suppose E wants the last printerthe last printer– This reduces A=(1 0 0 0)This reduces A=(1 0 0 0)– This leads to a potential deadlockThis leads to a potential deadlock– This request must be deferredThis request must be deferred

Houston, we have a Houston, we have a problem!problem!

• This has been highly studiedThis has been highly studied

• However, it suffers from a major However, it suffers from a major flaw:flaw:– It is uselessIt is useless– It needs to know the total resource It needs to know the total resource

needs of a program in advanceneeds of a program in advance– In addition, the number of processes is In addition, the number of processes is

dynamicdynamic