Last Class: Deadlocks. Today

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

Last Class: Synchronization Problems!

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

Operating systems. Lecture 5. Deadlock: System Model. Deadlock: System Model. Process synchronization Deadlocks. Deadlock: System Model

Operating Systems: Quiz2 December 15, Class: No. Name:

CSC 539: Operating Systems Structure and Design. Spring 2005

CSE 153 Design of Operating Systems

Lecture 9: Midterm Review

CS 318 Principles of Operating Systems

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

COMP 300E Operating Systems Fall Semester 2011 Midterm Examination SAMPLE. Name: Student ID:

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

Today: Synchronization. Recap: Synchronization

Chapter 7: Deadlocks

The Deadlock Problem (1)

Chapter 7: Deadlocks. Operating System Concepts 9th Edition DM510-14

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

Maximum CPU utilization obtained with multiprogramming. CPU I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait

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

Fall 2015 COMP Operating Systems. Lab 06

Chapter 7: Deadlocks

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

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

CPSC/ECE 3220 Summer 2018 Exam 2 No Electronics.

Chapter 7: Deadlocks

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

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

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

Midterm Exam. October 20th, Thursday NSC

OS Structure. User mode/ kernel mode (Dual-Mode) Memory protection, privileged instructions. Definition, examples, how it works?

The Deadlock Problem

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

Part II Process M anagement Management Chapter 7: Deadlocks Fall 2010

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

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

Module 7: Deadlocks. The Deadlock Problem

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

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

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

Chapter 8: Deadlocks

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

The deadlock problem

Resource Management and Deadlocks 1

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

Concurrency: Deadlock and Starvation

TDDB68 + TDDD82. Lecture: Deadlocks

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

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.

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

CMSC 412. Announcements

Last Class: Synchronization

System Model. Deadlocks. Deadlocks. For example: Semaphores. Four Conditions for Deadlock. Resource Allocation Graph

Deadlocks. System Model

CSE Opera+ng System Principles

Deadlocks. Dr. Yingwu Zhu

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

Deadlocks. Deadlock Overview

Potential Deadlock Example

CSE 120. Fall Lecture 8: Scheduling and Deadlock. Keith Marzullo

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

The Deadlock Problem

Principles of Operating Systems

Chapter 8: Deadlocks. The Deadlock Problem

CHAPTER 7 - DEADLOCKS

Chapter 7: Deadlocks

Operating Systems Comprehensive Exam. Spring Student ID # 3/16/2006

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable)

Deadlocks. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

System Model. Types of resources Reusable Resources Consumable Resources

Topic 4 Scheduling. The objective of multi-programming is to have some process running at all times, to maximize CPU utilization.

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

Resource Management and Deadlocks 43

Remaining Contemplation Questions

CS420: Operating Systems. Deadlocks & Deadlock Prevention

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

CS307: Operating Systems

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

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

Outline. G Honors Operating Systems. Deadlock Avoidance. Review: Deadlock Prevention. Lecture 11: Deadlocks Avoidance, Detection, and Recovery

csci 3411: Operating Systems

UNIT-3 DEADLOCKS DEADLOCKS

CS307 Operating Systems Deadlocks

Operating Systems Comprehensive Exam. Spring Student ID # 2/17/2011


Last Class: Monitors. Real-world Examples

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

Bridge Crossing Example

CSE 120 Principles of Operating Systems

UNIT:2. Process Management

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

Today: Protection! Protection!

Principles of Operating Systems

Unit-03 Deadlock and Memory Management Unit-03/Lecture-01

Operating Systems 2015 Spring by Euiseong Seo DEAD LOCK

Chapter 6 Concurrency: Deadlock and Starvation

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

ECE519 Advanced Operating Systems

Chapter 8: Deadlocks. Operating System Concepts with Java

Transcription:

Last Class: Deadlocks Necessary conditions for deadlock: Mutual exclusion Hold and wait No preemption Circular wait Ways of handling deadlock Deadlock detection and recovery Deadlock prevention Deadlock avoidance Lecture 13, page 1 Today Deadlock Avoidance: Banker's algorithm Synchronization wrap-up Exam review Lecture 13, page 2 1

