CS 153 Design of Operating Systems Winter 2016

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

CSE 120 Principles of Operating Systems

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

Operating Systems ECE344. Ding Yuan

CSE 120 Principles of Operating Systems Spring 2017

CS 318 Principles of Operating Systems

CSE120 Principles of Operating Systems. Prof Yuanyuan (YY) Zhou Scheduling

Lecture 9: Midterm Review

CSE120 Principles of Operating Systems. Prof Yuanyuan (YY) Zhou Deadlock

Advanced Operating Systems (CS 202) Scheduling (1)

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

Deadlocks. Dr. Yingwu Zhu

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

Scheduling. The Basics

Operating Systems ECE344. Ding Yuan

Last Class: Monitors. Real-world Examples

CS 153 Design of Operating Systems Spring 18

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

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

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

The Deadlock Problem (1)

CS 31: Intro to Systems Deadlock. Kevin Webb Swarthmore College April 21, 2015

CSE 153 Design of Operating Systems

More on Synchronization and Deadlock

UNIT:2. Process Management

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

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

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

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

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

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

Chapter 5: CPU Scheduling

Deadlocks: Detection & Avoidance

Advanced Topic: Efficient Synchronization

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.

CSCE Operating Systems Scheduling. Qiang Zeng, Ph.D. Fall 2018

Advanced Operating Systems (CS 202) Scheduling (1) Jan, 23, 2017

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

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

Last Class: Synchronization Problems!

Course Syllabus. Operating Systems

Scheduling of processes

CSE 120. Summer, Inter-Process Communication (IPC) Day 3. Inter-Process Communication (IPC) Scheduling Deadlock. Instructor: Neil Rhodes

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

Readings and References. Deadlock. Deadlock. Simple Traffic Gridlock Example. Reading. CSE Computer Systems November 30, 2001.

Deadlocks. Copyright : University of Illinois CS 241 Staff 1

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

Lecture 5 / Chapter 6 (CPU Scheduling) Basic Concepts. Scheduling Criteria Scheduling Algorithms

Chapter 8: Deadlocks. The Deadlock Problem

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

OPERATING SYSTEMS CS3502 Spring Processor Scheduling. Chapter 5

CSI3131 Final Exam Review

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

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

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

The Deadlock Lecture

Lecture 10: Multi-Object Synchronization

CS 318 Principles of Operating Systems

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

Main Points of the Computer Organization and System Software Module

CMPT 300 Introduction to Operating Systems

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

CSE 451: Operating Systems Winter Module 10 Scheduling

OPERATING SYSTEMS. Deadlocks

Deadlock CS 241. March 19, University of Illinois

Chapter 7: Deadlocks

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

Deadlock. Readings and References. Simple Traffic Gridlock Example. Deadlock. Reading. Other References. CSE 410, Spring 2006 Computer Systems

Operating System Concepts Ch. 5: Scheduling

Operating Systems. Process scheduling. Thomas Ropars.

CPU Scheduling. Rab Nawaz Jadoon. Assistant Professor DCS. Pakistan. COMSATS, Lahore. Department of Computer Science

CSE 4/521 Introduction to Operating Systems

CS162, Spring 2004 Discussion #6 Amir Kamil UC Berkeley 2/26/04

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

General Objectives: To understand the process management in operating system. Specific Objectives: At the end of the unit you should be able to:

Concurrency: Deadlock and Starvation. Chapter 6

Process Management. Deadlock. Process Synchronization. Management Management. Starvation

Review. Preview. Three Level Scheduler. Scheduler. Process behavior. Effective CPU Scheduler is essential. Process Scheduling

Last Class: Deadlocks. Today

Midterm Exam Amy Murphy 19 March 2003

System Model. Types of resources Reusable Resources Consumable Resources

Shared and Preemptable Resources. A few resources may be sharable. Anyone can write the system log.

CS370 Operating Systems

Chapter 7: Deadlocks CS370 Operating Systems

Frequently asked questions from the previous class survey

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

Processes-Process Concept:

Principles of Operating Systems

Department of Computer applications. [Part I: Medium Answer Type Questions]

Chapter 6 Concurrency: Deadlock and Starvation

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. Today. Next Time. ! Resources & deadlocks! Dealing with deadlocks! Other issues. ! Memory management

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

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

CHAPTER 2: PROCESS MANAGEMENT

CPSC/ECE 3220 Summer 2018 Exam 2 No Electronics.

COS 318: Operating Systems. Deadlocks

Scheduling. Multiple levels of scheduling decisions. Classes of Schedulers. Scheduling Goals II: Fairness. Scheduling Goals I: Performance

UNIT 2 Basic Concepts of CPU Scheduling. UNIT -02/Lecture 01

Transcription:

