Deadlock and Starvation

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
ICS Principles of Operating Systems Lectures 8 and 9 - Deadlocks Prof. Dmitri V. Kalashnikov dvk ics.uci.edu Slides © Prof. Nalini Venkatasubramanian.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Lecture 6 :Deadlocks. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate with each other Involves.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Chapter 7: Deadlocks.
Chapter 6 Concurrency: Deadlock and Starvation
Chapter 6 Concurrency: Deadlock and Starvation
Chapter 7: Deadlocks. 7.2 Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks.
1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Concurrency: Deadlock & Starvation
1 Lecture 8: Deadlocks Operating System Spring 2008.
Witawas Srisa-an Chapter 6
CPSC 4650 Operating Systems Chapter 6 Deadlock and Starvation
1 Concurrency: Deadlock and Starvation Chapter 6.
Chapter 6 Concurrency: Deadlock and Starvation
Deadlocks Gordon College Stephen Brinton. Deadlock Overview The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 7: Deadlock Dr. Mohamed Hefeeda.
Concurrency: Deadlock and Starvation Chapter 6. Goal and approach Deadlock and starvation Underlying principles Solutions? –Prevention –Detection –Avoidance.
1 Concurrency: Deadlock and Starvation Chapter 6.
Concurrency: Deadlock and Starvation
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Deadlocks.
1 Announcements The fixing the bug part of Lab 4’s assignment 2 is now considered extra credit. Comments for the code should be on the parts you wrote.
Cosc 4740 Chapter 6, Part 4 Deadlocks. The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-6 Deadlocks Department of Computer Science and Software Engineering.
CHAPTER 8: DEADLOCKS System Model Deadlock Characterization
Dr. Kalpakis CMSC 421, Operating Systems Deadlocks.
Deadlock Operating Systems: Internals and Design Principles.
Deadlocks System Model RAG Deadlock Characterization
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 7 Operating Systems.
Chapter 8 Deadlocks. Objective System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 6 Deadlocks Slide 1 Chapter 6 Deadlocks.
Deadlocks Introduction to Operating Systems: Module 7.
Informationsteknologi Monday, October 1, 2007Computer Systems/Operating Systems - Class 111 Today’s class Deadlock.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
7.1 CSE Department MAITSandeep Tayal 7: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
Lecture 6 Deadlock 1. Deadlock and Starvation Let S and Q be two semaphores initialized to 1 P 0 P 1 wait (S); wait (Q); wait (Q); wait (S);. signal (S);
Chapter 7: Deadlocks. The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Concurrency: Deadlock and Starvation
ITEC 202 Operating Systems
G.Anuradha Ref:- Galvin
Day 17 Deadlock.
Day 18 Deadlock.
Chapter 7: Deadlocks.
Advanced Operating System Fall 2009
Operating System: DEADLOCKS
Chapter 7 Deadlock.
Chapter 7 Deadlocks.
Process Deadlocks.
Chapter 7: Deadlocks.
G.Anuradha Ref:- Galvin
Chapter 7: Deadlocks.
Deadlocks Session - 13.
DEADLOCK.
Chapter 7: Deadlocks.
CENG334 Introduction to Operating Systems
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Presentation transcript:

Deadlock and Starvation In-class quiz 17 Allow 15 minutes for students to do Midterm Survey on Angel

Deadlock Permanent blocking of a set of processes Processes are waiting on events, but the events can only be triggered by other blocked processes No simple, efficient solution

Bridge Crossing Example Traffic only in one direction. Each section of a bridge can be viewed as a resource. If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback). Several cars may have to be backed up if a deadlock occurs. Starvation is possible.

Can deadlock occur? Process P … Get A Get B Release A Release B …. Process Q … Get B Get A Release B Release A ….

Joint Progress Diagram (Fig.6.2) The occurrence of deadlock depends on both of the execution rates and the details of the application.

Can deadlock occur? Process P … Get A Release A Get B Release B …. Process Q … Get B Get A Release B Release A ….

Types of Resources Reusable vs. Consumable Preemptible vs Non-preemptible

Reusable resources Description: Example: 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 Example: Processor time, I/O channels, main and secondary memory, files, databases, and semaphores, printers Deadlock occurs if each process holds one resource and requests the other

