Lecture Topics: 11/13 Semaphores Deadlock Scheduling.

Slides:



Advertisements
Similar presentations
Scheduling Criteria CPU utilization – keep the CPU as busy as possible (from 0% to 100%) Throughput – # of processes that complete their execution per.
Advertisements

CPU Scheduling Questions answered in this lecture: What is scheduling vs. allocation? What is preemptive vs. non-preemptive scheduling? What are FCFS,
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 6a: CPU Scheduling.
 Basic Concepts  Scheduling Criteria  Scheduling Algorithms.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 5: CPU Scheduling.
Operating Systems Chapter 6
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling.
WELCOME TO THETOPPERSWAY.COM
Chapter 3: CPU Scheduling
CS 311 – Lecture 23 Outline Kernel – Process subsystem Process scheduling Scheduling algorithms User mode and kernel mode Lecture 231CS Operating.
What we will cover…  CPU Scheduling  Basic Concepts  Scheduling Criteria  Scheduling Algorithms  Evaluations 1-1 Lecture 4.
CS 104 Introduction to Computer Science and Graphics Problems Operating Systems (2) Process Management 10/03/2008 Yang Song (Prepared by Yang Song and.
Chapter 5-CPU Scheduling
Job scheduling Queue discipline.
7/2/2015cse deadlock © Perkins, DW Johnson and University of Washington1 Deadlock CSE 410, Spring 2008 Computer Systems
02/11/2004CSCI 315 Operating Systems Design1 CPU Scheduling Algorithms Notice: The slides for this lecture have been largely based on those accompanying.
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads and Scheduling 6.
1Chapter 05, Fall 2008 CPU Scheduling The CPU scheduler (sometimes called the dispatcher or short-term scheduler): Selects a process from the ready queue.
CPU-Scheduling Whenever the CPU becomes idle, the operating system must select one of the processes in the ready queue to be executed. The short term scheduler.
Chapter 6: CPU Scheduling
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-3 CPU Scheduling Department of Computer Science and Software Engineering.
COT 4600 Operating Systems Spring 2011 Dan C. Marinescu Office: HEC 304 Office hours: Tu-Th 5:00-6:00 PM.
Chapter 6 CPU SCHEDULING.
CS 153 Design of Operating Systems Spring 2015 Lecture 11: Scheduling & Deadlock.
Operating Systems Lecture Notes CPU Scheduling Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Scheduling. Alternating Sequence of CPU And I/O Bursts.
1 CSE451 Scheduling Autumn 2002 Gary Kimura Lecture #6 October 11, 2002.
CPU Scheduling CSCI 444/544 Operating Systems Fall 2008.
1 Our focus  scheduling a single CPU among all the processes in the system  Key Criteria: Maximize CPU utilization Maximize throughput Minimize waiting.
Alternating Sequence of CPU And I/O Bursts. Histogram of CPU-burst Times.
CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Thread Scheduling Multiple-Processor Scheduling Operating Systems Examples Algorithm.
Chapter 5 CPU Scheduling Bernard Chen Spring 2007.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 7: CPU Scheduling Chapter 5.
Lecture 7: Scheduling preemptive/non-preemptive scheduler CPU bursts
Chapter 5: Process Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Basic Concepts Maximum CPU utilization can be obtained.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
Processor Scheduling Hank Levy. 22/4/2016 Goals for Multiprogramming In a multiprogramming system, we try to increase utilization and thruput by overlapping.
Lecture Topics: 11/15 CPU scheduling: –Scheduling goals and algorithms.
1 CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling.
CPU Scheduling Operating Systems CS 550. Last Time Deadlock Detection and Recovery Methods to handle deadlock – Ignore it! – Detect and Recover – Avoidance.
Introduction to Operating System Created by : Zahid Javed CPU Scheduling Fifth Lecture.
Operating Systems Scheduling. Scheduling Short term scheduler (CPU Scheduler) –Whenever the CPU becomes idle, a process must be selected for execution.
Chapter 4 CPU Scheduling. 2 Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation.
Lecture 4 CPU scheduling. Basic Concepts Single Process  one process at a time Maximum CPU utilization obtained with multiprogramming CPU idle :waiting.
CPU Scheduling CS Introduction to Operating Systems.
CPU scheduling.  Single Process  one process at a time  Maximum CPU utilization obtained with multiprogramming  CPU idle :waiting time is wasted 2.
1 Module 5: Scheduling CPU Scheduling Scheduling Algorithms Reading: Chapter
Basic Concepts Maximum CPU utilization obtained with multiprogramming
1 Lecture 5: CPU Scheduling Operating System Fall 2006.
Lecturer 5: Process Scheduling Process Scheduling  Criteria & Objectives Types of Scheduling  Long term  Medium term  Short term CPU Scheduling Algorithms.
Scheduling.
Chapter 5: CPU Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria.
1 Chapter 5: CPU Scheduling. 2 Basic Concepts Scheduling Criteria Scheduling Algorithms.
CPU Scheduling Andy Wang Operating Systems COP 4610 / CGS 5765.
Lecture 6 Deadlock 1. Deadlock and Starvation Let S and Q be two semaphores initialized to 1 P 0 P 1 wait (S); wait (Q); wait (Q); wait (S);. signal (S);
Chapter 5a: CPU Scheduling
April 6, 2001 Gary Kimura Lecture #6 April 6, 2001
Scheduling (Priority Based)
Chapter 6: CPU Scheduling
Process management Information maintained by OS for process management
CPU Scheduling Basic Concepts Scheduling Criteria
Chapter 6: CPU Scheduling
Chapter 5: CPU Scheduling
Operating System Concepts
CSE 451: Operating Systems Winter 2003 Lecture 6 Scheduling
Processor Scheduling Hank Levy 1.
Chapter 6: Scheduling Algorithms Dr. Amjad Ali
CSE 153 Design of Operating Systems Winter 2019
CPU Scheduling: Basic Concepts
CSE 451: Operating Systems Winter 2001 Lecture 6 Scheduling
Presentation transcript:

Lecture Topics: 11/13 Semaphores Deadlock Scheduling

Semaphores Semaphores were the first synchronization mechanism (so every mechanism created since is better) The semaphore is an integer variable that has two atomic operations: –P() (the entry procedure) wait for semaphore to become positive and then decrement it by 1 –V() (the exit procedure) increment semaphore by 1, wake up a waiting P if any Who came up with those names? –They’re from Dutch for probieren (to try) and verhogen (to increment) –Thanks, Dijkstra Don't use semaphores

Deadlock Circular waiting for resources Process A wants what process B has Process B wants what process A has Neither can make progress without acquiring the other’s resource Neither will relinquish its own resource No progress possible! lockOne->Acquire(); lockTwo->Acquire(); lockOne->Acquire(); red has lockOne and is waiting on lockTwo blue has lockTwo and is waiting on lockOne DEADLOCK!

System Model There are processes and resources A process follows these steps to utilize a resource –Acquire the resource If the resource is unavailable, block –Use the resource –Release the resource

Necessary Conditions for Deadlock Mutual Exclusion –The resource can’t be shared Hold and Wait –Some process holds one resource while waiting for another No Preemption –Once a process has a resource, it cannot be forced to give it up Circular Wait –A waits for B, B for C, C for D, D for A C AB D

Dealing with Deadlock Deadlock Prevention –Ensure statically that deadlock is impossible Deadlock Avoidance –Ensure dynamically that deadlock is impossible Deadlock Detection and Recovery –Allow deadlock to occur, but notice when it does and try to recover Ignore the Problem

Deadlock Prevention There are four necessary conditions for deadlock Take any one of them away and deadlock is impossible Let’s attack deadlock by –examining each of the conditions –considering what would happen if we threw it out

Necessary Conditions for Deadlock Mutual Exclusion –The resource can’t be shared Hold and Wait –Some process holds one resource while waiting for another No Preemption –Once a process has a resource, it cannot be forced to give it up Circular Wait –A waits for B, B for C, C for D, D for A

Mutual Exclusion Can't eliminate of this condition –some resources are intrinsically non- sharable Examples include printer, write access to a file or record, entry into a section of code

Necessary Conditions for Deadlock Mutual Exclusion –The resource can’t be shared Hold and Wait –Some process holds one resource while waiting for another No Preemption –Once a process has a resource, it cannot be forced to give it up Circular Wait –A waits for B, B for C, C for D, D for A

Hold and Wait A process acquires all the resources it needs before it does anything; if it can’t get them all, then get none –If can't acquire both scanner and printer, then wait until they are both available Resource utilization may be low –If you need P for a long time and Q only at the end, you still have to hold Q’s lock the whole time Starvation prone –May have to wait indefinitely before popular resources are all available at the same time

Necessary Conditions for Deadlock Mutual Exclusion –The resource can’t be shared Hold and Wait –Some process holds one resource while waiting for another No Preemption –Once a process has a resource, it cannot be forced to give it up Circular Wait –A waits for B, B for C, C for D, D for A

No Preemption To attack the no preemption condition: –If a process asks for a resource not currently available, block it and take away all of its other resources –Add the preempted resources to the list of resources the process is waiting for This strategy works for some resources: –CPU state (contents of registers can be spilled to memory) –memory (can be spilled to disk) But not for others: –The printer

Necessary Conditions for Deadlock Mutual Exclusion –The resource can’t be shared Hold and Wait –Some process holds one resource while waiting for another No Preemption –Once a process has a resource, it cannot be forced to give it up Circular Wait –A waits for B, B for C, C for D, D for A

Circular Wait To attack the circular wait condition: –Assign each resource a priority –Make processes acquire resources in priority order Two processes need the printer and the scanner, both must acquire the printer (higher priority) before the scanner This is the most common form of deadlock prevention The only problem: sometimes forced to relinquish a resource

Deadlock Avoidance Deadlock prevention is too strict –low device utilization –reduced system throughput If the OS had more information, it could do more sophisticated things to avoid deadlock and keep the system in a safe state

The Banker’s Algorithm Idea: know what each process might ask for Only make allocations that leave the system in a safe state Inefficient safe unsafe deadlock Resource allocation state space

Deadlock Detection Build a wait-for graph and periodically look for cycles, to find the circular wait condition The wait-for graph contains: –nodes, corresponding to processes –directed edges, corresponding to a resource held by one process and desired by the other E C AB D A waits for B B waits for D D waits for A deadlock!

Deadlock Recovery Once you’ve discovered deadlock, what do you do about it? One option: abort one of the processes to recover from circular wait –Process will likely have to start over from scratch –Which process should you choose? Another solution is to take a resource away from a process –Again, which process should you choose? –How can you roll back the process to its state before it had the coveted resource? –Make sure you don’t keep on preempting from the same process: avoid starvation

Ignoring Deadlock Not as silly as it sounds The mechanisms outlined previously for handling deadlock may be very expensive; if the alternative is to have a forced reboot once a year, that might be acceptable

Review: Process State A process can be (ready, waiting, running) OS has queue of PCBs for each state The ready queue contains PCBs of processes that are ready to run Ready Queue Header Wait Queue Header head ptr tail ptr head ptr tail ptr PCB TetrisPCB WordPCB MSVC PCB DefragPCB Telnet

The Scheduling Problem Need to share the CPU between multiple processes in the ready queue. –OS needs to decide which process gets the CPU next –Once a process is selected, OS needs to do some work to get the process running on the CPU Scheduling is declining in importance –important with slow, heavily-used, shared computers –now most CPU cycles are idle on PCs –still important for supercomputers

How Scheduling Works The scheduler is responsible for choosing a process from the ready queue. The scheduling algorithm implemented by this module determines how process selection is done. The scheduler hands the selected process off to the dispatcher which gives the process control of the CPU

When Does The OS Make Scheduling Decisions ? Scheduling decisions are always made: –when a process is terminated, and –when a process switches from running to waiting. Scheduling decisions are made when an interrupt occurs in a preemptive system.

Non-preemptive/Preemptive Non-preemptive scheduling: –The process decides when it stops –The scheduler must wait for a running process to voluntarily relinquish the CPU (process either terminates or blocks) –Used in the past, now only in real-time systems Preemptive scheduling: –The OS can force a running process to give up control of the CPU, allowing the scheduler to pick another process –Used by all major OS's today –We will assume preemptive scheduling

Scheduling Goals Maximize throughput and resource utilization. –Need to overlap CPU and I/O activities Minimize response time, waiting time and turnaround time Share CPU in a fair way May be difficult to meet all these goals-- sometimes need to make tradeoffs

CPU and I/O Bursts Typical process execution pattern: use the CPU for a while (CPU burst), then do some I/O operations (IO burst) CPU bound processes perform I/O operations infrequently and tend to have long CPU bursts I/O bound processes spend less time doing computation and tend to have short CPU bursts

Scheduling Algorithms: FCFS First Come First Served (FCFS) (aka FIFO) –Scheduler selects the process at the head of the ready queue; typically non-preemptive –Example: 3 processes arrive at the ready queue in the following order: P1 ( CPU burst = 240 ms), P2 ( CPU burst = 30 ms), P3 ( CPU burst = 30 ms) +Simple to implement –Average waiting time can be large

Scheduling Algorithms: RR Round Robin (RR) –Each process on ready queue gets the CPU for a time slice typically ms –A process runs until it blocks, terminates, or uses up its time slice –Short jobs don’t get stuck behind long jobs –Average response time for jobs of same length is bad FCFS: RR:

Scheduling Algorithms: RR RR pros & cons: +Works well for short jobs; typically used in timesharing systems +Shares CPU “fairly” –Overhead due to frequent context switches (but only 1% of CPU) –Increases average waiting time, for jobs that are the same length –What's the right value for the time slice? High throughput vs. Low latency

Scheduling Algorithms: Priority Priority Scheduling –Run the process with the highest priority –Priority based on some attribute of the process (e.g., memory requirements, owner of process, etc.) Issue: –Starvation: low priority jobs may wait indefinitely –Can prevent starvation by aging (increase process priority as it waits)

Priority Inversion Three processes with different priorities: HI, MED, LOW HI runs if it can Suppose, LOW holds a lock that HI wants –LOW prevents HI from running –MED prevents LOW from running –HI can’t run until MED finishes This is known as priority inversion Solution: increase priority of a process holding a lock to the max priority of a process waiting on the lock –LOW -> LOW until it releases the lock

Scheduling Algorithms: SJF Shortest Job First (SJF) –Special case of priority scheduling (priority = expected length of CPU burst) –Scheduler chooses the process with the shortest remaining time to completion (think copy machine) –Example: What’s the average waiting time? –Issue: How do you predict the future? Systems use past process behavior to predict the length of the next CPU burst

Scheduling Algorithms: SJF Shortest Job First (SJF) SJF pros & cons: +Better average response time +Can prove SJF provides optimal response time –Impossible to predict the future –Unfair-- possible starvation (many short jobs can keep long jobs from making any progress)

Multi-level Feedback Adaptive algorithm: process priority changes based on past behavior Process starts with high priority –because it’s probably a short job Decrease priority of processes that hog the CPU (CPU-bound jobs) Increase priority of processes that don’t use the CPU much (I/O-bound jobs)