CS 3204 Operating Systems Lecture 12 Godmar Back.

Slides:



Advertisements
Similar presentations
ICS Principles of Operating Systems Lectures 8 and 9 - Deadlocks Prof. Dmitri V. Kalashnikov dvk ics.uci.edu Slides © Prof. Nalini Venkatasubramanian.
Advertisements

Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
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.
Deadlocks Today Resources & deadlocks Dealing with deadlocks Other issues Next Time Memory management.
6. Deadlocks 6.1 Deadlocks with Reusable and Consumable Resources
Chapter 6 Concurrency: Deadlock and Starvation
Chapter 6 Concurrency: Deadlock and Starvation
Ceng Operating Systems Chapter 2.4 : Deadlocks Process concept  Process scheduling  Interprocess communication  Deadlocks Threads.
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.
Deadlocks. 2 System Model There are non-shared computer resources –Maybe more than one instance –Printers, Semaphores, Tape drives, CPU Processes need.
Deadlock CSCI 444/544 Operating Systems Fall 2008.
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
1 Concurrency: Deadlock and Starvation Chapter 6.
7/2/2015cse deadlock © Perkins, DW Johnson and University of Washington1 Deadlock CSE 410, Spring 2008 Computer Systems
1 Deadlock Solutions CS 241 March 28, 2012 University of Illinois.
1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.
Deadlock Questions answered in this lecture: What are the four necessary conditions for deadlock? How can deadlock be prevented? How can deadlock be avoided?
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.4. Deadlock detection and recovery 3.5. Deadlock avoidance 3.6. Deadlock prevention.
Operating Systems Part III: Process Management (Deadlocks)
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All.
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
CS 346 – Chapter 7 Deadlock –Properties –Analysis: directed graph Handle –Prevent –Avoid Safe states and the Banker’s algorithm –Detect –Recover.
CS333 Intro to Operating Systems Jonathan Walpole.
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.
Deadlock CS Introduction to Operating Systems.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Deadlock.
Informationsteknologi Monday, October 1, 2007Computer Systems/Operating Systems - Class 111 Today’s class Deadlock.
Deadlocks References –text: Tanenbaum ch.3. Deadly Embrace Deadlock definition –A set of process is dead locked if each process in the set is waiting.
NETW 3005 Monitors and Deadlocks. Reading For this lecture, you should have read Chapter 7. NETW3005 (Operating Systems) Lecture 06 - Deadlocks2.
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.
Deadlock and Starvation
Deadlock CSE451 Andrew Whitaker. Review: What Can Go Wrong With Threads? Safety hazards  “Program does the wrong thing” Liveness hazards  “Program never.
Process Management Deadlocks.
CSE 120 Principles of Operating
Concurrency: Deadlock and Starvation
CSE 451: Operating Systems Winter 2012 Deadlock
ITEC 202 Operating Systems
CS703 - Advanced Operating Systems
Deadlock and Starvation
Chapter 6 : Deadlocks What is a Deadlock?
Advanced Operating System Fall 2009
Introduction to Operating Systems
ICS 143 Principles of Operating Systems
Lecture 19: Deadlock: Conditions, Detection and Avoidance
CS 4284 Systems Capstone Deadlock Godmar Back.
COT 5611 Operating Systems Design Principles Spring 2014
CS 333 Introduction to Operating Systems Class 7 - Deadlock
DPNM Lab. Dept. of CSE, POSTECH
Deadlocks Peng Lu In a multiprogramming environment, several processes may compete for a finite number of resources. A process requests resources; if the.
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
DEADLOCK.
Lecture 19: Deadlock: Conditions, Detection and Avoidance
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
SE350: Operating Systems Lecture 5: Deadlock.
OPERATING SYSTEMS DEADLOCKS.
CSE 451: Operating Systems Spring 2008 Module 9 Deadlock
DEADLOCKS.
Introduction to Deadlocks
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Deadlock CSE 2431: Introduction to Operating Systems
Presentation transcript:

CS 3204 Operating Systems Lecture 12 Godmar Back

Announcements Project 2 due Oct 20 Will return midterm on Thursday Should have initial steps in 3.2 Suggested Order of Implementation by about now, but completed no later than Oct 8. Will return midterm on Thursday Office Hours this week: Back on Wed 2pm-4pm Ryan on Wed 4pm-6pm No hours on Thursday CS 3204 Fall 2008 5/21/2019

Deadlock