Deadlock with Reusable Resources P1 Request disk Lock disk … Request tape Lock tape Unlock disk Unlock tape P1 Request tape Lock tape … Request disk Lock disk Unlock tape Unlock disk

Consumable Resources Description: Examples: Deadlock: Created (produced) and destroyed (consumed) by a process Examples: 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

Example of Deadlock with a Consumable Resource Deadlock occurs if at some point each is waiting to receive a message from the other, assuming Receive is blocking. Caused by a design error. Process P … Receive (Q) … Send (Q, message1) Process Q … Receive (P) … Send (P, message2)

Preemptible Resources Can be taken away (preempted) from the owning process with no ill effects Can be restored to the process at a later time Examples: memory processor Cannot cause deadlock

Nonpreemptible Resources Cannot be taken away from the owning process without adversely affecting its computation Examples: printer tape disk semaphore Can cause deadlock

Four Conditions for Deadlock Necessary policies Mutual exclusion must be enforced. No preemption – no resource can be forcibly removed from the process. Hold and Wait – a process may hold a resource while waiting for another. Sufficient circumstance Circular wait – closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain. Conditions 1-3 create a situation that could result in deadlock Condition 4 indicates that deadlock has occurred.

Resource Allocation Graphs A directed graph used to represent the current snapshot of a system, i.e. current allocations and pending requests. A circle represents a process A square represents a resource (can have more than one instance of a resource) A graph edge from a resource instance to a process implies that the resource instance is allocated to the process A graph edge from a process to a resource implies that the process is requesting an instance of the resource Can be used to determine if a system is deadlocked.

RAG Example with Deadlock Q2

RAG Example with No Deadlock

Basic Facts If graph contains no cycles If graph contains a cycle 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.

Approaches to Deadlock Handling Ostrich Approach Prevention Avoidance Detection and Recovery

Ostrich Approach Treat deadlock as a rare annoyance by ignoring it Used by most operating systems, including Windows and UNIX Stick your head in the sand and pretend that it never happened. Mathematicians and engineers respond to deadlocks differently. Mathematicians believe it is unacceptable. Engineers think about the severity of the problem, the frequency of the problem, and the cost associated with a solution.

Ostrich Approach Not Always Viable A real-time control system monitoring a gasoline refinery ensures the safe and proper operation of the refinery. An avoidance strategy or prevention strategy must be employed. A computerized pacemaker – can’t afford to miss a single beat deadlock avoidance or prevention must be employed Embedded computers – cell phones, PDAs. These are SoCs or system on chips i.e. everything is on one chip and SoCs are characterized by a small set of resources and real-time processing constraints. Can’t have a system administrator come in and decide which process to abort after deadlock has been detected. Either a prevention or avoidance strategy must be employed.

Deadlock Prevention Restrain ways a request can be made Indirect method: prevent any one of the three necessary conditions from occurring Mutual Exclusion No preemption Hold and Wait Direct method: prevent circular wait from occurring

Prevention – Indirect Method Option 1 – Don’t enfoce mutual exclusion Rarely an option Option 2 – Allow preemption Resource(s) must be preemptible Priority must be used to decide which process should be preempted.

Prevention – Indirect Method (cont.) Option 3 – Don’t allow “Hold and wait” One approach – a process must request all its resources at the same time. If it does not get all of them, then it must release all and block until all resources simultaneously available. Another approach – a process, if denied a resource, must release all of its previously allocated resources. If necessary, it must request them all again together with the next resource. Drawbacks: Processes can be held up for a long time waiting to acquire all resources Processes can hold on to resources they do not immediately need Processes may not know in advance all the resources they need

Prevention – Direct Method Prevent circular wait Organize resources into groups that are linearly ordered: G1 , G2 , G3 … Gn. A process can request a resource from G1 and then from G2 and then from G3 and so on, but not vice versa. Drawbacks Processes may have to request and hold on to resources that may be used much later, thus wasting resources. Processes must know in advance all the resources they need.

Deadlock Avoidance Two strategies: Process initiation denial Resource allocation denial Each process declares the maximum number of resources of each type that it may need Requires that system has a priori information The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands/claims of the processes

