Lecture 8 Restrictions on Resource Allocation Dining Philosophers - Havender's Linear Ordering.

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Deadlock Prevention, Avoidance, and Detection
ICS Principles of Operating Systems Lectures 8 and 9 - Deadlocks Prof. Dmitri V. Kalashnikov dvk ics.uci.edu Slides © Prof. Nalini Venkatasubramanian.
Deadlock and Starvation
Operating Systems Lecture Notes Deadlocks Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Deadlock Prevention, Avoidance, and Detection.  The Deadlock problem The Deadlock problem  Conditions for deadlocks Conditions for deadlocks  Graph-theoretic.
Lecture 7: Deadlock     Necessary Conditions for Deadlock     Deadlock Prevention - Havender's Linear Ordering Deadlock Avoidance Deadlock Detection &
Section 3. True/False Changing the order of semaphores’ operations in a program does not matter. False.
Lecture 5 Concurrency and Process/Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Concurrent Programming James Adkison 02/28/2008. What is concurrency? “happens-before relation – A happens before B if A and B belong to the same process.
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 7 – Deadlock and Indefinite Postponement Outline 7.1 Introduction 7.2Examples of Deadlock.
7.4 Resource Concepts Preemptible resources (e.g. processors and main memory) –Can be removed from a process without loss of work Nonpreemptible resources.
1 CSC 539: Operating Systems Structure and Design Spring 2005 Process deadlock  deadlock prevention  deadlock avoidance  deadlock detection  recovery.
Avishai Wool lecture Introduction to Systems Programming Lecture 5 Deadlocks.
Deadlock Chapter 3 R1 R2 P2P1 Allocated Requested.
Deadlock Problem Deadlock: A set of processes, each holding a resource, and each waiting to acquire a resource held by another process in the set. Example:
The Banker’s Algorithm for A Single Resource
CS 450 OPERATING SYSTEMS DEADLOCKS Manju Muralidharan Priya.
OS Spring 2004 Concurrency: Principles of Deadlock Operating Systems Spring 2004.
Chapter 7 – Deadlock and Indefinite Postponement
OS Fall’02 Concurrency: Principles of Deadlock Operating Systems Fall 2002.
Deadlock Detection with One Resource of Each Type (1)
Chapter 6 Concurrency: Deadlock and Starvation
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:
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks Modified.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-6 Deadlocks Department of Computer Science and Software Engineering.
Operating Systems (OS)
CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.
Deadlock cs550 Operating Systems David Monismith.
Chapter 7 Deadlocks. 7.1 Introduction Deadlock Common approaches to deal with deadlock – Prevention – Avoidance – Detection and recovery.
Operating Systems Lecture 4 Deadlock Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software Engineering.
Styresystemer og Multiprogrammering Block 3, 2005 Deadlocks Robert Glück.
1 OPERATING SYSTEMS DEADLOCKS created by:-gaurav shrivastava contact:- om.
Deadlock Conditions for Deadlock Deadlock Prevention Deadlock Detection Deadlock Recovery Dining Philosophers Semaphores.
Deadlocks.  Deadlocks: Occurs when threads are waiting for resources with circular dependencies Often involve nonpreemptable resources, which cannot.
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.
Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, Chapter 7: Deadlocks.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
Deadlock CS Introduction to Operating Systems.
CSS430 Deadlocks Textbook Ch7
OPERATING SYSTEM CONCEPTS AND PRACTISE
Chapter 7: Deadlocks.
Applied Operating System Concepts -
ITEC 202 Operating Systems
Chapter 7 – Deadlock and Indefinite Postponement
Operating System: DEADLOCKS
OPERATING SYSTEMS DEADLOCKS
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.
Deadlock B.Ramamurthy CSE421 1/11/2019 B.Ramamurthy.
CSc 552 Advanced Unix Process deadlock deadlock prevention
Conditions for Deadlock
Deadlock B.Ramamurthy CSE421 2/23/2019 B.Ramamurthy.
Deadlock B.Ramamurthy CSE421 4/23/2019 B.Ramamurthy.
OPERATING SYSTEMS DEADLOCKS.
IT3002 Computer Architecture
Deadlock B.Ramamurthy CSE421 5/1/2019 B.Ramamurthy.
OPERATING SYSTEMS DEADLOCKS.
DEADLOCKS.
Chapter 7: Deadlocks.
Chapter 7: Deadlock CSS503 Systems Programming
CSE 542: Operating Systems
CSE 542: Operating Systems
Deadlock B.Ramamurthy CSE421 8/28/2019 B.Ramamurthy.
Deadlock B.Ramamurthy CSE421 9/3/2019 B.Ramamurthy.
Presentation transcript:

Lecture 8 Restrictions on Resource Allocation Dining Philosophers - Havender's Linear Ordering

Deadlock Detection In some systems the OS allows deadlock to occur and then suspends or restarts the deadlocked processes allowing others to proceed. To accomplish this recovery we must first have a means of detecting deadlock. Remember that all processes running in a multiprogramming environment need not be involved in the deadlock. Resource allocation diagrams are a graphical representation of an algorithm for deadlock detection. R1 R2 P1P2 P3 Each resource type is shown as a large circle, each instance of a resource is shown as a smaller circle in one of the large circles. Each process is shown as a rectangle. When a resource is allocated to a process an arrow is drawn from the resource instance to the process. When a process requests a resource an arrow is drawn from the process to the desired resource type.