Deadlock (Definition) A situation in which two or more threads or processes are blocked and cannot proceed “blocked” either on a resource request that can’t be granted, or waiting for an event that won’t occur Possible causes: resource-related or communication-related Cannot easily back out CS 3204 Fall 2008 5/21/2019

Deadlock Canonical Example (1) pthread_mutex_t A; pthread_mutex_t B; … pthread_mutex_lock(&A); pthread_mutex_lock(&B); pthread_mutex_unlock(&B); pthread_mutex_unlock(&A); pthread_mutex_lock(&B); pthread_mutex_lock(&A); … pthread_mutex_unlock(&A); pthread_mutex_unlock(&B); A B Thread 1 Thread 2 CS 3204 Fall 2008 5/21/2019

Canonical Example (2) Q.: How to fix? account acc1(10000, "acc1"); // Thread 1: for (int i = 0; i < 100000; i++) acc2.transferTo(&acc1, 20); // Thread 2: acc1.transferTo(&acc2, 20); class account { pthread_mutex_t lock; // protects balance int balance; const char *name; public: account(int balance, const char *name) : balance(balance), name(name) { pthread_mutex_init(&this->lock, NULL); } void transferTo(account *that, int amount) { pthread_mutex_lock(&this->lock); pthread_mutex_lock(&that->lock); cout << "Transferring $" << amount << " from " << this->name << " to " << that->name << endl; this->balance -= amount; that->balance += amount; pthread_mutex_unlock(&that->lock); pthread_mutex_unlock(&this->lock); } }; Q.: How to fix? CS 3204 Fall 2008 5/21/2019

