Deadlocks (part II) Deadlock avoidance – (not reasonable)

Slides:



Advertisements
Similar presentations
Deadlock and Starvation
Advertisements

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.
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.
Ceng Operating Systems Chapter 2.4 : Deadlocks Process concept  Process scheduling  Interprocess communication  Deadlocks Threads.
Chapter 3 Deadlocks TOPICS Resource Deadlocks The ostrich algorithm
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Avishai Wool lecture Introduction to Systems Programming Lecture 5 Deadlocks.
Deadlock Chapter 3 R1 R2 P2P1 Allocated Requested.
1 Wednesday, June 28, 2006 Command, n.: Statement presented by a human and accepted by a computer in such a manner as to make the human feel that he is.
CS 450 OPERATING SYSTEMS DEADLOCKS Manju Muralidharan Priya.
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
1 Concurrency: Deadlock and Starvation Chapter 6.
Chapter 6 Concurrency: Deadlock and Starvation
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
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.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
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 软件学院 高海昌 Operating Systems Gao Haichang, Software School, Xidian University 22 Contents  1. Introduction** 
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Operating Systems (OS)
1 Deadlock. 2 Concurrency Issues Past lectures:  Problem: Safely coordinate access to shared resource  Solutions:  Use semaphores, monitors, locks,
CIS Operating Systems Deadlock Professor Qiang Zeng Fall 2015.
This Time - Deadlock Definition Conditions for deadlocks
Deadlock cs550 Operating Systems David Monismith.
CS 346 – Chapter 7 Deadlock –Properties –Analysis: directed graph Handle –Prevent –Avoid Safe states and the Banker’s algorithm –Detect –Recover.
Chapter 7 Deadlocks. 7.1 Introduction Deadlock Common approaches to deal with deadlock – Prevention – Avoidance – Detection and recovery.
Deadlocks.  Deadlocks: Occurs when threads are waiting for resources with circular dependencies Often involve nonpreemptable resources, which cannot.
Deadlocks Copyright ©: University of Illinois CS 241 Staff1.
Operating Systems Unit 4: – Dining Philosophers – Deadlock – Indefinite postponement Operating Systems.
Introduction to operating systems What is an operating system? An operating system is a program that, from a programmer’s perspective, adds a variety of.
Deadlock A deadlock is a situation wherein two or more competing actions are waiting for the other to finish, and thus neither ever does. Example : “When.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
MULTITHREADED PROGRAMMING Processes vs. Threads Process states and state transitions Hazards and Semaphores 1.
Deadlock CS Introduction to Operating Systems.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Deadlock.
NETW 3005 Monitors and Deadlocks. Reading For this lecture, you should have read Chapter 7. NETW3005 (Operating Systems) Lecture 06 - Deadlocks2.
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);
Sistem Operasi IKH311 Deadlock. 2 Resources Examples of computer resources printers tape drives tables Processes need access to resources in reasonable.
Deadlocks Lots of resources can only be used by one process at a time. Exclusive access is needed. Suppose process A needs R1 + R2 and B needs R1 + R2.
Process Management Deadlocks.
Concurrency: Deadlock and Starvation
Deadlocks (part II) Deadlock avoidance – (not reasonable)
ITEC 202 Operating Systems
G.Anuradha Ref:- Galvin
Chapter 6 : Deadlocks What is a Deadlock?
Operating Systems (CS 340 D)
Operating System: DEADLOCKS
Introduction to Operating Systems
Chapter 7 Deadlock.
Operating Systems: Deadlock
Deadlock avoidance Deadlock detection Resource Allocation Graphs
Deadlocks Definition 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.
Review: Readers-Writers Problem
Conditions for Deadlock
Andy Wang Operating Systems COP 4610 / CGS 5765
The relation between the process and
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Deadlocks Session - 13.
Chapter 7: Deadlocks.
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
SE350: Operating Systems Lecture 5: Deadlock.
OPERATING SYSTEMS DEADLOCKS.
Introduction to Deadlocks
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
CENG334 Introduction to Operating Systems
EECE.4810/EECE.5730 Operating Systems
Deadlock CSE 2431: Introduction to Operating Systems
Presentation transcript:

Deadlocks (part II) Deadlock avoidance – (not reasonable) Deadlock detection Resource Allocation Graphs Resource Pool and Counting Semaphores Dining Philosophers Problem Condition Variables

Resource Tables

Deadlock Definition A deadlock is a situation in which two or more competing actions are each waiting for the other to finish, and thus neither ever does. P1 P2 P(R1) P(R2) P(R2) P(R1) CS CS V(R2) V(R1) V(R1) V(R2)

Deadlock 4 necessary conditions Mutual Exclusion – Processes claim exclusive control of the resources they require Wait For – processes hold resources already allocated to them while waiting for additional resources No Pre-emption – resources cannot be removed from the process using them until it is used to completion Circular Wait – A circular chain of processes exist in which each process holds one or more resources that are requested by the next process in the chain.