Deadlock Avoidance Claim edges: an edge from a thread to a resource that may be requested in the future Satisfying a request results in converting a claim edge to an allocation edge and changing its direction. A cycle in this extended resource allocation graph indicates an unsafe state. If the allocation would result in an unsafe state, the allocation is denied even if the resource is available. The claim edge is converted to a request edge and the thread waits. This solution does not work for multiple instances of the same resource. Lecture 13, page 3 Banker's Algorithm This algorithm handles multiple instances of the same resource. Force threads to provide advance information about what resources they may need for the duration of the execution. The resources requested may not exceed the total available in the system. The algorithm allocates resources to a requesting thread if the allocation leaves the system in a safe state. Otherwise, the thread must wait. Lecture 13, page 4 2

Preventing Deadlock with Banker's Algorithm class ResourceManager { int n; // # threads int m; // # resources int avail[m], // # of available resources of each type max[n,m], // # of each resource that each thread may want alloc[n,m], //# of each resource that each thread is using need[n,m], // # of resources that each thread might still request Lecture 13, page 5 Banker's Algorithm:Resource Allocation public void synchronized allocate (int request[m], int i) { // request contains the resources being requested // i is the thread making the request if (request > need[i]) //vector comparison error(); // Can't request more than you declared else while (request[i] > avail) wait(); // Insufficient resources available // enough resources exist to satisfy the requests // See if the request would lead to an unsafe state avail = avail - request; // vector additions alloc[i] = alloc[i] + request; need[i] = need[i] - request; while (!safestate () ) { // if this is an unsafe state, undo the allocation and wait <undo the changes to avail, alloc[i], and need[i]> wait (); <redo the changes to avail, alloc[i], and need[i]> } } Lecture 13, page 6 3

Banker's Algorithm: Safety Check private boolean safestate () { boolean work[m] = avail[m]; // accommodate all resources boolean finish[n] = false; // none finished yet // find a process that can complete its work now while (find i such that finish[i] == false and need[i] <= work) { // vector operations work = work + alloc[i] finish[i] = true; } if (finish[i] == true for all i) return true; else return false; } Worst case: requires O(mn 2 ) operations to determine if the system is safe. Lecture 13, page 7 Example using Banker's Algorithm System snapshot: P 0 0 0 1 0 0 1 P 1 1 7 5 1 0 0 P 2 2 3 5 1 3 5 P 3 0 6 5 0 6 3 Total Max Allocation 2 9 9 Available 1 5 2 Lecture 13, page 8 4

Example (contd) How many resources are there of type (A,B,C)? What is the contents of the Need matrix? P 0 P 1 P 2 P 3 Is the system in a safe state? Why? Lecture 13, page 9 Example (contd) If a request from process P 1 arrives for additional resources of (0,5,2), can the Banker's algorithm grant the request immediately? What would be the new system state after the allocation? Max P 0 0 0 1 P 1 1 7 5 P 2 2 3 5 P 3 0 6 5 Allocation Need Available Total What is a sequence of process execution that satisfies the safety constraint? Lecture 13, page 10 5

Example: solutions How many resources of type (A,B,C)? (3,14,11) resources = total + avail What is the contents of the need matrix? Need = Max - Allocation. P 0 0 0 0 P 1 0 7 5 P 2 1 0 0 P 3 0 0 2 Is the system in a safe state? Why? Yes, because the processes can be executed in the sequence P 0, P 2, P 1, P 3, even if each process asks for its maximum number of resources when it executes. Lecture 13, page 11 Example: solutions If a request from process P 1 arrives for additional resources of (0,5,2), can the Banker's algorithm grant the request immediately? Show the system state, and other criteria. Yes. Since 1. (0,5,2) (1,5,2), the Available resources, and 2. (0,5,2) + (1,0,0) = (1,5,2) (1,7,5), the maximum number P 1 can request. 3. The new system state after the allocation is: Allocation Max P 0 0 0 1 0 0 1 P 1 1 5 2 1 7 5 P 2 1 3 5 2 3 5 P 3 0 6 3 0 6 5 and the sequence P 0, P 2, P 1, P 3 satisfies the safety constraint. Available 1 0 0 Lecture 13, page 12 6

Synchronization Wrap up Low-Level Synchronization Primitives: hardware support Advantages Disadvantages Load/Store Interrupt Disable Test&Set What can the OS do with these low-level primitives? the user? Lecture 13, page 13 High-Level Synchronization Primitives Locks: Value: Initially lock is always free. Acquire: Guarantees only one thread has lock; if another thread holds the lock, the acquiring thread waits, else the thread continues Release: Enables another thread to get lock. If threads are waiting, one gets the lock, else, the lock becomes free. Semaphores: Value: Initialization depends on problem. Wait: Decrements value, Thread continues if value 0 (semaphore is available), otherwise, it waits on semaphore Signal: unblocks a process on the wait queue, otherwise, increments value A counting semaphore enables simultaneous access to a fixed number of resources What is the relationship between semaphores and locks? Lecture 13, page 14 7

High-Level Synchronization Primitives: Monitors Monitor Locks provide mutual exclusion to shared data. Lock::Acquire -- wait until lock is free, then grab it. Lock::Release -- unlock, and wake up any thread waiting in Acquire. Always acquire lock before accessing shared data structure. Always release lock when finished with shared data. Lock is initially free. A Condition Variable is a queue of threads waiting for something inside a critical section. Operations: Wait() - atomically release lock, go to sleep Signal() - wake up waiting thread (if one exists) and give it the lock Broadcast() - wake up all waiting threads Rule: thread must hold the lock when doing condition variable operations. Lecture 13, page 15 Deadlocks Necessary conditions for deadlock: Mutual exclusion Hold and wait No preemption Circular wait Ways of handling deadlock Deadlock detection and recovery Deadlock prevention Deadlock avoidance Lecture 13, page 16 8

Exam Review Lecture 13, page 17 Processes and Threads Topics you should understand: 1. What is a process? 2. What is a process control block? What is it used for? What information does it contain? 3. What execution states can a process be in? What do they mean? What causes a process to change execution states? 4. How does the OS keep track of processes? 5. What is a context switch? What happens during a context switch? What causes a context switch to occur? 6. What is the difference between a process and a thread? 7. What is the difference between a kernel thread and a user-level thread? Lecture 13, page 18 9

CPU Scheduling Topics you should understand: 1. What are FCFS, Round Robin, SJF, Multilevel Feedback Queue, and Lottery Scheduling algorithms? 2. What are the advantages and disadvantages of each? 3. What is preemptive scheduling? What is non-preemptive scheduling? Which scheduling algorithms can be preemptive? 4. What is a time slice? What effect does a very small time slice have? What effect does a very large time slice have? 5. What is an I/O bound process? What is a CPU bound process? Is there any reason to treat them differently for scheduling purposes? Lecture 13, page 19 CPU Scheduling Things you should be able to do: 1. Given a list of processes, their arrival time, the lengths of their CPU and I/O bursts, and their total CPU time, you should be able to compute their completion time and waiting time for each scheduling algorithm we have discussed. 2. Given a variation to a scheduling algorithm we studied, discuss what impact you would expect that variation to have. Lecture 13, page 20 10

Synchronization Topics you should understand: 1. Why do we need to synchronize processes/threads? 2. What is mutual exclusion? 3. What is a critical section? 4. What is a lock? What do you need to do to use a lock correctly? 5. What is a semaphore? What are the three things a semaphore can be used for? 6. What is a monitor? What is a condition variable? What are the two possible resumption semantics after a condition variable has been signaled? What are the advantages and disadvantages of each? 7. What is busy waiting? 8. How can interrupts be manipulated to support the implementation of critical sections? What are the advantages and disadvantages? 9. What is test&set? How can a test&set instruction be used to support the implementation of critical sections? What are the advantages and disadvantages? Lecture 13, page 21 Synchronization Things you should be able to do: 1. Given some code that uses locks, semaphores, or monitors, you should be able to explain whether you believe it works. In particular, does it guarantee mutual exclusion where appropriate, does it avoid starvation, and does it avoid deadlock? Lecture 13, page 22 11

Deadlocks Topics you should understand: 1. What are the four necessary conditions for deadlock to occur? 2. What is the difference between deadlock detection and deadlock prevention? 3. After detecting deadlock, what options are conceivable for recovering from deadlock? 4. What is a safe state? What is the difference between an unsafe state and a deadlocked state? Lecture 13, page 23 Deadlocks Things you should be able to do: 1. Given some code, reason about whether or not it is possible for deadlock to occur. 2. Given a state consisting of resources allocated to processes, processes waiting on resources, and available resources, determine if the processes are deadlocked. 3. Given a state consisting of resources allocated to processes, maximum resource requirements of processes, and available resources, determine if the state could lead to deadlock. 4. Given a state consisting of resources allocated to processes, maximum resource requirements of processes, and available resources, and a request for additional resources from a process, determine if the request can be safely satisfied. 5. Given some code that might deadlock, describe how you might change the algorithm to prevent deadlock. Lecture 13, page 24 12

General Skills You should be able to read C++/Java code. You will be asked to write pseudo code with synchronization. You will not be asked detailed questions about any specific operating system, such as Unix, Windows NT,... Lecture 13, page 25 13