Canonical Example (2, cont’d) Answer: acquire locks in same order void transferTo(account *that, int amount) { if (this < that) { pthread_mutex_lock(&this->lock); pthread_mutex_lock(&that->lock); } else { } /* rest of function */ CS 3204 Fall 2008 5/21/2019

Reusable vs. Consumable Resources Distinguish two types of resources when discussing deadlock A resource: “anything a process needs to make progress” But must be something that can be counted in units (Serially) Reusable resources (static, concrete, finite) CPU, memory, locks Can be a single unit (CPU on uniprocessor, lock), or multiple units (e.g. memory, semaphore initialized with N) Consumable resources (dynamic, abstract, infinite) Can be created & consumed: messages, signals Deadlock may involve reusable resources or consumable resources CS 3204 Fall 2008 5/21/2019

Consumable Resources & Deadlock void client() { for (i = 0; i < 10; i++) send(request[i]); for (i = 0; i < 10; i++) { receive (reply[i]); send(ack); } void server() { while (true) { receive(request); process(request); send(reply); receive(ack); } Assume client & server communicate using 2 bounded buffers (one for each direction) Real-life example: flow-controlled TCP Q.: Under what circumstances does this code deadlock? CS 3204 Fall 2008 5/21/2019

Deadlocks, more formally 4 necessary conditions Exclusive Access Hold and Wait No Preemption Circular Wait Aka Coffman conditions Note that cond 1-3 represent things that are normally desirable or required Will look at strategies to Detect & break deadlocks Prevent Avoid Resource Allocation Graph R → P Assignment P → R Request R1 P1 P2 P3 P4 R2 R3 R4 CS 3204 Fall 2008 5/21/2019

Deadlock Detection Idea: Look for circularity in resource allocation graph Q.: How do you find out if a directed graph has a cycle? Can be done eagerly on every resource acquisition/release, resource allocation graph is updated & tested or lazily when all threads are blocked & deadlock is suspected, build graph & test Windows provides this for its mutexes as an option Note: all processes in BLOCKED state is not sufficient to conclude existence of deadlock. (Why?) Note: circularity test is only sufficient criteria if there’s only a single instance of each resource – see next slide for multi-unit resources CS 3204 Fall 2008 5/21/2019

Multi-Unit Resources Note: Cycle, but no deadlock! R3 R1 P1 P2 P3 P4 Resource Allocation Graph R → P Assignment P → R Request Resource Unit R3 R1 P1 P2 P3 P4 R2 R4 Note: Cycle, but no deadlock! CS 3204 Fall 2008 5/21/2019

Deadlock Detection For reusable resources For consumable resources If each resource has exactly one unit, deadlock iff cycle If each resource has multiple units, existence of cycle may or may not mean deadlock Must use reduction algorithm to determine if deadlock exists (Intuition: remove processes that don’t have request edges, return their resource units and remove assignment edges, assign resources to remove request edges, repeat until out of processes without request edges. – If entire graph reduces to empty graph, no deadlock.) For consumable resources analog algorithm possible Q.: What to do once deadlock is detected? CS 3204 Fall 2008 5/21/2019

Deadlock Recovery Preempt resources (if possible) Back processes up to a checkpoint Requires checkpointing or transactions (typically expensive) Kill processes involved until deadlock is resolved Kill all processes involved Reboot Increasing Severity CS 3204 Fall 2008 5/21/2019

Killing Threads or Processes Can be difficult issue: When is it safe to kill a thread/process? Consider: What if thread is killed here? thread_func() { while (!done) { lock_acquire(&lock); p = queue1.get(); queue2.put(p); lock_release(&lock); } thread_func() { while (!done) { lock_acquire(&lock); // access shared state lock_release(&lock); } Must guarantee full resource reclamation (e.g., locks held must be unlocked) consistency of all surviving system data structures (e.g., no packets dropped) CS 3204 Fall 2008 5/21/2019

Safe Termination System code must make sure that no shared data structures are left in an inconsistent state when thread is terminated Same assurance must hold even if thread terminates itself (as in Pintos project 2) General strategy: Allow termination of user processes at any point in time Note: this does not protect user’s data structures Restrict kernel code to certain termination points (where process checks if termination request is pending): E.g., after schedule(); before returning from syscall Protects kernel’s data structures CS 3204 Fall 2008 5/21/2019

Deadlock Prevention (1) Idea: remove one of the necessary conditions! (C1) (Don’t require) Exclusive Access Duplicate resource or make it shareable (where possible) (C2) (Avoid) Hold and Wait a) Request all resources at once hard to know in modular system b) Drop all resources if additional request cannot be immediately granted – retry later requires “try_lock” facility can be inefficient if lots of retries CS 3204 Fall 2008 5/21/2019

Deadlock Prevention (2) (C3) (Allow) Preemption Take resource away from process Difficult: how should process react? Virtualize resource so it can be taken away Requires saving & restoring resource’s state (C4) (Avoid) Circular Wait Use partial ordering Requires mapping to domain that provides an ordering function (addresses often work!) CS 3204 Fall 2008 5/21/2019

Deadlock Avoidance Don’t grant resource request if deadlock could occur in future Or don’t admit process at all Banker’s Algorithm (Dijkstra 1965, see book) Avoids “unsafe” states that might lead to deadlock Need to know what future resource demands are (“credit lines” of all customers) Need to capture all dependencies (no additional synchronization requirements – “loans” can be called back if needed) Mainly theoretical Impractical assumptions Tends to be overly conservative – inefficient use of resources CS 3204 Fall 2008 5/21/2019

Deadlock In The Real World CS 3204 Fall 2008 5/21/2019

Deadlock in the Real World Most common strategy of handling deadlock Test: fix all deadlocks detected during testing Deploy: if deadlock happens, kill and rerun (easy!) If it happens too often, or reproducibly, add deadlock detection code Weigh cost of preventing vs cost of (re-) occurring Static analysis tools detects some kinds of deadlocks before they occur Example: Microsoft Driver Verifier Idea: monitor order in which locks are taken, flag if not consistent lock order CS 3204 Fall 2008 5/21/2019

Deadlock vs. Starvation No matter which policy the scheduler chooses, there is no possible way for processes to make forward progress Starvation: There is a possible way in which threads can make possible forward progress, but the scheduler doesn’t choose it Example: strict priority scheduler will never scheduler lower priority threads as long as higher-priority thread is READY Example: naïve reader/writer lock: starvation may occur by “bad luck” CS 3204 Fall 2008 5/21/2019

Informal uses of term `deadlock’ 4 Coffman conditions apply specifically to deadlock with definable resources Term deadlock is sometimes informally used to also describe situations in which not all 4 criteria apply See interesting discussion in Levine 2003, Defining Deadlock Consider: When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up again until the other has gone. Does it meet the 4 conditions? However, even under informal/extended view, not all “lack of visible progress” situations can reasonably be called deadlocked e.g., an idle system is not usually considered deadlocked CS 3204 Fall 2008 5/21/2019

Summary Deadlock: Strategies to deal with: 4 necessary conditions: mutual exclusion, hold-and-wait, no preemption, circular wait Strategies to deal with: Detect & recover Prevention: remove one of 4 necessary conditions Avoidance: if you can’t do that, avoid deadlock by being conservative CS 3204 Fall 2008 5/21/2019