Deadlocks. Deadlock in Resource Sharing Environment. CIT 595 Spring Recap Example. Representing Deadlock

Similar documents
OPERATING SYSTEMS. Deadlocks

Deadlock and Starvation

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

More on Synchronization and Deadlock

The Deadlock Problem (1)

Chapter 8: Deadlocks. Bridge Crossing Example. The Deadlock Problem

Deadlocks. Copyright : University of Illinois CS 241 Staff 1

The deadlock problem

CISC 7310X. C10: Deadlocks. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 4/12/2018 CUNY Brooklyn College

Operating Systems. Deadlocks

!! What is a deadlock? !! What causes a deadlock? !! How do you deal with (potential) deadlocks? Maria Hybinette, UGA

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

Chapter 6 Concurrency: Deadlock and Starvation

Deadlock Prevention. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Operating Systems. Deadlock. User OS. Kernel & Device Drivers. Interface Programs. Brian Mitchell - Operating Systems

! What is a deadlock? ! What causes a deadlock? ! How do you deal with (potential) deadlocks? Maria Hybinette, UGA

Last Class: Synchronization Problems!

The Deadlock Problem. A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set.

System Model. Types of resources Reusable Resources Consumable Resources

Last Class: Monitors. Real-world Examples

Concurrency: Deadlock and Starvation. Chapter 6

Operating Systems. Operating Systems Sina Meraji U of T

Deadlocks: Detection & Avoidance

CS307 Operating Systems Deadlocks

Deadlocks. Bridge Crossing Example. The Problem of Deadlock. Deadlock Characterization. Resource-Allocation Graph. System Model

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

The Deadlock Problem

CSCC 69H3 2/1/13. Today: Deadlock. Remember example from last week? Example of Deadlock. Example: dining philosophers: Not just an OS Problem!

CMPT 300 Introduction to Operating Systems

Chapter 8: Deadlocks. The Deadlock Problem

The Deadlock Problem. Chapter 8: Deadlocks. Bridge Crossing Example. System Model. Deadlock Characterization. Resource-Allocation Graph

Operating System: Chap7 Deadlocks. National Tsing-Hua University 2016, Fall Semester

Deadlock Prevention, Avoidance, and Detection

Chapter 7: Deadlocks

EECS 482 Introduction to Operating Systems

Chapter 6 Concurrency: Deadlock and Starvation

Chapter 3: Deadlocks

ECS 150 (Operating Systems) Goal To examine what causes deadlock, and what to do about it. Spring Quarter

Deadlock. A Bit More on Synchronization. The Deadlock Problem. Deadlock Characterization. Operating Systems 2/7/2005. CSC 256/456 - Spring

The Slide does not contain all the information and cannot be treated as a study material for Operating System. Please refer the text book for exams.

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

Chapter 7: Deadlocks

Deadlocks. Dr. Yingwu Zhu

Back to synchronization

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

CS 153 Design of Operating Systems Winter 2016

Chapter 7: Deadlocks. Operating System Concepts with Java 8 th Edition

Yet another synchronization problem

Chapter 7: Deadlocks. Operating System Concepts 8 th Edition,

Bridge Crossing Example

Chapter 7: Deadlocks. Chapter 7: Deadlocks. The Deadlock Problem. Chapter Objectives. System Model. Bridge Crossing Example

COP 4610: Introduction to Operating Systems (Spring 2016) Chapter 7 Deadlocks. Zhi Wang Florida State University

CSE Opera+ng System Principles

UNIT:2. Process Management

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

Chapter 8: Deadlocks

Chapter 8: Deadlocks. The Deadlock Problem. System Model. Bridge Crossing Example. Resource-Allocation Graph. Deadlock Characterization

Chapter 7: Deadlocks. Operating System Concepts 8th Edition, modified by Stewart Weiss

Liveness properties. Deadlock

Deadlocks. Minsoo Ryu. Real-Time Computing and Communications Lab. Hanyang University.

Deadlocks. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Module 7: Deadlocks. The Deadlock Problem. Bridge Crossing Example. System Model

Chapter 7 : 7: Deadlocks Silberschatz, Galvin and Gagne 2009 Operating System Concepts 8th Edition, Chapter 7: Deadlocks

Concurrency: Deadlock and Starvation. Chapter 6

The Deadlock Problem

OPERATING SYSTEMS. Prescribed Text Book. Operating System Principles, Seventh Edition. Abraham Silberschatz, Peter Baer Galvin and Greg Gagne

6. Concurrency: Deadlock

Chapter 7: Deadlocks. Operating System Concepts 8 th Edition,! Silberschatz, Galvin and Gagne 2009!

CS420: Operating Systems. Deadlocks & Deadlock Prevention

