Chapter 6 Concurrency Deadlock and Starvation CS 345

  • Slides: 39
Download presentation
Chapter 6 Concurrency: Deadlock and Starvation

Chapter 6 Concurrency: Deadlock and Starvation

CS 345 Stalling’s Chapter # Project 1: Computer System Overview 2: Operating System Overview

CS 345 Stalling’s Chapter # Project 1: Computer System Overview 2: Operating System Overview 4 P 1: Shell 3: Process Description and Control 4: Threads 4 P 2: Tasking 5: Concurrency: ME and Synchronization 6: Concurrency: Deadlock and Starvation 6 P 3: Jurassic Park 7: Memory Management 8: Virtual memory 6 P 4: Virtual Memory 9: Uniprocessor Scheduling 10: Multiprocessor and Real-Time Scheduling 6 P 5: Scheduling 11: I/O Management and Disk Scheduling 12: File Management 8 P 6: FAT Student Presentations 6 BYU CS 345 Concurrency 2

Learning Objectives… Learning Outcomes After completing this section, you should be able to n

Learning Objectives… Learning Outcomes After completing this section, you should be able to n n n Topics n n List and explain the conditions of deadlock. n Define deadlock prevention and describe deadlock prevention strategies related to each of the conditions for deadlock. n n n Explain the difference between deadlock prevention and deadlock avoidance. n Understand how an integrated deadlock strategy can be designed. n Analyze the dining philosophers problem. n n Resources Deadlock Joint Process Diagrams Deadlock Conditions Circular Wait Resource Allocation Graph Handling Deadlock Avoidance Detection Recovery Explain the concurrency and synchronization methods used in UNIX, Linux, Solaris, and Windows 7. BYU CS 345 Concurrency 3

Deadlock Quiz 6. 1 How could deadlock occur when n 200 K bytes of

Deadlock Quiz 6. 1 How could deadlock occur when n 200 K bytes of memory is available for allocation by the system Process 1 needs 140 K in 80 K, 60 K blocks Process 2 needs 150 k in 70 K, 80 K blocks Process 1 Process 2 Request 80 K bytes … Request 60 K bytes … … Request 70 K bytes … Request 80 K bytes Process 1 Process 2 Receive(P 2) … Send(P 2) … … Receive(P 1) … Send(P 1) How could deadlock occur when n Two processes need to communicate via send/receive messages Process 1 waits to hear from process 2 before sending data Process 2 proceeds after hearing from process 1 BYU CS 345 Concurrency 4

Resources Types of Resources n Reusable Resources n n n Used by one process

Resources Types of Resources n Reusable Resources n n n 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 Processor time, I/O channels, main and secondary memory, files, databases, and semaphores Deadlock occurs if each process holds one resource and requests the other Consumable Resources n n Created (produced) and destroyed (consumed) by a process 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 BYU CS 345 Concurrency 5

