Docsity

CS 241 Spring 2007: Deadlock Handling Lecture Notes, Study notes of Computer Science

Partial preview of the text

Download CS 241 Spring 2007: Deadlock Handling Lecture Notes and more Study notes Computer Science in PDF only on Docsity!

CS 241 Spring 2007System Programming

Deadlock Handling^ Lecture 22^ Klara Nahrstedt

CS241 Administrative † Read Stallings Chapter 6 about Deadlock Handling † Regular Quiz 7 this week on Queuing and DeadlockHandling † Discussion Sessions this week (on Queuing and FileSystems – needed for LMP1) † LMP1 (Long Machine Problem) starts today^ †^ LMP1 is part of three major assignments (LMP1, LMP2, LMP3 whichtogether will build distributed file system services)^ †^ LMP1 will be split into two parts, PART I and overall LMP1 delivery.^ †^ You will need to deliver PART I by Wednesday, March 28 and thefinal overall LMP1 will be due by April 2. For each delivery you willreceive points.^ †^ LMP1 quiz will be on April 2.^ †^ The graders will provide some feedback on the Part I that shouldassist you in the overall LMP1 delivery.

A^ How deadlock occurs

B^ C

Deadlock Modeling

Resource Allocation Graph (multiple resource types)

Deadlock Prevention Prevention : design a system in such a way thatdeadlocks cannot occur, at least with respect to^ serially reusable resources Having rules so that a deadlock can NEVERhappen

Deadlock Prevention: Havender's Algorithms If any one of the conditions for deadlock (withreusable resources) is denied, deadlock isimpossible

Wait-for Condition Force each process to request all requiredresources at once. It cannot proceed until allresources have been acquired

While did not get all resources {^ P(mutex)^ Try to pick up all resources^ if did not get all resources put resources back^ V(mutex)^ wait a while and try again } Use resources P(mutex) Release all resources V(mutex

while need a resource^ {^ P(mutex)^ Test to see if resource is free^ if free, grab resource else release all resources^ V(mutex)^ Wait a while and try again^ }

Circular Wait Condition All resource types are numbered Processes must request resources in numericalorder if a resource of order N is held, the only resourceswhich can be requested must be of order > N

Deadlock Avoidance^ Avoidance : impose less stringent conditions thanfor prevention, allowing the possibility ofdeadlock, but sidestepping it as it approaches Banker algorithm^ Each process provides the maximum number ofresources for each type^ For each request, the system checks if granting thisrequest may lead to a deadlock in the worse case^ If yes, not granting the request^ If no, granting the request

Deadlock Avoidance^ The system needs to know the resource ahead of time^ Banker Algorithm (Dijkstra, 1965)^ Each customer tells banker the maximum number of resources itneeds^ Customer borrows resources from banker^ Customer returns resources to banker^ Customer eventually pays back loan^ Banker only lends resources if the system will be in a

safe^ state

after the loan Safe state^ - there is a lending sequence such that allcustomers can take out a loan Unsafe state^ - there is a possibility of deadlock

Example (Safe Sequence)^ Res. Alloc^ Max^ Need^

Available (Banker) Processes^ |^ A B^ | A B | A B |

A B --------------------------------------------------------------------------------------------------------------------- P1^3 0 5

2 3 6 5 P2^2 2 6

4 3 P3^1 3 9

8 5 P4^0 5 8

8 0 Sequence: †^ P1 can go^ (borrows from Banker A(2) and B(3), since Need.A(2) is less than Available A(6) andNeed.B(3) < Available.B(5). After lending, Available will be A(4), B(2)). After P1 finishes it releasesAllocated Resources A(3) and B(0) to the banker, and so the banker will have A(8) = A(6) + A(3) andB(5)=B(5)+B(0) available †^ P2 can go ( borrows from Banker A(4) and B(3)). After P2 finishes it releases the allocated resourceA(1) and B(3) to the banker and available will be A(11), B(7) †^ P3 can go^ (borrows from Banker A(8) and B(5), since Need.A(8) < Available.A(11) and Need.B(5) <Available.B(7)). After P3 finishes, it releases the allocated resources to the banker and Availableresources will be A(12) = A(11) + A(1) and B(10)=B(7+B(3) †^ P4 can go^ because P4 can borrow from Banker Need.A(8) < Available.A(12) andNeed.B(0)<Available.B(10). After P4 finishes, the resources are released, and Banker will haveavailable A(12) and B(15) †^ Hence, there exists a safe sequence with this snapshot of resources and processes. The safesequence is P1, P2, P3, P

Example (Unsafe State)

-^ let’s assume Max/Need

changes for the processes^ Res. Alloc^

Max^ Need^ Available (Banker) Processes^ |^ A B^ |^

A B |^ A B |^ A B
----------------------------------------------------------------------------------------------------- P1^3
P2^2
P3^1
P4^0

Sequence: †^ P1 can’t go^ because Need.B(6) > Available.B(5) †^ P2 can’t go^ because Need.A(7) > Available.A(6) †^ P3 can’t go^ because Need.A(8) > Available.A(6) †^ P4 can’t go^ because Need.A(8) > Available.A(6) †^ Hence, there is no safe sequence and this is snapshot is in “unsafe” state