CS 153 Design of Operating Systems Winter 2016 Lecture 12: Scheduling & Deadlock

Priority Scheduling Priority Scheduling Choose next job based on priority» Airline checkin for first class passengers Can implement SJF, priority = 1/(expected CPU burst) Also can be either preemptive or non-preemptive Problem? Solution Starvation low priority jobs can wait indefinitely Age processes» Increase priority as a function of waiting time» Decrease priority as a function of CPU consumption 2

More on Priority Scheduling For real-time (predictable) systems, priority is often used to isolate a process from those with lower priority. Priority inversion is a risk unless all resources are jointly scheduled. P H x->acquire() x->acquire() x->release() time P L P H x->acquire() How can this be avoided? time P M P L x->acquire() 3

Priority Inversion on Mars Pathfinder P H = (Frequent) Bus Management P M = (Long-Running) Communications P L = (Infrequent and short) Data Gathering P H x->acquire() Killed by watchdog P M x->acquire() x->release() time P L 4

Combining Algorithms Scheduling algorithms can be combined Have multiple queues Use a different algorithm for each queue Move processes among queues Example: Multiple-level feedback queues (MLFQ) Multiple queues representing different job types» Interactive, CPU-bound, batch, system, etc. Queues have priorities, jobs on same queue scheduled RR Jobs can move among queues based upon execution history» Feedback: Switch from interactive to CPU-bound behavior 5

Multi-level Feedback Queue (MFQ) Goals: Responsiveness Low overhead Starvation-free Some tasks are high/low priority Fairness (among equal priority tasks) Not perfect at any of them! Used in Linux (and probably Windows, MacOS) 6

MFQ Set of Round Robin queues Each queue has a separate priority High priority queues have short time slices Low priority queues have long time slices Scheduler picks first task in highest priority queue If time slice expires, task drops one level 7

MFQ 8

Unix Scheduler The canonical Unix scheduler uses a MLFQ 3-4 classes spanning ~170 priority levels» Timesharing: first 60 priorities» System: next 40 priorities» Real-time: next 60 priorities» Interrupt: next 10 (Solaris) Priority scheduling across queues, RR within a queue The process with the highest priority always runs Processes with the same priority are scheduled RR Processes dynamically change priority Increases over time if process blocks before end of quantum Decreases over time if process uses entire quantum 9

Motivation of Unix Scheduler The idea behind the Unix scheduler is to reward interactive processes over CPU hogs Interactive processes (shell, editor, etc.) typically run using short CPU bursts They do not finish quantum before waiting for more input Want to minimize response time Time from keystroke (putting process on ready queue) to executing keystroke handler (process running) Don t want editor to wait until CPU hog finishes quantum This policy delays execution of CPU-bound jobs But that s ok 10

Multiprocessor Scheduling This is its own topic, we won t go into it in detail Could come back to it towards the end of the quarter What would happen if we used MFQ on a multiprocessor? Contention for scheduler spinlock Multiple MFQ used this optimization technique is called distributed locking and is common in concurrent programming A couple of other considerations Co-scheduling for parallel programs Core affinity 11

Scheduling Summary Scheduler (dispatcher) is the module (not a thread) that gets invoked when a context switch needs to happen Scheduling algorithm determines which process runs, where processes are placed on queues Many potential goals of scheduling algorithms Utilization, throughput, wait time, response time, etc. Various algorithms to meet these goals FCFS/FIFO, SJF, Priority, RR Can combine algorithms Multiple-level feedback queues 12

Deadlock! 13

Deadlock the deadly embrace! Synchronization we can easily shoot ourselves in the foot Incorrect use of synchronization can block all processes You have likely been intuitively avoiding this situation already More generally, processes that allocate multiple resources generate dependencies on those resources Locks, semaphores, monitors, etc., just represent the resources that they protect If one process tries to access a resource that a second process holds, and vice-versa, they can never make progress We call this situation deadlock, and we ll look at: Definition and conditions necessary for deadlock Representation of deadlock conditions Approaches to dealing with deadlock 14

Deadlock Definition Resource: any (passive) thing needed by a thread to do its job (CPU, disk space, memory, lock) Preemptable: can be taken away by OS Non-preemptable: must leave with thread Starvation: thread waits indefinitely Deadlock: circular waiting for resources Deadlock => starvation, but not vice versa Process 1 Process 2 locka->acquire(); lockb->acquire(); lockb->acquire(); locka->acquire(); 15

Dining Philosophers Each lawyer needs two chopsticks to eat. Each grabs chopstick on the right first. 18

Real example! 19