Deadlock Follow the Rules… n System Model (Rules) n n Process must request (and

Deadlock Follow the Rules… n System Model (Rules) n n Process must request (and be granted) a resource before using it. Process must release the resource when done. Why? ? Deadlock n A set of processes is in a deadlock state when every process in the set is waiting for an event that can only be caused by another process in the set. P 1: holds R 1, needs R 2 P 2: holds R 2, needs R 3 P 4: needs R 1 P 3: holds R 3, needs R 1 BYU CS 345 Concurrency 6

Deadlock Quiz 6. 2 1. What are the resources? 2. Where is mutual exclusion

Deadlock Quiz 6. 2 1. What are the resources? 2. Where is mutual exclusion needed? 3. What is required for deadlock to occur? BYU CS 345 Concurrency 7

Deadlock Quiz 6. 2 (solution) 1. What are the resources? 2. Where is mutual

Deadlock Quiz 6. 2 (solution) 1. What are the resources? 2. Where is mutual exclusion needed? 3. What is required for deadlock to occur? ME BYU CS 345 Concurrency 8

Diagrams Joint Process Diagram Progress of Q Impossible joint conditions are grayed out. 2

Diagrams Joint Process Diagram Progress of Q Impossible joint conditions are grayed out. 2 1 Release A A Required Both P and Q have A Release B Get A B Required 5 ? P has B Both P and Q Q has A have B Get B Deadlock is only inevitable if the joint progress of the two processes creates a path that enters the fatal region. BYU CS 345 6 Get A Get B 4 3 Release A Release B Progress of P A Required B Required Concurrency 9

Diagrams Joint Process Diagram Progress of Q 1 2 3 Release A A Required

Diagrams Joint Process Diagram Progress of Q 1 2 3 Release A A Required Release B 6 Both P and Q have A Get A Both P and Q have B B Required 5 Get B 4 No fatal region, as there are “exit” paths available. BYU CS 345 Get A Release A Get B A Required Concurrency Release B Progress of P B Required 10

Necessary Conditions of Deadlock n Necessary (but not sufficient) n Mutual exclusion – Everyone

Necessary Conditions of Deadlock n Necessary (but not sufficient) n Mutual exclusion – Everyone abides by the rules n n n only one process may use a resource at a time. no process may access resource allocated to another. Hold-and-wait n a process may hold allocated resources while awaiting assignment of other resources. No preemption n no resource can be forced to free a resource. Circular wait (sufficient) n n a closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain (consequence of the first three conditions) Other conditions are necessary but not sufficient for deadlock all four conditions must hold for deadlock - Unresolvable circular wait is the definition of deadlock! BYU CS 345 Concurrency 11

Circular Wait Resource A sts que Re Hel db Process P 1 y Process

Circular Wait Resource A sts que Re Hel db Process P 1 y Process P 2 Hel ests d. B u Req y Resource B BYU CS 345 Concurrency 12

Resource Allocation Graph R 1 P 1 Deadlocks can be described using resource allocation

Resource Allocation Graph R 1 P 1 Deadlocks can be described using resource allocation graph. R 2 P 2 Vertices: circles are Processes, rectangles are Resources. P 3 A directed edge from Pi to Rj indicates process Pi has requested an instance of resource Rj R 3 R 4 BYU CS 345 A directed edge from Rj to Pi indicates resource Rj has been allocated to process Pi Concurrency 13

Resource Allocation Graph R 1 P 1 R 2 P 2 Is there a

Resource Allocation Graph R 1 P 1 R 2 P 2 Is there a cycle (ie. some number of vertices connected in a closed chain)? P 3 If the graph contains a cycle, deadlock MAY exist. R 3 Is there deadlock? Yes R 4 BYU CS 345 If a graph contains no cycles, then no process in the system is deadlocked. Concurrency 14

Resource Allocation Graph Deadlock? P 2 Yes R 1 P 1 Is there a

Resource Allocation Graph Deadlock? P 2 Yes R 1 P 1 Is there a cycle? P 3 Is there deadlock? No R 2 P 4 BYU CS 345 Concurrency 15

Handling Deadlock n Four general approaches exist for dealing with deadlock. 1. Prevent deadlock

Handling Deadlock n Four general approaches exist for dealing with deadlock. 1. Prevent deadlock n by adopting a policy that eliminates one of the conditions. 2. Avoid deadlock n by making the appropriate dynamic choices based on the current state of resource allocation. 3. Detect Deadlock n by checking whether conditions 1 through 4 hold and take action to recover. 4. Ignore Deadlock n BYU CS 345 System may hang, so? ? Concurrency 17

Handling Deadlock 1. Prevent Deadlock n Eliminate Mutual Exclusion n n Use non-sharable resources

Handling Deadlock 1. Prevent Deadlock n Eliminate Mutual Exclusion n n Use non-sharable resources Eliminate Hold and wait n Guarantee that when a process requests a resource, it does not hold any other resources n n System calls requesting resources precede all others A process can only request resources when it has none Usually results in low utilization of resources Allow Preemption n If a process holds resources and requests more that cannot be allocated, all its other resources are preempted n n n BYU CS 345 If you can’t hold all, you can’t hold any Process is restarted only when it can have all Works for resources whose state can be easily saved and restored later such as registers or memory. Concurrency 18

Handling Deadlock 1. Prevent Deadlock n Eliminate Circular Wait n n Impose a total

Handling Deadlock 1. Prevent Deadlock n Eliminate Circular Wait n n Impose a total ordering of all resources (transitivity, antisymmetry) Require that all processes request resources in increasing order. Whenever a process requests a resource, it must release all resources that are lower With this rule, the resource allocation graph can never have a cycle. n May be impossible to find an ordering that satisfies everyone 1≡ 2≡ 3≡ 4≡ 5≡ BYU CS 345 Card reader Printer Plotter Tape drive Card punch Processes request resources whenever, but must be made in numerical order. A process may request first printer and then a tape drive (order: 2, 4), but not a plotter and then a printer (order: 3, 2). Concurrency 19

Avoidance 2. Avoid Deadlock n n Allow general requests, but grant only when safe

Avoidance 2. Avoid Deadlock n n Allow general requests, but grant only when safe Assume we know the maximum requests (claims) for each process n Process must state it needs n n Do not need to use its max claims n n n Ie. max of 5 A objects, 3 B objects, 2 C objects. Ie. Ok to set max=5 and only use 3 Can make requests at any time and in any order Process Initiation Denial n n n Track current allocations Assume all processes may make maximum requests at the same time Only start process if it can’t result in deadlock regardless of allocations BYU CS 345 Concurrency 20

Avoidance Resource Allocation Denial n Safe State – We can finish all processes by

Avoidance Resource Allocation Denial n Safe State – We can finish all processes by some scheduling sequence n n Banker’s Algorithm (Dijkstra) n n n Reject a request if it exceeds the processes’ declared maximum claims Grant a request if the new state would be safe Determining if a state is safe n n Example: Finish P 1, P 4, P 2, P 5, P 3 Find any process Pi for which we can meet it’s maximum requests n Don't forget already allocated resources Mark Pi as “done”, add its resources to available resource pool State is safe if we can mark all processes as “done” Block a process if the resources are not currently available or the new state is not safe BYU CS 345 Concurrency 21

Avoidance Banker’s Algorithm Example Claim P 1 P 2 P 3 P 4 A

Avoidance Banker’s Algorithm Example Claim P 1 P 2 P 3 P 4 A B C 3 6 3 4 2 1 1 2 2 3 4 2 Resource § Allocation P 1 P 2 P 3 P 4 A B C 9 3 6 A B C 1 5 2 0 0 1 1 2 Available C-A P 1 P 2 P 3 P 4 A B C 2 1 1 4 2 0 0 2 2 2 3 0 A B C 1 6 1 2 2 3 Are we in a safe state? Yes! BYU CS 345 Concurrency 22

Avoidance Quiz 6. 3 Carpentry Company XYZ has 4 employees, 9 clamps, 2 drills,

Avoidance Quiz 6. 3 Carpentry Company XYZ has 4 employees, 9 clamps, 2 drills, and 2 bottles of glue. Chair Desk Picture Frame Book Case BYU CS 345 4 clamps, 1 drill 6 clamps, 1 drill, 1 glue 4 clamps, 1 drill, 1 glue 6 clamps, 1 drill, 1 glue Concurrency 23

Quiz 6. 3 Claim P 1 P 2 P 3 P 4 Allotted Clamp

Quiz 6. 3 Claim P 1 P 2 P 3 P 4 Allotted Clamp Drill Glue 4 6 1 1 0 1 1 1 Resource P 1 P 2 P 3 P 4 Clamp Drill Glue 9 2 2 Clamp Drill Glue 1 2 2 2 0 0 1 0 0 Available Clamp Drill Glue 2 1 1 1. Are we in a safe state? 2. P 1 needs a drill. Is it OK to give it to him? 3. Then, P 4 needs a glue. Would you give it to him? BYU CS 345 Concurrency 24

Detection 3. Detect Deadlock n n n Avoidance methods tend to limit access to

Detection 3. Detect Deadlock n n n Avoidance methods tend to limit access to resources Instead, grant arbitrary requests and watch for deadlock Can vary how often we check n n Early detection vs. overhead of checks Algorithm (Stallings, Figure 6. 9) n n Preparation: n Create table of process requests, current allocations n Note available resources Mark processes with no resources Mark any process whose requests can be met (requests £ available resources) n Include resources from that process as ‘available’ (this process can finish) n If multiple processes available, pick any If any processes cannot be marked, they are part of a deadlock BYU CS 345 Concurrency 25

Detection Example Request P 1 P 2 P 3 P 4 Allocation A B

Detection Example Request P 1 P 2 P 3 P 4 Allocation A B C D E 0 0 0 1 1 0 0 0 0 1 1 P 1 P 2 P 3 P 4 Resource A B C D E 1 1 0 0 0 1 0 0 0 2 1 1 2 1 A B C D E Available A B C D E 0 0 1 Temporary Available 0 0 0 1 n § § Mark P 4 (not holding anything someone else wants) Mark P 3, new available: 0 0 0 1 1 Cannot mark P 1 or P 2, so we are deadlocked BYU CS 345 Concurrency 26

Detection Quiz 6. 4 Requests P 1 P 2 P 3 P 4 P

Detection Quiz 6. 4 Requests P 1 P 2 P 3 P 4 P 5 A B C 0 2 0 1 0 0 0 0 2 Allocation P 1 P 2 P 3 P 4 P 5 Temporary Available A B C 0 2 3 2 0 1 0 0 0 3 1 2 A B C 0 0 0 Resource A B C 7 2 6 Available A B C 0 0 0 Are we deadlocked? (Why or why not? ) BYU CS 345 Concurrency 27

Detection Quiz 6. 4 (solution) Requests P 1 P 2 P 3 P 4

Detection Quiz 6. 4 (solution) Requests P 1 P 2 P 3 P 4 P 5 A B C 0 2 0 1 0 0 0 0 2 Allocation P 1 P 2 P 3 P 4 P 5 Temporary Available A B C 0 2 3 2 0 1 0 0 0 3 1 2 A B C 5 7 3 0 2 1 0 4 6 3 0 Are we deadlocked? BYU CS 345 Resource A B C 7 2 6 Available A B C 0 0 0 No Concurrency 28

Detection Deadlock Detection Questions? n n Does it really work? How often should you

Detection Deadlock Detection Questions? n n Does it really work? How often should you run a detection process? How often is deadlock likely to occur? How expensive is the detection process? BYU CS 345 Concurrency 29

Recovery Deadlock Recovery n n Several possible approaches Abort all deadlocked processes n n

Recovery Deadlock Recovery n n Several possible approaches Abort all deadlocked processes n n Back up processes to a previously saved checkpoint, then restart n n Simple but common Assumes we have checkpoints and a rollback mechanism Runs risk of repeating deadlock n Assumes that the deadlock has enough timing dependencies it won’t happen Selectively abort processes until deadlock broken Preempt resources until deadlock broken n Must roll back process to checkpoint prior to acquiring key resource BYU CS 345 Concurrency 30

Recovery Deadlock Recovery n Process Termination n n n Kill them all One at

Recovery Deadlock Recovery n Process Termination n n n Kill them all One at a time Consider priority Time computing Who has most resources Resource Preemption n n Who gets preempted Do you consider process rollback and starvation BYU CS 345 Concurrency 31

Detection 4. Ignore Deadlock n n n Perhaps not as silly as it sounds

Detection 4. Ignore Deadlock n n n Perhaps not as silly as it sounds - since handling deadlock has a very high cost, it might be better to ignore it. Usually, the OS assumes that even if deadlock occurs, the loss of data will be minimal and the probability of occurrence of such a situation is very low. Use resource prioritization for heavily used resources, otherwise, don’t worry about deadlock. Handling deadlock requires an algorithm constantly running in background - is 20% of the CPU time worth trying to prevent an event with a probability of occurrence less than 0. 01%? If the alternative is to have a forced reboot once a year, that might be acceptable. When is ignoring deadlock unacceptable? BYU CS 345 Concurrency 32

Recovery Mixed Strategy n May group resources into classes and have different deadlock strategies

Recovery Mixed Strategy n May group resources into classes and have different deadlock strategies for each class: n n n Swap Space n Prevent deadlocks by requiring all space to be allocated at once n Avoidance also possible Tapes/Files n Avoidance can be effective here n Prevention by ordering resources also possible Main Memory n Preemption a good approach Internal Resources (channels, etc. ) n Prevention by ordering resources Can use linear ordering between classes BYU CS 345 Concurrency 33

Advantages/Disadvantages Approach Prevention Allocation Policy Conservative; under commits resources Scheme Advantages Disadvantages Requesting all

Advantages/Disadvantages Approach Prevention Allocation Policy Conservative; under commits resources Scheme Advantages Disadvantages Requesting all resources at once Works well for process that perform a single burst of activity No preemption necessary Inefficient Delays process initiation Future resource requirements must be known by processes Preemption Convenient when applied to resources whose state can be saved and restored easily Preempts more often than necessary Resource ordering Feasible to enforce via compile -time checks Disallows incremental Needs no run-time resource requests computation since problem is solved in system design Future resource requirements must be known by OS Processes can be blocked for long periods Avoidance Burden on system analysist; Midway between detection and prevention Manipulate to find at least one safe path Detection Very liberal; requested resources are granted where possible Invoke Never delays process initiation Inherent preemption losses periodically to Facilitates online handling test for deadlock Ignorance Extremely liberal; requested resources are always granted Infrequent deadlock acceptable BYU CS 345 No preemption necessary No CPU overhead Concurrency Not safety critical Inherent preemption losses 34

Dining Philosophers Quiz 6. 5 n n n 5 philosophers who only eat and

Dining Philosophers Quiz 6. 5 n n n 5 philosophers who only eat and think. Each need to use 2 forks for eating. There are only 5 forks. 1. Two philosophers are eating. Are we in a safe state? 2. Is deadlock possible? 3. How can deadlock be prevented? BYU CS 345 Concurrency 35

Dining Philosophers The Dining Philosophers Problem n n n 5 philosophers who only eat

Dining Philosophers The Dining Philosophers Problem n n n 5 philosophers who only eat and think. Each need to use 2 forks for eating. There are only 5 forks. Classical synchronization problem. Illustrates the difficulty of allocating resources among process without deadlock and starvation. BYU CS 345 Concurrency 36

Dining Philosophers Solution? ? Process Pi: repeat think; wait(forks[i]); wait(forks[(i+1)%5]); eat; signal(forks[(i+1)%5]); signal(forks[i]); forever

Dining Philosophers Solution? ? Process Pi: repeat think; wait(forks[i]); wait(forks[(i+1)%5]); eat; signal(forks[(i+1)%5]); signal(forks[i]); forever n n Each philosopher is a process. One semaphore per fork: n n forks: array[0. . 4] of semaphores Initialization: forks[i]. count: =1 for i: =0. . 4 • Deadlock if each philosopher starts by picking left fork! BYU CS 345 Concurrency 37

Dining Philosophers Another Solution n n A solution: admit only 4 philosophers at a

Dining Philosophers Another Solution n n A solution: admit only 4 philosophers at a time that tries to eat Then 1 philosopher can always eat when the other 3 are holding 1 fork Introduce semaphore T that limits to 4 the number of philosophers “sitting at the table” Initialize: T. count: =4 BYU CS 345 Process Pi: repeat think; wait(T); wait(forks[i]); wait(forks[(i+1)%5]); eat; signal(forks[(i+1)%5]); signal(forks[i]); signal(T); forever Concurrency 38

Dining Philosophers Other Solutions… n Buy more Forks n n Put fork down if

Dining Philosophers Other Solutions… n Buy more Forks n n Put fork down if 2 nd fork busy n n n Only let 4 of the philosophers into the room at once May have 4 philosophers in room, but only 1 can eat Left-Handed Philosophers (asymmetric solution) n n n “livelock” if philosophers stay synchronized Room Attendant n n Equivalent to increasing resources Grab forks in the other order (right fork, then left fork) Any mix will avoid deadlock (linear ordering on forks) A philosopher may only pick up forks in pairs. must allocate all resources at once n BYU CS 345 Concurrency 39

BYU CS 345 Concurrency 40

BYU CS 345 Concurrency 40