More on Synchronization and Deadlock

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

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

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

Chapter 8: Deadlocks. The Deadlock Problem

Chapter 7: Deadlocks

Chapter 8: Deadlocks. The Deadlock Problem

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

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

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

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

Deadlocks. Dr. Yingwu Zhu

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

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

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

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

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

Lecture 7 Deadlocks (chapter 7)

Chapter 7: Deadlocks

Introduction to OS. Deadlock. MOS Ch. 6. Mahmoud El-Gayyar. Mahmoud El-Gayyar / Introduction to OS 1

Chapter 3. Deadlocks

Chapter 7: Deadlocks

The Deadlock Problem

Chapter 8: Deadlocks

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

Module 7: Deadlocks. The Deadlock Problem

Deadlocks. Thomas Plagemann. With slides from C. Griwodz, K. Li, A. Tanenbaum and M. van Steen

Chapter 8: Deadlocks. Operating System Concepts with Java

Module 7: Deadlocks. System Model. Deadlock Characterization. Methods for Handling Deadlocks. Deadlock Prevention. Deadlock Avoidance

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

The Deadlock Problem

The Deadlock Problem (1)

Chapter 7: Deadlocks CS370 Operating Systems

Principles of Operating Systems

Resources. Lecture 4. Deadlocks. Resources (2) Resources (1) Four Conditions for Deadlock. Introduction to Deadlocks

CMSC 412. Announcements

Principles of Operating Systems

CS370: Operating Systems [Spring 2017] Dept. Of Computer Science, Colorado State University

Bridge Crossing Example

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

OPERATING SYSTEMS. Deadlocks

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

CS307: Operating Systems

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

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

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

Deadlocks. Operating System Concepts - 7 th Edition, Feb 14, 2005

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

DEADLOCKS M O D E R N O P E R A T I N G S Y S T E M S C H A P T E R 6 S P R I N G

CS370 Operating Systems

Deadlock. Operating Systems. Autumn CS4023

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

Deadlocks. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Chapter 7: Deadlocks

CHAPTER 7: DEADLOCKS. By I-Chen Lin Textbook: Operating System Concepts 9th Ed.

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

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

Chapter 3. Deadlocks

Deadlocks. Deadlock Overview

Fig Bridge crossing - deadlock

CS307 Operating Systems Deadlocks

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.

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

Deadlock Risk Management

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

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

Deadlocks. Tore Larsen. With slides from T. Plagemann, C. Griwodz, K. Li, A. Tanenbaum and M. van Steen

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

CS370 Operating Systems

CS370: Operating Systems [Spring 2016] Dept. Of Computer Science, Colorado State University

Frequently asked questions from the previous class survey

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

The Deadlock Problem

Module 6: Deadlocks. Reading: Chapter 7

Process Synchronisation (contd.) Deadlock. Operating Systems. Spring CS5212

CS370 Operating Systems

Last Class: Monitors. Real-world Examples

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

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

CS370 Operating Systems

VI. Deadlocks. What is a Deadlock? Intended Schedule. Deadlock Problem (1) ???

VI. Deadlocks Operating Systems Prof. Dr. Marc H. Scholl DBIS U KN Summer Term

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Deadlocks. Today. Next Time. ! Resources & deadlocks! Dealing with deadlocks! Other issues. ! Memory management

COS 318: Operating Systems. Deadlocks. Jaswinder Pal Singh Computer Science Department Princeton University

UNIT-5 Q1. What is deadlock problem? Explain the system model of deadlock.

CSC 539: Operating Systems Structure and Design. Spring 2005

Roadmap. Deadlock Prevention. Deadlock Prevention (Cont.) Deadlock Detection. Exercise. Tevfik Koşar. CSE 421/521 - Operating Systems Fall 2012

Deadlocks. Prepared By: Kaushik Vaghani

Deadlocks. Today. Next Time. Resources & deadlocks Dealing with deadlocks Other issues. I/O and file systems

CS450/550 Operating Systems

Chapter 3: Deadlocks

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

CSC Operating Systems Fall Lecture - XII Deadlocks - III. Tevfik Ko!ar. Louisiana State University. October 6 th, 2009

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