Conditions for Deadlock Deadlock can exist if and only if the following four conditions hold simultaneously: 1. Mutual exclusion At least one resource must be held in a non-sharable mode 2. Hold and wait There must be one process holding one resource and waiting for another resource 3. No preemption Resources cannot be preempted (critical sections cannot be aborted externally) 4. Circular wait There must exist a set of processes [P 1, P 2, P 3,,P n ] such that P 1 is waiting for P 2, P 2 for P 3, etc. 20

Circular Waiting 21

Dealing With Deadlock There are four approaches for dealing with deadlock: Ignore it responsibility of the developers. UNIX and Windows take this approach Detection and Recovery look for a cycle in dependencies Prevention make it impossible for deadlock to happen Avoidance control allocation of resources 22

Resource Allocation Graph Deadlock can be described using a resource allocation graph (RAG) The RAG consists of a set of vertices P={P 1, P 2,, P n } of processes and R={R 1, R 2,, R m } of resources A directed edge from a process to a resource, P i R i, means that P i has requested R j A directed edge from a resource to a process, R i P i, means that R j has been allocated to P i Each resource has a fixed number of units If the graph has no cycles, deadlock cannot exist If the graph has a cycle, deadlock may exist 23

RAG Example P 1 P 1 R 3 R 1 R 1 R 3 P 2 P 2 P 3 R 2 P 3 R 2 P 4 A cycle and deadlock! Same cycle but no deadlock. Why? 24

A Simpler Case If all resources are single unit and all processes make single requests, then we can represent the resource state with a simpler waits-for graph (WFG) The WFG consists of a set of vertices P={P 1, P 2,, P n } of processes A directed edge P i P j means that P i has requested a resource that P j currently holds If the graph has no cycles, deadlock cannot exist If the graph has a cycle, deadlock exists 25

#1: Detection and Recovery Algorithm How? Scan waits-for graph (WFG) or RAG Detect cycles Fix cycles May be expensive Remove one or more threads, reassign its resources» Requires exception handling code to be very robust Roll back actions of one thread (Preempt resources)» Databases: all actions are provisional until committed» Hard for general cases 26

#2: Deadlock Prevention Prevention Ensure that at least one of the necessary conditions cannot happen Mutual exclusion» Make resources sharable (not generally practical) Hold and wait» Process cannot hold one resource when requesting another» Process requests all needed resources at once (in the beginning) Preemption» OS can preempt resource (costly) Circular wait» Impose an ordering (numbering) on the resources and request them in order (popular implementation technique) 27

#2: Deadlock Prevention How would you do each of the following for dining philosophers? Don't enforce mutex? Don't allow hold and wait? Allow preemption? Don't allow circular waiting? 28

#3: Deadlock Avoidance Avoidance dynamic strategy Tough Provide information in advance about what resources will be needed by processes to guarantee that deadlock will not happen System only grants resource requests if it knows that the process can obtain all resources it needs in future requests» Hint: it will release all resources eventually Avoids circularities (wait dependencies) Hard to determine all resources needed in advance Good theoretical problem, not as practical to use 29

Banker s Algorithm The Banker s Algorithm is the classic approach to deadlock avoidance for resources with multiple units 1. Assign a credit limit to each customer (process) Maximum credit claim must be stated in advance 2. Reject any request that leads to a dangerous state A dangerous state is one where a sudden request by any customer for the full credit limit could lead to deadlock A recursive reduction procedure recognizes dangerous states 3. In practice, the system must keep resource usage well below capacity to maintain a resource surplus Rarely used in practice due to low resource utilization 30

Banker s Algorithm Simplified OK OK OK UNSAFE 3 3 2 3 2 2 1 2 P 1 P 2 P 1 P 2 P 1 P 2 P 1 P 2 31

Possible System States 32

Deadlock Summary Deadlock occurs when processes are waiting on each other and cannot make progress Cycles in Resource Allocation Graph (RAG) Deadlock requires four conditions Mutual exclusion, hold and wait, no resource preemption, circular wait Four approaches to dealing with deadlock: Ignore it Living life on the edge Detection and Recovery Look for a cycle, preempt or abort Prevention Make one of the four conditions impossible Avoidance Banker s Algorithm (control allocation) 33

Concurrency Bugs Subtle to detect compared to deterministic bugs A huge problem in critical infrastructure (airplane control, power systems) Extensive research still ongoing Hardest traditional OS research problem, the others: Memory problems File system problems 34

Example Concurrency Bug A concurrency bug in Mozilla 35

Example Concurrency Bug Another concurrency bug in Mozilla 36

Research on Concurrency Problems Jie Yu and Satish Narayanasamy, A Case for an Interleaving Constrained Shared-Memory Multi- Processor, ISCA 09 Observe safe thread inter-leavings during software testing phase Disallow any unseen inter-leavings Safe but may be too conservative 37

Next Class Preparation for Exam 39