ICS 143 Principles of Operating Systems Discussion Section Week 4
Today Announcements Programming Assignment Preview Lifetime of a Process Schedulers Pop Quiz #1 11/15/2018
Announcements Homework #2 Mid-term exam New office hours Due Thu, 30 April 2015, 11:55pm via EEE DropBox Mid-term exam Fri, 8 May 2015, 11:00-11:50am in EH 1200 New office hours Ekin: Mons 2:00-3:00pm in ICS 468A Michael: Tues 1:00-2:00pm in SBSG 2236 Prof V: Thus 10:00-11:00am in DBH 2086 11/15/2018
Programming Assignment Preview Individual submission – everyone gets to code! It’s ok to discuss ideas, but everyone must submit their own code Implement several schedulers Simulator will provide template / skeleton (Java) Preemptive / non-preemptive Short report (1 page max.) Black-box testing Public tests and expected output provided Will test on public / private tests to make sure your code works More details will be announced next week… 11/15/2018
Schedulers Overview Long Term Short Term Medium Term Creation Terminated Short Term Ready Running Blocked Medium Term Suspended 11/15/2018
Lifetime of a Process 11/15/2018
Lifetime of a Process No PCB yet Must go through scheduler dispatch 11/15/2018
Levels of Scheduling 11/15/2018
CPU Scheduler CPU scheduler selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them. Non-preemptive Scheduling Once CPU has been allocated to a process, the process keeps the CPU until Process exits OR Process switches to waiting state Preemptive Scheduling Process can be interrupted and must release the CPU. Need to coordinate access to shared data 11/15/2018
Dispatch Module Dispatcher module gives control of the CPU to the process selected by the short-term scheduler. This involves: switching context switching to user mode jumping to the proper location in the user program to restart that program Dispatch Latency: time it takes for the dispatcher to stop one process and start another running. Dispatcher must be fast. 11/15/2018
Scheduling Criteria CPU Utilization Throughput Turnaround time Keep the CPU and other resources as busy as possible Throughput # of processes that complete their execution per time unit. Turnaround time amount of time to execute a particular process from its entry time. Waiting time amount of time a process has been waiting in the ready queue. Response Time (in a time-sharing environment) amount of time it takes from when a request was submitted until the first response is produced, NOT output. MAX MAX MIN MIN MIN 11/15/2018
CPU Burst Distribution 11/15/2018
Determining Length of Next CPU Burst One can only estimate the length of burst. Use the length of previous CPU bursts and perform exponential averaging. tn = actual length of nth burst n+1 =predicted value for the next CPU burst = 0, 0 1 Define: n+1 = tn + (1- ) n 11/15/2018
Exponential Averaging(cont.) = 0 n+1 = n; Recent history does not count = 1 n+1 = tn; Only the actual last CPU burst counts. Similarly, expanding the formula: n+1 = tn + (1-) tn-1 + …+ (1-)^j tn-j + … (1-)^(n+1) 0 Each successive term has less weight than its predecessor. j 11/15/2018
First Come First Serve (FCFS) Scheduling Policy: Process that requests the CPU FIRST is allocated the CPU FIRST. Commonly preemptive or non-preemptive? Non-preemptive Implementation - using FIFO queues incoming process is added to the tail of the queue. Process selected for execution is taken from head of queue. Performance metric? Average waiting time in queue. Advantage? Simple, no starvation Disadvantages? Convoy effect: Short processes at the back of the queue may have to wait a long time 11/15/2018
First-Come, First-Served(FCFS) Scheduling Example Suppose the arrival order for the processes is P1, P2, P3 Waiting time P1 = 0; P2 = 24; P3 = 27; Average waiting time (0+24+27)/3 = 17 Gantt Chart for Schedule 24 P1 P2 P3 27 30 11/15/2018
Shortest-Job-First(SJF) Scheduling Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time. Two Schemes: Scheme 1: Non-preemptive Once CPU is given to the process it cannot be preempted until it completes its CPU burst. Scheme 2: Preemptive If a new CPU process arrives with CPU burst length less than remaining time of current executing process, preempt. Also called Shortest-Remaining-Time- First (SRTF). Advantage? SJF is optimal - gives minimum average waiting time for a given set of processes. Disadvantage? May lead to starvation 11/15/2018
Non-Preemptive SJF Scheduling Example Gantt Chart for Schedule P1 7 P3 P2 P4 Average waiting time = (0+6+3+7)/4 = 4 8 12 16 11/15/2018
Preemptive SJF Scheduling(SRTF) Example Waiting time: P1 = 9 Waited from t=2 to t=11 P2 = 1 Waited from t=4 until t=5 P3 = 0 P4 = 2 Waited from t=5 until t=7 Gantt Chart for Schedule P1 2 P2 4 P3 5 7 P2 P4 11 16 P1 11/15/2018
Priority Scheduling A priority value (integer) is associated with each process. Can be based on Cost to user Importance to user Aging %CPU time used in last X hours. CPU is allocated to process with the highest priority. Preemptive Non-preemptive Advantage? Prioritize important jobs Disadvantage? May result in starvation 11/15/2018
Priority Scheduling Example (non-preemptive) Highest priority = 1 Waiting time: P1 = 0 Came in at t=0, processed right away P2 = 6.8 Came in at t=0.2, had to wait until t=7 P3 = 11 Came in at t=4, had to wait until t=15 P4 = 6 Came in at t=5, had to wait until t=11 Gantt Chart for Schedule P1 7 P2 11 15 P4 16 P3 11/15/2018
Round Robin (RR) Each process gets a small unit of CPU time Time quantum usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. n processes, time quantum = q Each process gets 1/n CPU time in chunks of at most q time units at a time. No process waits more than (n-1)q time units (plus context swtich time). Advantages? No starvation, fair time-sharing, good response time. Disadvantages? Typically higher average turnaround time than SRTF, might be complicated to implement 11/15/2018
Round Robin Example Waiting time: Time Quantum = 20 Assume arrival time = 0 for all processes Neglect context switch time for simplicity Waiting time: P1 = 57+24 = 81 from t=20 to t=77, from t=97 to t=121 P2 = 20 from t=0 to t=20 P3 = 37+40+17 = 94 From t=0 to t=37, from t=57 to t=97, from t=117 to t=134 P4 = 57 + 40 = 97 From t=0 to t=57, from t=77 to t=117 Gantt Chart for Schedule P1 20 P2 37 P3 57 P4 77 P1 97 P3 117 P4 121 P1 134 P3 154 P3 162 11/15/2018
Real-Time Scheduling Hard Real-time Computing - required to complete a critical task within a guaranteed amount of time. Soft Real-time Computing - requires that critical processes receive priority over less fortunate ones. Types of real-time Schedulers Periodic Schedulers - Fixed Arrival Rate Demand-Driven Schedulers - Variable Arrival Rate Deadline Schedulers - Priority determined by deadline ….. 11/15/2018
Issues in Real-time Scheduling Dispatch Latency Problem - Need to keep dispatch latency small, OS may enforce process to wait for system call or I/O to complete. Solution - Make system calls preemptible, determine safe criteria such that kernel can be interrupted. Priority Inversion and Inheritance Problem: Priority Inversion Higher Priority process needs kernel resource currently being used by another lower priority process... higher priority process must wait. Solution: Priority Inheritance Low priority process now inherits high priority until it has completed use of the resource in question. 11/15/2018
Pop Quiz #1 11/15/2018