Using Resource Allocation Graphs to Prevent Deadlock Consider a special case of resource allocation graphs in which you require that each resource type contains no more than one more resource that any other resource type and that a process may request at most one resource of each type. Given 3 resource types (R), and 2 processes (P) what is the total number of resources N (evenly disributed among the 3 resource types) needed to prevent the possibility of deadlock? R1 R3 P1P2 R2 Develop an expression for the total number of resources N needed to be distributed evenly across R resource types to prevent deadlock in P processes (again assume that each process can request at most 1 of each resource type).

R1R3 P1P2 R2R1 P1P2 R2 R1R3 P1P2 R2 P3 R1R3 P1P2 R2 P3 R4 A Few Sample Cases For a maximum of m resource types allowed for each process find the maximum number N-1 of resources, evenly distributed across the types for which deadlock can still occur. m=1 m=2 :

Minimum Number of Total Resources to "Prevent" Deadlock P R m NP R m N P = number of processes R = number of resource types m = max number allowed for each process N = min total # resources needed to "prevent" deadlock

P0P0 P1P1 P2P2 P3P3 P4P4 R0R0 R4R4 R1R1 R3R3 R2R2 Dining Philosophers

We will implement Havender's Linear Ordering (HLO) in Dining Philosophers using Dekker's Algorithm in the Laboratory. HLO can be implemented so that it is restarted in each thread (process) at the top of its execution cycle (i.e. the while(!done) loop). The only restriction is that the rules of HLO must be adhered to while a thread is competing for shared resources. Otherwise, individual threads can "jump into" and "jump out of" the HLO at any time. Compare this with the effect on the Banker's Algorithm (deadlock avoidance by Safe State detection), if the number of competing threads was to change during resource management. We will need to implement five separate Dekker's Algorithms, because two philosophers will be competing for each of the five forks. We will use random sleep intervals inside and outside each critical section to simulate the passage of time. Part of this task will be to implement cataloging of performance. At a minimum, we will count the number of times that each philosopher eats. Features of the Dining Philosopher Simulation

static void Main(string[] args) { Thread p0 = new Thread(new ThreadStart(P0)); Thread p1 = new Thread(new ThreadStart(P1)); Thread p2 = new Thread(new ThreadStart(P2)); Thread p3 = new Thread(new ThreadStart(P3)); Thread p4 = new Thread(new ThreadStart(P4)); p0.Start(); p1.Start(); p2.Start(); p3.Start(); p4.Start(); Thread.Sleep(20000); done = true; Thread.Sleep(1000); Console.WriteLine("P0 P1 P2 P3 P4"); for (int i = 0; i < 5; i++) Console.Write(count[i] + " "); Console.ReadLine(); } Main Program

public static bool done = false; public static int[] count = new int[5] {0, 0, 0, 0, 0}; public static char[] status = new char[5] {'T','T','T','T','T'}; public static bool p0wantsf0 = false; public static bool p0wantsf4 = false; public static bool p1wantsf0 = false; public static bool p1wantsf1 = false; public static bool p2wantsf1 = false; public static bool p2wantsf2 = false; public static bool p3wantsf2 = false; public static bool p3wantsf3 = false; public static bool p4wantsf3 = false; public static bool p4wantsf4 = false; public static int favoredforf0 = 0; public static int favoredforf1 = 1; public static int favoredforf2 = 2; public static int favoredforf3 = 3; public static int favoredforf4 = 4; Global Parameters

public static void P0() { System.Random rnd = new Random(); while (!done) { Thread.Sleep(rnd.Next(100)); status[0] = 'H'; // P0 is Hungry p0wantsf0 = true; favoredforf0 = 1; while (p1wantsf0 && favoredforf0 == 1) ; p0wantsf4 = true; favoredforf4 = 4; while (p4wantsf4 && favoredforf4 == 4) ; status[0] = 'E'; // P0 is Eating show_status(); Thread.Sleep(rnd.Next(100)); status[0] = 'T'; // P0 is Talking count[0] += 1; p0wantsf4 = false; // P0 puts down Forks p0wantsf0 = false; } Philosopher 0

P0 P1 P2 P3 P4 T T E T E T E T H T T E T E T H T E T H H T E T E E H H H H E H H E H H H E T H H E H H H H E H H E H T H E T E T H E T T H H T E T H E T E T E T T E H H H E T H H E H H E H E H H T H E H E T E T H T T E T E T E T T E T : : : : :. Sample Runs : : : : : E T H E T E T E T T T E T H E E H H H T E H H E T H H H T E H H E T E H H T E T H E T E T H E H T E H T H E T E T H E T E T E H T T E T H H T E T H E T E T E T E T H E T T H E T H H E T H H H E T H E H T T E T E T T E T

Summary Restricting (or partitioning) resources can "prevent" deadlock while permitting all four conditions for deadlock...Can you explain? Resource allocation formula based on exceeding by a single resource (of any type) the maximum number of resources (evenly distributed across the resource type) that still permit deadlock to occur. Implement Dining Philosophers using Havender's Linear Ordering (HLO) requires the use of five critical sections. We used an implementation of Dekker's Algorithm for enforcing mutual exclusion for each fork. We can ensure that mutual exclusion is enforced (for each fork) and that every philosopher eventually gets a chance to eat. How do we ensure that starvation will not occur?