CMSC 412. Announcements

COMP 3713 Operating Systems Slides Part 3. Jim Diamond CAR 409 Jodrey School of Computer Science Acadia University

Deadlock. Concepts to discuss. A System Model. Deadlock Characterization. Deadlock: Dining-Philosophers Example. Deadlock: Bridge Crossing Example

Classical Synchronization Problems. Copyright : University of Illinois CS 241 Staff 1

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Last Class: Deadlocks. Today

MODERN OPERATING SYSTEMS. Third Edition ANDREW S. TANENBAUM. Chapter 6 Deadlocks

Operating Systems. Lecture3.2 - Deadlock Management. Golestan University. Hossein Momeni

Quiz Answers. CS 537 Lecture 9 Deadlock. What can go wrong? Readers and Writers Monitor Example

Deadlocks Detection and Avoidance. Prof. Sirer CS 4410 Cornell University

Deadlock Risk Management

Chapter 8: Deadlocks. Operating System Concepts with Java

Process P holds resource R. Process P is waiting for resource R. P Process P holds T and requests R. R Process Q holds R and requests T

Chapter 8: Deadlocks. The Deadlock Problem

Last Class: Synchronization Problems. Need to hold multiple resources to perform task. CS377: Operating Systems. Real-world Examples

Deadlock and Starvation

Chapter 7: Deadlocks

Deadlocks: Part I Prevention and Avoidance Yi Shi Fall 2017 Xi an Jiaotong University

Deadlocks. Prepared By: Kaushik Vaghani

Deadlock Prevention. Restrain the ways request can be made. Mutual Exclusion not required for sharable resources; must hold for nonsharable resources.

The Deadlock Problem

Chapter 7: Deadlocks

MODERN OPERATING SYSTEMS. Third Edition ANDREW S. TANENBAUM. Chapter 6 Deadlocks

csci 3411: Operating Systems

Chapter 7: Deadlocks. Operating System Concepts 8 th Edition,

CS153: Deadlock. Chengyu Song. Slides modified from Harsha Madhyvasta, Nael Abu-Ghazaleh, and Zhiyun Qian

Deadlock Risk Management

Deadlocks. Mehdi Kargahi School of ECE University of Tehran Spring 2008

Transcription:

Deadlock in Resource Sharing Environment Operating System Deadlocks CIT 595 Spring 2010 A deadlock occurs when 2 or more processes/threads permanently block each other by each having a lock on a resource which the other tasks are trying to lock Deadlock can occur due to Locks: Waiting to acquire locks on resources, such as objects, pages etc. Sharing resources such as I/O devices printer, disks etc. CIT 595 2 Recap Example 2 Threads access 2 shared variables A and B Variable A is protected by lock x and variable B by lock y Here s what Thread 1 and Thread 2 need to do: Thread 1 A = A + 10 B = B + 20 A = A + B A = A + 30 Thread 2 B = B + 10 A = A + 20 A = A + B B = B + 30 Representing Deadlock Tasks (processes/threads) represented with Circles Resources represented with squares Task T1 has a lock on resource R1 indicated by the arrow from R1 to T1 Task T1 has requested a lock on resource R2 indicated by the arrow from T1 to R2. Task T2 has a lock on resource R2 and has requested a lock on resource R1 Because neither task can continue until a resource is available and neither resource can be released until a task continues, a deadlock state exists CIT 595 3 CIT 595 4 1

Mutual Exclusion Conditions for Deadlock A resource that cannot be used by more than one task at a time Hold and Wait A task holds one resource and may request a new resource No preemption Resources are released voluntarily after competition Circular wait Two or more tasks form a circular chain where each task waits for a resource that the next process in the chain holds Deadlock Handling Deadlock Prevention Ensure deadlock does not occur by ensuring at least one of 4 conditions does not occur No generic solution Deadlock Detection and Recovery: Periodically checks on waiting tasks to determine deadlock If deadlock is determined, then Abort all tasks within that are deadlock expensive Abort one task at a time till deadlock is cycle is eliminated Undo effects of task -> must roll-back to safe state Should not starve the task by repeatedly aborting it Avoidance By not explicitly over allocating resources However this requires prior knowledge of total resources needed by each task not practical Difficult but many algorithms are proposed E.g. Banker s Algorithm CIT 595 5 CIT 595 6 Deadlock Prevention No Mutual Exclusion Applies to limited scenario E.g. shared files that are read only No Hold and Wait 1. Only request resources when have none 2. Atomically acquire all resources New primitive to have single lock to protect all resources Problem: Could lead to starvation for others if too many resources are acquired all at once by one task Allow preemption E.g. B waiting for something held by A, then take resource away from B and give to A Must rollback i.e. undo actions by A No Circular wait Impose ordering on resources E.g. Dinning Philosophers Problem Even # philosopher picks up lower # fork first, and then the higher # fork After eating, put down the highest # fork first, followed by the lower # fork, freeing another philosopher to grab the latter and begin eating. CIT 595 7 Actions Hw 3: Dining Philosopher Acquire fork, eat, Put down fork N philosophers A philosopher is a process Create using fork() N forks Shared resources for adjacent neighbors Synchronize using semaphore Assignment : provide solution that is deadlock free fair(no starvation) 2