Silberschatz, Galvin and Gagne 2013! CPU cycles, memory space, I/O devices! " Silberschatz, Galvin and Gagne 2013!

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

ICS Principles of Operating Systems. Lectures Set 5- Deadlocks Prof. Nalini Venkatasubramanian

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

CS420: Operating Systems. Deadlocks & Deadlock Prevention

Transcription:

Examples of OS Kernel Synchronization More on Synchronization and Deadlock Two processes making system calls to read/write on the same file, leading to possible race condition on the file system data structures in OS. CS 256/456 Dept. of Computer Science, University of Rochester Interrupt handlers put I/O data into a buffer queue that might be retrieved by application-initiated I/O system calls. 2/7/2007 CSC 256/456 - Spring 2007 1 2/7/2007 CSC 256/456 - Spring 2007 2 OS Kernel Structure for Synchronization OS is divided into two parts: upper part (serving application requests): system call, exception lower part (serving hardware device requests): interrupt handling Upper part runs in process/thread context resource accounting to corresponding process/thread running on a kernel stack usually associated with the corresponding process/thread control block Lower part runs in a separate interrupt context resource accounting to who? running in a separate (often dedicated) kernel interrupt stack Blocking behaviors: Upper part may block (yield CPU), interleave with others. Lower part does not block, must run atomically (one by one) interrupt handlers typically run with other interrupts disabled. Why? Preemption/priority: A lower part interrupt handler may preempt an upper part system call processing, not vice versa. 2/7/2007 CSC 256/456 - Spring 2007 3 OS Kernel Synchronization Available mechanisms: disabling interrupts spin_lock (busy waiting lock) blocking synchronization (mutex lock, semaphore, ) Synchronization between upper part kernel threads typically blocking synchronization spin_lock if critical section short (only useful on multiprocessor) Synchronization between a upper part kernel thread and a lower part interrupt handler: if blocking synchronization: block only at upper part, never lower part (possible in semaphore) spin_lock may be used (only useful on multiprocessor) the upper part should disable interrupt before entering critical section 2/7/2007 CSC 256/456 - Spring 2007 4

A Little More on OS Kernel Structure Lower part interrupt handlers do not block interrupt handlers typically run with other interrupts disabled. This can be a problem when interrupt handlers do more and more work In modern OS, interrupt handlers typically defer some work to later (interruptible contexts) software irqs in Linux The Deadlock Problem Definition: A set of blocked processes each holding some resources and waiting to acquire a resource held by another process in the set. None of the processes can proceed or back-off (release resources it owns) Examples: Dining philosopher problem System has 2 memory pages (unit of memory allocation); P 1 and P 2 each hold one page and each needs another one. Semaphores A and B, initialized to 1 P 1 P 2 wait (A); wait(b) wait (B); wait(a) 2/7/2007 CSC 256/456 - Spring 2007 5 2/7/2007 CSC 256/456 - Spring 2007 6 Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: Mutual exclusion: only one process at a time can use a resource. Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task. Circular wait: there exists a set {P 0, P 1,, P n, P 0 } of waiting processes such that P 0 is waiting for a resource that is held by P 1, P 1 is waiting for a resource that is held by P 2,, P n 1 is waiting for a resource that is held by P n, and P n is waiting for a resource that is held by P 0. Methods for Handling Deadlocks Ignore the problem and pretend that deadlocks would never occur. Ensure that the system will never enter a deadlock state. Allow the system to enter a deadlock state and then detect/recover. 2/7/2007 CSC 256/456 - Spring 2007 7 2/7/2007 CSC 256/456 - Spring 2007 8

