Dining Philosophers and the Deadlock Concept. Announcements Homework 2 and Project 2 Design Doc due Today –Make sure to look at the lecture schedule to.

Slides:



Advertisements
Similar presentations
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Advertisements

Deadlocks Operating Systems (234123) Spring 2013 Deadlocks Dan Tsafrir (13/5/2013) Partially based on slides by Hagit Attiya OS (234123) - spring 2013.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Operating Systems Lecture Notes Deadlocks Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
1 Deadlock Solutions: Avoidance, Detection, and Recovery CS 241 March 30, 2012 University of Illinois.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
6. Deadlocks 6.1 Deadlocks with Reusable and Consumable Resources
MODERN OPERATING SYSTEMS Third Edition ANDREW S
CS 162 Discussion Section Week 4 9/ /4. Today’s Section ●Project administrivia ●Quiz ●Lecture Review ●Worksheet and Discussion.
02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying.
SPL/2010 Liveness And Performance 1. SPL/2010 Performance ● Throughput - How much work can your program complete in a given time unit? ● Example: HTTP.
Deadlock CS Introduction to Operating Systems.
Classic Synchronization Problems
Chapter 6 Concurrency: Deadlock and Starvation
Deadlock Avoidance CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
1 CSC 539: Operating Systems Structure and Design Spring 2005 Process deadlock  deadlock prevention  deadlock avoidance  deadlock detection  recovery.
Operating Systems Principles Process Management and Coordination Lecture 6: Deadlocks 主講人:虞台文.
Starvation and Deadlock
Avishai Wool lecture Introduction to Systems Programming Lecture 5 Deadlocks.
CSC 322 Operating Systems Concepts Lecture - 29: by
CS 162 Week 3 Discussion (2/13 – 2/14). Today’s Section Project Administrivia (5 min) Quiz (5 min) Review Lectures 6 and 7 (15 min) Worksheet and Discussion.
Deadlocks Detection and Avoidance Prof. Sirer CS 4410 Cornell University.
Ken Birman. Dining Philosophers A problem that was invented to illustrate some issues related to synchronization with objects Our focus here is on the.
Classical Problems of Concurrency
CS514: Intermediate Course in Operating Systems Professor Ken Birman Vivek Vishnumurthy: TA.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Deadlock CSCI 444/544 Operating Systems Fall 2008.
OS Spring 2004 Concurrency: Principles of Deadlock Operating Systems Spring 2004.
Deadlock Prevention CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Chapter 7 – Deadlock and Indefinite Postponement
OS Fall’02 Concurrency: Principles of Deadlock Operating Systems Fall 2002.
IPC and Classical Synchronization Problems
1 Concurrency: Deadlock and Starvation Chapter 6.
Deadlocks: Part I Prevention and Avoidance
7: Deadlocks1 DEADLOCKS EXAMPLES: "It takes money to make money". You can't get a job without experience; you can't get experience without a job. BACKGROUND:
CPS110: Deadlock Landon Cox February 5, Concurrency so far  Mostly tried to constrain orderings  Locks, monitors, semaphores  It is possible.
1 Concurrency: Deadlock and Starvation Chapter 6.
1 Deadlock Solutions CS 241 March 28, 2012 University of Illinois.
CS 153 Design of Operating Systems Spring 2015 Lecture 11: Scheduling & Deadlock.
Lecture 9 Page 1 CS 111 Online Deadlock What is a deadlock? A situation where two entities have each locked some resource Each needs the other’s locked.
Chapter 7 – Deadlock (Pgs 283 – 306). Overview  When a set of processes is prevented from completing because each is preventing the other from accessing.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Deadlock Detection and Recovery
CIS Operating Systems Deadlock Professor Qiang Zeng Fall 2015.
This Time - Deadlock Definition Conditions for deadlocks
CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
CS 346 – Chapter 7 Deadlock –Properties –Analysis: directed graph Handle –Prevent –Avoid Safe states and the Banker’s algorithm –Detect –Recover.
Operating Systems Lecture 4 Deadlock Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software Engineering.
CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.
Deadlocks: Part I Prevention and Avoidance. 2 Review: Dining Philosopher’s Dijkstra –A problem that was invented to illustrate a different aspect of communication.
Deadlocks.  Deadlocks: Occurs when threads are waiting for resources with circular dependencies Often involve nonpreemptable resources, which cannot.
CS3771 Today: Distributed Coordination  Previous class: Distributed File Systems Issues: Naming Strategies: Absolute Names, Mount Points (logical connection.
Deadlocks Copyright ©: University of Illinois CS 241 Staff1.
Operating System Concepts and Techniques Lecture 16 Deadlock and starvation-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques,
Operating Systems Unit 4: – Dining Philosophers – Deadlock – Indefinite postponement Operating Systems.
Deadlock CS Introduction to Operating Systems.
NETW 3005 Monitors and Deadlocks. Reading For this lecture, you should have read Chapter 7. NETW3005 (Operating Systems) Lecture 06 - Deadlocks2.
ICS Deadlocks 6.1 Deadlocks with Reusable and Consumable Resources 6.2 Approaches to the Deadlock Problem 6.3 A System Model –Resource Graphs –State.
Concurrency: Deadlock and Starvation
Deadlocks (part II) Deadlock avoidance – (not reasonable)
Introduction to Operating Systems
Chapter 5: Process Synchronization (Con’t)
OPERATING SYSTEMS DEADLOCKS
Liveness And Performance
CSc 552 Advanced Unix Process deadlock deadlock prevention
SE350: Operating Systems Lecture 5: Deadlock.
OPERATING SYSTEMS DEADLOCKS.
Operating Systems Principles Process Management and Coordination Lecture 6: Deadlocks 主講人:虞台文.
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

Dining Philosophers and the Deadlock Concept

Announcements Homework 2 and Project 2 Design Doc due Today –Make sure to look at the lecture schedule to keep up with due dates! Prelim coming up in two and a half weeks: –Thursday March 8 th, 7:30—9:00pm, 1½ hour exam –203 Phillips –Closed book, no calculators/PDAs/… –Bring ID –Topics: Everything up to (and including) Monday, March 5 th Lectures 1-18, chapters 1-9 (7 th ed) –Review Session: Scheduling either night of Monday, March 5th or Tuesday, March 6 th

So far… We’ve focused on two styles of communication Bounded buffer: sits between producers and consumers –Used widely in O/S to smooth out rate mismatches and to promote modularity Readers and writers –Models idea of a shared data object that threads read and sometimes update

Dining Philosophers A problem that was invented to illustrate a different aspect of communication Our focus here is on the notion of sharing resources that only one user at a time can own –Such as a keyboard on a machine with many processes active at the same time –Or a special disk file that only one can write at a time (bounded buffer is an instance)

Dining Philosopher’s Problem Dijkstra Philosophers eat/think Eating needs two forks Pick one fork at a time Idea is to capture the concept of multiple processes competing for limited resources

Rules of the Game The philosophers are very logical –They want to settle on a shared policy that all can apply concurrently –They are hungry: the policy should let everyone eat (eventually) –They are utterly dedicated to the proposition of equality: the policy should be totally fair

What can go wrong? Primarily, we worry about: –Starvation: A policy that can leave some philosopher hungry in some situation (even one where the others collaborate) –Deadlock: A policy that leaves all the philosophers “stuck”, so that nobody can do anything at all –Livelock: A policy that makes them all do something endlessly without ever eating!

Starvation vs Deadlock Starvation vs. Deadlock –Starvation: thread waits indefinitely Example, low-priority thread waiting for resources constantly in use by high-priority threads –Deadlock: circular waiting for resources Thread A owns Res 1 and is waiting for Res 2 Thread B owns Res 2 and is waiting for Res 1 –Deadlock  Starvation but not vice versa Starvation can end (but doesn’t have to) Deadlock can’t end without external intervention Res 2 Res 1 Thread B Thread A Wait For Wait For Owned By Owned By

A flawed conceptual solution # define N 5 Philosopher i (0, 1,.. 4) do { think(); take_fork(i); take_fork((i+1)%N); eat(); /* yummy */ put_fork(i); put_fork((i+1)%N); } while (true);

Coding our flawed solution? Shared: semaphore fork[5]; Init: fork[i] = 1 for all i=0.. 4 Philosopher i do { P(fork[i]); P(fork[i+1]); /* eat */ V(fork[i]); V(fork[i+1]); /* think */ } while(true); Oops! Subject to deadlock if they all pick up their “right” fork simultaneously!

Dining Philosophers Solutions Allow only 4 philosophers to sit simultaneously Asymmetric solution –Odd philosopher picks left fork followed by right –Even philosopher does vice versa Pass a token Allow philosopher to pick fork only if both available

One Possible Solution Introduce state variable –enum {thinking, hungry, eating} Philosopher i can set the variable state[i] only if neighbors not eating –(state[(i+4)%5] != eating) and (state[(i+1)%5]!= eating Also, need to declare semaphore self, where philosopher i can delay itself.

One possible solution Shared: int state[5], semaphore s[5], semaphore mutex; Init: mutex = 1; s[i] = 0 for all i=0.. 4 Philosopher i do { take_fork(i); /* eat */ put_fork(i); /* think */ } while(true); take_fork(i) { P(mutex); state[i] = hungry; test(i); V(mutex); P(s[i]); } put_fork(i) { P(mutex); state[i] = thinking; test((i+1)%N); test((i-1+N)%N); V(mutex); } test(i) { if(state[i] == hungry && state[(i+1)%N] != eating && state[(i-1+N)%N != eating) { state[i] = eating; V(s[i]); }

Solutions are less interesting than the problem itself! In fact the problem statement is why people like to talk about this problem! Rather than solving Dining Philosophers, we should use it to understand properties of solutions that work and of solutions that can fail!

Cyclic wait For example… consider a deadlock –Each philosopher is holding one fork –… and each is waiting for a neighbor to release one fork We can represent this as a graph in which –Nodes represent philosophers –Edges represent waiting-for

Cyclic wait

We can define a system to be in a deadlock state if –There exists ANY group of processes, such that –Each process in the group is waiting for some other process –And the wait-for graph has a cycle Doesn’t require that every process be stuck… even two is enough to say that the system as a whole contains a deadlock (“is deadlocked”)

What about livelock? This is harder to express –The issue is that processes may be active and yet are “actually” waiting for one-another in some sense –Need to talk about whether or not processes make progress –Once we do this, starvation can also be formalized These problems can be solved… but not today In CS414 we’ll limit ourselves to deadlock –Detection: For example, build a graph and check for cycles (not hard to do) –Avoidance – we’ll look at several ways to avoid getting into trouble in the first place!

Real World Deadlocks? Truck A has to wait for truck B to move Not deadlocked

Real World Deadlocks? Gridlock

Real World Deadlocks? Gridlock

The strange story of “priorité a droite” France has many traffic circles… –… normally, the priority rule is that a vehicle trying to enter must yield to one trying to exit –Can deadlock occur in this case? But there are two that operate differently –Place Etoile and Place Victor Hugo, in Paris –What happens in practice? In Belgium, all incoming roads from the right have priority unless otherwise marked, even if the incoming road is small and you are on a main road. –This is useful to remember. –Is the entire country deadlock-prone?

Testing for deadlock Steps –Collect “process state” and use it to build a graph Ask each process “are you waiting for anything”? Put an edge in the graph if so –We need to do this in a single instant of time, not while things might be changing Now need a way to test for cycles in our graph

Testing for deadlock How do cars do it? –Never block an intersection –Must back up if you find yourself doing so Why does this work? –“Breaks” a wait-for relationship –Illustrates a sense in which intransigent waiting (refusing to release a resource) is one key element of true deadlock!

Testing for deadlock One way to find cycles –Look for a node with no outgoing edges –Erase this node, and also erase any edges coming into it Idea: This was a process people might have been waiting for, but it wasn’t waiting for anything else –If (and only if) the graph has no cycles, we’ll eventually be able to erase the whole graph! This is called a graph reduction algorithm

Graph reduction example This graph can be “fully reduced”, hence there was no deadlock at the time the graph was drawn. Obviously, things could change later!

Graph reduction example This is an example of an “irreducible” graph It contains a cycle and represents a deadlock, although only some processes are in the cycle

What about “resource” waits? When dining philosophers wait for one- another, they don’t do so directly –Erasmus doesn’t “wait” for Ptolemy Instead, they wait for resources –Erasmus waits for a fork… which Ptolemy exclusively holds Can we extend our graphs to represent resource wait?

Resource-wait graphs We’ll use two kinds of nodes A process: P 3 will be represented as: A resource: R 7 will be represented as: –A resource often has multiple identical units, such as “blocks of memory” –Represent these as circles in the box Arrow from a process to a resource: “I want k units of this resource.” Arrow to a process: this process holds k units of the resource –P 3 wants 2 units of R

A tricky choice… When should resources be treated as “different classes”? –To be in the same class, resources do need to be equivalent “memory pages” are different from “forks” –But for some purposes, we might want to split memory pages into two groups The main group of forks. The extra forks –Keep this in mind next week when we talk about ways of avoiding deadlock. It proves useful in doing “ordered resource allocation”

Resource-wait graphs

Reduction rules? Find a process that can have all its current requests satisfied (e.g. the “available amount” of any resource it wants is at least enough to satisfy the request) Erase that process (in effect: grant the request, let it run, and eventually it will release the resource) Continue until we either erase the graph or have an irreducible component. In the latter case we’ve identified a deadlock

This graph is reducible: The system is not deadlocked

This graph is not reducible: The system is deadlocked

Comments It isn’t common for systems to actually implement this kind of test However, we’ll use a version of the resource reduction graph as part of an algorithm called the “Banker’s Algorithm” later in the week Idea is to schedule the granting of resources so as to avoid potentially deadlock states

Some questions you might ask Does the order in which we do the reduction matter? –Answer: No. The reason is that if a node is a candidate for reduction at step i, and we don’t pick it, it remains a candidate for reduction at step i+1 –Thus eventually, no matter what order we do it in, we’ll reduce by every node where reduction is feasible

Some questions you might ask If a system is deadlocked, could this go away? –No, unless someone kills one of the threads or something causes a process to release a resource –Many real systems put time limits on “waiting” precisely for this reason. When a process gets a timeout exception, it gives up waiting and this also can eliminate the deadlock –But that process may be forced to terminate itself because often, if a process can’t get what it needs, there are no other options available!

Some questions you might ask Suppose a system isn’t deadlocked at time T. Can we assume it will still be free of deadlock at time T+1? –No, because the very next thing it might do is to run some process that will request a resource… … establishing a cyclic wait … and causing deadlock