Process Initiation Denial The following conditions will apply All resources are either available or allocated. No process can claim more than the total amount of resources. No process can claim or hold more resources than it originally claimed. Start a process only if all the needs of the current processes and the new process can be met. Pessimistic approach as the assumption is that all processes will require their maximum claims at the same time.

Resource Allocation Denial: Bank Analogy Credit limit Allocated Ann $10000 $1000 Mark $6000 $2000 Judy $3000 $1000 Greg $6000 $1000 Bank had a total of $12,000. Current total = $7000. Should Ann’s request for $5000 be granted? If yes, amount remaining is $2000.

Resource Allocation Denial: Bank Analogy (cont.) Credit limit Allocated Ann $10000 $6000 Mark $6000 $2000 Judy $3000 $1000 Greg $6000 $1000 Bank had a total of $12,000. Current total = $2000. Can any other customer get her/his max credit limit? Judy can take the $2000 and finish, leaving $3000.

Resource Allocation Denial: Bank Analogy (cont.) Credit limit Allocated Ann $10000 $6000 Mark $6000 $2000 Greg $6000 $1000 Bank had a total of $12,000. Current total = $3000. No other customer can get her/his max credit limit According to Banker’s algorithm, Ann’s original request for $5000 should not have been granted.

Avoidance Using the Bankers algorithm A request is granted only if the request keeps the system in a safe state Otherwise, the process blocks A safe state is one which there is at least one sequence of resource allocations such that every process can claim its maximum resources and complete. An unsafe state does not imply deadlock, only the possibility of deadlock All processes do not request maximum claims. Even if they do, deadlock will occur depending on the sequence in which these demands are made.

Determination of a Safe State

Determination of a Safe State (cont.)

Determination of a Safe State (cont.)

Determination of a Safe State (cont.) Q3

Deadlock Detection and Recovery Construct a resource allocation graph If only one instance of each resource exists Circular wait implies deadlock If there are two or more instances of any resource Circular wait could mean deadlock Must reduce the RAG to see if deadlock exists

Recovery from Deadlock Abort all deadlocked processes. Back up each deadlocked process to some previously defined checkpoint and restart all processes. Widely used in current database systems. Possibility of the same situation occurring again. Successively abort all deadlocked processes, until deadlock no longer exists. Must decide which process to abort first. Successively pre-empt resources until deadlock no longer exists. Must decide which process to pre-empt from

Recovery from Deadlock (cont.) To choose which process to abort or to pre-empt a device from: Least amount of processor time consumed so far Least amount of output produced so far Most estimated remaining time Least total resources allocated so far Lowest priority

An Integrated Approach Group resources into a number of classes. Use linear ordering to prevent circular wait between classes. Within a class use an algorithm that is most appropriate for the class.

Dining Philosophers Five philosophers who alternately think and eat spaghetti/sushi Share a fork/chopstick with each neighbor Assume each philosopher enters, picks up left utensil, then right utensil, then eats Deadlock if all enter at once Figure 6.11, page 276

Dining Philosophers’ problem Solution 1: Show with an example why this solution is not a correct solution. semaphore fork[5] = {1,1,1,1,1}; Philosopher(int i){ while(true){ Think(); semwait(fork[i]); semwait(fork[(i+1)mod5]); eat(); semsignal(fork[(i+1)mod5]); semsignal(fork[i]); } int main(){ parbegin(Philosopher(0), Philosopher(2), Philosopher(3), Philosopher(4), Philosopher(1));

Solution 2: Show with an example why this solution is not a correct solution. semaphore x = 1; Philosopher(int i){ while(true){ Think(); semwait(x); pick up fork[i]; pick up fork[(i+1)mod5]; semsignal(x); eat(); place fork[i] back on the table; place fork[(i+1)mod5] back on the table; } int main(){ parbegin(Philosopher(1), Philosopher(2), Philosopher(3), Philosopher(4), Philosopher(5));

Solution 3: Provide a correct solution for the dining philosophers’ problem.

Solutions to Dining Philosophers’ Problem Buy more utensils Equivalent to increasing resources Put utensil down if 2nd utensil busy Can produce “livelock” if philosophers stay synchronized Room attendant Only let 4 of the philosophers into the room at once May have 4 philosophers in room, but only 1 can eat Invite some left-handed philosophers Grab forks in the other order (right fork, then left fork) Any mix will avoid deadlock (effective linear ordering on forks)