The Ostrich Algorithm Pretend there is no problem unfortunately they can occur Reasonable if deadlocks occur very rarely cost of prevention is high Your typical OSes take this approach It is a trade off between convenience correctness Deadlock Prevention Restrain the ways requests can be made to break one of the four necessary conditions for deadlocks. Attacking the Mutual Exclusion Condition: Some devices (such as printer) can be spooled only the printer daemon uses printer resource thus deadlock for printer eliminated Not all devices can be spooled 2/7/2007 CSC 256/456 - Spring 2007 9 2/7/2007 CSC 256/456 - Spring 2007 10 Deadlock Prevention Attacking the Hold and Wait Condition: Require processes to request all resources before starting Problems may not know required resources at start of run also ties up resources other processes could be using Variation: before a process requests for a new resource, it must give up all resources and then request all resources needed Deadlock Prevention Attacking the No Preemption Condition: Preemption when a process holding some resources and waiting for others, its resources may be preempted to be used by others Problem Many resources may not allow preemption; i.e., preemption will cause process to fail Attacking the Circular Wait Condition: impose a total order of all resource types; and require that all processes request resources in the same order 2/7/2007 CSC 256/456 - Spring 2007 11 2/7/2007 CSC 256/456 - Spring 2007 12

Deadlock Avoidance When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. System is in safe state if there exists a safe sequence of all processes. Sequence <P 1, P 2,, P n > is safe if for each P i, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the P j, with j<i. If P i resource needs are not immediately available, then P i can wait until all P j have finished. When P j is finished, P i can obtain needed resources, execute, return allocated resources, and terminate. When P i terminates, P i+1 can obtain its needed resources, and so on. Deadlock Avoidance (cont.) If a system is in safe state no deadlocks. If a system is in unsafe state possibility of deadlock. Deadlock avoidance dynamically examines the resource-allocation state ensure that a system will never enter an unsafe state. 2/7/2007 CSC 256/456 - Spring 2007 13 2/7/2007 CSC 256/456 - Spring 2007 14 Banker s Algorithm Each process must a priori claim the maximum set of resources that might be needed in its execution. Safety check repeat pick any process that can finish with existing available resources; finish it and release all its resources until no such process exists all finished safe; otherwise unsafe. When a resource request is made, the process must wait if: no enough available resource this request granting of such request would result in a unsafe system state Example of Banker s Algorithm 5 processes P 0 through P 4 3 resource types: A (10 instances), B (5 instances), and C (7 instances) Snapshot at time T 0 : Allocation MaxNeeds Available A B C A B C A B C P 0 0 1 0 7 5 3 3 3 2 P 1 2 0 0 3 2 2 P 2 3 0 2 9 0 2 P 3 2 1 1 2 2 2 P 4 0 0 2 4 3 3 Is this a safe state? Can request for (1,0,2) by P 1 be granted? 2/7/2007 CSC 256/456 - Spring 2007 15 2/7/2007 CSC 256/456 - Spring 2007 16

Methods for Handling Deadlocks Ignore the problem and pretend that deadlocks would never occur. Ensure that the system will never enter a deadlock state. Single Instance of Each Resource Type Maintain wait-for graph Nodes are processes. P i P j if P i is waiting for P j. Periodically search for a cycle in the graph. Allow the system to enter a deadlock state and then detect/recover. 2/7/2007 CSC 256/456 - Spring 2007 17 Resource-Allocation Graph Corresponding wait-for graph 2/7/2007 CSC 256/456 - Spring 2007 18 Additional Issues When there are several instances of a resource type cycle detection in wait-for graph is not sufficient. Deadlock detection is very similar to the safety check in the Banker s algorithm just replace the maximum needs with the current requests Recovery from Deadlock Recovery through preemption take a resource from some other process depends on nature of the resource Recovery through rollback checkpoint a process state periodically rollback a process to its checkpoint state if it is found deadlocked Recovery through killing processes kill one or more of the processes in the deadlock cycle the other processes get its resources In which order should we choose process to kill? 2/7/2007 CSC 256/456 - Spring 2007 19 2/7/2007 CSC 256/456 - Spring 2007 20

Disclaimer Parts of the lecture slides contain original work of Abraham Silberschatz, Peter B. Galvin, Greg Gagne, Andrew S. Tanenbaum, and Gary Nutt. The slides are intended for the sole purpose of instruction of operating systems at the University of Rochester. All copyrighted materials belong to their original owner(s). 2/7/2007 CSC 256/456 - Spring 2007 21