CSC400 - Operating Systems 7: Scheduling J. Sumey Scheduling one of the main tasks of an OS the scheduler / dispatcher concerned with deciding which runnable process/thread should get the CPU next occurs on any timesharing system and PCs where multiple processes compete concurrently for the CPU scheduling policy: the method by which this decision is made CSC400 - Scheduling 2 CSC400 Operating Systems 1
Scheduling Implementations nonpreemptive once given a CPU, a process runs to completion (no revoke) easy, but questionable fairness in-order processing, but no priorities vulnerable to errant programs preemptive OS can revoke CPU from a running process at quantum expiration additional cost of context switches and memory demands useful in real-time or interactive systems CSC400 - Scheduling 3 Priorities are assigned to each process to indicate relative importance of the process may influence selection decisions by scheduler types: static (fixed) easy to implement, nonresponsive to changes dynamic more complex but can respond to changes to increase throughput or decrease latencies CSC400 - Scheduling 4 CSC400 Operating Systems 2
Scheduling Objectives scheduler needs are affected by type of system, but may be required to: 1. maximize throughput number of completed processes per unit of time 2. minimize response time for interactive processes 3. maximize resource utilization (CPU, memory) 4. avoid indefinite postponement 5. enforce priorities 6. minimize overhead of scheduler/os 7. ensure predictability of system service some objectives contradict each other! CSC400 - Scheduling 5 Scheduling Criteria priorities should favor jobs with higher priorities process behavior CPU-bound: tends to use entire quantum I/O-bound: tends towards brief CPU usage before I/O request response requirements interactive: needs good response (ex: GUI) batch: can suffer reasonable delays real-time: requires precise response CSC400 - Scheduling 6 CSC400 Operating Systems 3
Scheduling Algorithms Non-preemptive FIFO/FCFS: First-Come-First-Served SJF/SRT: Shortest Job First HRRN: Highest Response Ratio Next Preemptive RR: Round-Robin lottery multilevel queues real-time CSC400 - Scheduling 7 First-Come-First-Served FCFS, a.k.a. FIFO, dispatches jobs according to their arrival time is nonpreemptive fairness all jobs treated equally unfairness long jobs force short jobs to wait, unbounded simple to implement, but rarely used in modern OSs as primary policy CSC400 - Scheduling 8 CSC400 Operating Systems 4
Shortest-Job-First SJF is similar to FIFO, but chooses waiting job with shortest (estimated) remaining runtime, aka SRT favors shorter-running jobs at expense of longer jobs; thus reducing number of waiting jobs & average wait time con: requires runtime estimates, difficult to provide accurate values may require use of a "penalty" mechanism CSC400 - Scheduling 9 Highest-Response-Ratio-Next uses estimated runtime (e.r.t.) like SJF but reduces bias against long jobs HRRN assigns dynamic priorities to jobs based on e.r.t. and wait time wait_time e.r.t. priority e.r.t. eliminates indefinite postponement CSC400 - Scheduling 10 CSC400 Operating Systems 5
Round-Robin RR dispatches jobs FIFO, but only gives CPU for a fixed time slice/quantum preemption causes job to be moved to end of the ready queue con: assumes equal priority for all jobs efficiency consideration: choice of quantum duration vs. context switch time ex: Linux uses 100ms, XP uses 20ms (but variable) CSC400 - Scheduling 11 Lottery Scheduling jobs are given lottery tickets for system resources, including CPU scheduler chooses a random ticket and grants access to "winner" easily accommodates different priorities via number of tickets highly responsive and automatically fair based on statistical probabilities CSC400 - Scheduling 12 CSC400 Operating Systems 6
Multilevel Queues adaptive scheme that caters to changing job behaviors based on whether or not job uses full quantum (CPU- vs. IO-bound) use FIFO queues for all levels except lowest, which uses RR scheduler chooses job at head of highest nonempty level new jobs have higher priority at least for a while CSC400 - Scheduling 13 Real-time Scheduling adds requirement of guaranteeing reaction to external stimuli within fixed amount of time to ensure timing constraints are met real-time system events may be: periodic (regular intervals) ex: RPM sensor aperiodic/asynchronous (unpredictable) ex: emergency stop button CSC400 - Scheduling 14 CSC400 Operating Systems 7
Real-time Scheduling - 2 RT scheduling models: hard RT - absolute deadlines always met soft RT - missing occasional deadlines tolerable RT systems must be schedulable given m periodic events each with a period of P i requiring C i CPU time, m Ci system is schedulable only if: 1 i 1 Pi does not account for context switches & nonperiodic events CSC400 - Scheduling 15 Real-time Scheduling - 3 RT scheduling algorithms rate-monotonic: favors periodic processes via higher priorities earliest-deadline-first: dispatches processes according to nearest deadline least-laxity: processes chosen based on smallest "elbow room" (laxity) formula: L = D (T+C) D=deadline T=current time C=remaining CPU time CSC400 - Scheduling 16 CSC400 Operating Systems 8
Scheduling Support in pthreads POSIX threads includes various support routines to affect policies, priorities, etc. pthread_attr_getschedpolicy() pthread_attr_setschedpolicy() get/set scheduling policy to SCHED_FIFO, SCHED_RR, or SCHED_OTHER pthread_attr_getschedparam() pthread_attr_setschedparam() allows caller to get/set priority of calling thread CSC400 - Scheduling 17 Thread Scheduling in Java Java's virtual machine provides multithreading to every applet or app. apps are derived from class Thread uses multilevel queues based on priorities from 1 (min) to 10 (max) with RR policy within each queue threads run to completion, quantum expiration, or till calling yield() method CSC400 - Scheduling 18 CSC400 Operating Systems 9
Thread Scheduling in Windows XP's dispatcher performs preemptive scheduling of each thread doesn't care about owning process! uses 32 ready queues based on priority 31-16 for real-time threads 15-0 for dynamic (non RT) threads RR policy within each level supports SMP (symmetric multiprocessing) dispatches threads to mult. physical or virtual CPUs ex: Pentium HT/Core2Duo/QuadCore technologies CSC400 - Scheduling 19 CSC400 Operating Systems 10