Deadlock Avoidance (reasonable?) Designing an Operating System (Linux, Windows, iOS, Android etc.) that avoids deadlocks would require removing at least 1 of the 4 necessary conditions of deadlock. Many systems programmers would argue that the removal of any one of these 4 would limit the system too much and the gain of having a deadlock-free system would not be worth it. Just like driving a car, which can on (hopefully rare) occasions get a flat tire, it is popular to design systems that may on rare occasions go into deadlock and the system detects and deals with the problem at that time.

Deadlock Detection We need a way to detect that 2 or more processes are in a deadlock. Allow the system to run normally and occasionally have the Operating System check for deadlocks. If a deadlock is detected, the Operation System should attempt to fix the situation as best as it can which includes asking the users of a deadlocked process to volunteer it killing their process which will return resources to the system, which will then end the deadlock.

Asking the user to end Deadlock

Review of Data Structures and Algorithms Graphs can be recorded in memory Algorithms can be run on graphs that answer questions: What is the shortest path from point A to point B? Is a graph fully connected (yes or no)? Does a graph contain a cycle (yes or no)? If an existing graph which doesn’t currently have a cycle, was to have an edge added to it, would the graph now have a cycle?

Representing Graphs in Memory Adjacency matrix

Representing Graphs in Memory Adjacency lists

Deadlock (DAG with a cycle) A deadlock is a situation in which two or more competing actions are each waiting for the other to finish, and thus neither ever does. P1 P2 P(R1) P(R2) P(R2) P(R1) CS CS V(R2) V(R1) V(R1) V(R2)

Resource Pool A collection of identical resources Ex) A room full of printers Computer Storage (on the stack or heap)

Counting Semaphores When a system has multiple identical copies of a resource, a Counting Semaphore can be used to allow mutual access to that pool of identical resources. S=8 P(S) { if (S > 0) { S = S - 1; } else OS moves the process to the queue waiting for S Update resource tables and the Resource Allocation Graph

Counting Semaphores And the V function for counting semaphores is V(S) { if (a process is waiting for an S) OS moves the process to the ready queue } else S = S + 1; Update the resource tables and the RAG )

RAG – Resource Allocation Graph (with counting Semaphores)

Dijkstra’s Banker’s Algorithm – Safe State Total common resources is 8

Bankers Algorithm - 2 Resource Pools (not in a Safe State)

P(S) using Dijkstra’s Banker’s Algorithm { if (System would stay in a Safe State) { S = S - 1; } else OS moves the process to the queue waiting for S Update resource tables and the Resource Allocation Graph

Dining Philosophers Problem It was originally formulated in 1965 by Edsger Dijkstra as a student exam exercise, presented in terms of computers competing for access to tape drive peripherals. Soon after, Tony Hoare gave the problem its present formulation.

Dining philosophers solution??? while(1) { P(chopstick i); P(chopstick ((i + 1) %n); EAT V(chopstick i); V(chopstick ((i + 1) %n); THINK } In order to deadlock, - all 5 philosophers must be involved. - all 5 just have the right chopstick and want the left

Avoid Deadlock – Dining philosophers Only allow 4 (n-1) philosophers to sit at the table (only 5-way cycles an happen…add variable to count Eaters) Only allow a philosopher to get both or none of the chopsticks (don’t allow hold and wait) Odd philosophers get right chopstick first left second and even get left first and right second. Add a 6th emergency chopstick. The point is Deadlocks for some apps may be avoided on a case by case basis

Ex: Second Cashier for long lines A market has a full-time cashier than handles customers. If the line get too long, a second part-time cashier snaps into action and start serving customers. We need the full-time cashier (that always runs) to wake up the part-time cashier when the line get too long (let’s say 5 customers) Part-time cashier will wait for a signal from the full-time cashier.

Condition Variables The value of a variable will determine if your process can proceed or must wait. Ex) full-time cashier always runs and part-time cashier only runs if the condition (customerCount >= 5) if true. This variable (i.e. customerCount) is referred to as a condition variable. You need obtain a mutex to access a condition variable. Based on that variable’s current value, part-time cashier should continue or wait.

Waiting while releasing condition variable Question: You access the mutex for the condition variable, based on it’s value, you may determine that you should wait…how do you release the mutex and make yourself wait? Full-time Cashier process while(1) { P(cc) if (customerCount >= 5) V(c5); } V(cc) // Handle customers P(cc); customerCount–; V(cc); Part-time cashier process while(1) { P(cc) if (customerCount < 5) { P(c5); } V(cc); // Handle Customers P(cc); customerCount–; V(cc);

Condition Variables Question: You access the mutex for the condition variable, based on it’s value, you determine that you should wait…how do you release the mutex and make yourself wait? Part-time cashier process while(1) { P(cc) if (customerCount < 5) CP(cc,c5); // release CC and wait on C5 atomically } V(cc); // Handle Customers

CP(S,C) Conditional Atomic Operation void CP(S,C) { V(S); // release the binary semaphore P(C); // wait on the conditional variable }