Hw3: Deadlock Prevention & Fairness Deadlock due to Circular wait Soln: Impose ordering on fork access Fairness one of the philosophers could make progress but does not because others always go first. Soln: Make sure that philosopher i after eating lets their neighbors eat before eating again Hw3: Fairness technique Every philosopher has two locks for each of its forks, therefore every philosopher sees 4 locks There are 2 exclusive locks, one each for philosopher's left and right, which he/she must be successful in acquiring Before eating, the philosopher will acquire its exclusive locks, once a philosopher is done eating with his exclusive locks, he will unlock the other two locks (which are exclusive semaphores of his/her neighboring philosopher) In the program, you have to decide some philosophers will already start out with their exclusive locks, eat and then unlock the other two locks (i.e. exclusive locks of neighbor) You will need to think little about initial condition i.e. what values the semaphore initial counts will be. CIT 595 10 Hw3: Input/Output Command line input # of philosophers, and # steps to simulate./prog 2 10 Step 1 : Philosopher 0 picks up fork 0 Step 2 : Philosopher 0 picks up fork 1 Step 3 : Philosopher 0 eats Step 4 : Philosopher 0 puts down fork 1 Step 5 : Philosopher 0 puts down fork 0 Step 6 : Philosopher 1 picks up fork 1 Step 7 : Philosopher 1 picks up fork 0 Step 8 : Philosopher 1 eats Step 9 : Philosopher 1 puts down fork 0 Step 10 : Philosopher 1 puts down fork 1 Hw3: Inter-Process Communication (IPC) One IPC technique is message passing Communication is made by the sending of messages to recipients via a message queue For hw3 Message: Action performed by philosopher i put into the queue Observer (parent process): Retrieves the message Parent process should create the message and have exclusive access and should only read from the queue. Child processes should open the same queue (which h the parent process had created) and open it in write only mode Both parent and children will set the mode to be read, write and exclusive for the owner 3

Deadlock Detection Construct a resource graph A graph is collection of nodes and edges Node: task(circles) or resource (square) Edge: Connects to nodes together with arrow indicating direction Resource held by task (Resource -> Task) Task wanting resource (Task -> Resource) If the graph contains one or more cycles then a deadlock exists Use Depth First Search (DFS) Start at the node and explore as far as possible along each branch before backtracking If you see a node revisited again then we have found a cycle CIT 595 13 Example with only one instance of a resource type Task A G & Resources R - W A holds R and want S B holds nothing but wants T C holds nothing but wants S D holds U and wants S and T E holds T and wants V F holds W and wants S G holds V and wants U Search B leads B, T, E, V, G, U, D, T See T twice, found a cycle CIT 595 14 Example with multiple instances of a resource type With more than one instances of resource type, a cycle can exist but deadlock may not Deadlock Avoidance Grant a resource request only if request cannot lead to a deadlock either immediately or in the near future No deadlock is known as safe state Conservative Approach: Each process declares the maximum number of resource units of each resource class that it may require Determine if resource should be given Based on the current allocation and available resources leads to safe state If safe state is possible then actually grant the request Safe State Construct a sequence of task completion, resource release and resource allocation events through which each task can obtain its max resources for each resource class CIT 595 15 CIT 595 16 4

Bankers Algorithm Modeled after banker that gives credit to customers Idea is that not all customers will need their maximum credit immediately Reserve lesser units than the total need Before actually giving credit check to see if financially safe Example with Single Resource class System contains 10 units of resource class Max Need for P1 = 8, P2 = 7, P3 = 5 Current Allocation for (P1 =3, P2 = 1, P3 = 3) If process P1 makes request for one more resource unit i.e. (1, 0, 0) is the system in safe state? Banker s Algorithm To complete a process Pi, check for all resource class R k Total_resource k - Total_allocated k >= Max_need i,k - Allocated_resources i,k Total_Allocated k = Σ (Allocated_resources i,k ) If satisfied, it simulates completion of Process Pi and release of all resources allocated to it by updating Total_allocated k for each R k P1 s request for one resource unit leads system into safe state Sequence P3, P1, P2 CIT 595 17 CIT 595 18 5