450 likes | 644 Views
Scheduling. Fred Kuhns (fredk@arl.wustl.edu, http://www.arl.wustl.edu/~fredk) Department of Computer Science and Engineering Washington University in St. Louis. CPU Scheduling. Multiprogrammed Operating System maximize utilization by switching the CPU between competing processes.
E N D
Scheduling Fred Kuhns (fredk@arl.wustl.edu, http://www.arl.wustl.edu/~fredk) Department of Computer Science and Engineering Washington University in St. Louis
CPU Scheduling • Multiprogrammed Operating System maximize utilization by switching the CPU between competing processes. • goal is to always have some process running • one process runs until it must wait for some event (for example an I/O operation) • rather than remaining idle the OS removes the waiting process from the CPU and inserts another “ready” process onto the CPU • Overview of scheduling module: • Basic Concepts – burst cycle, scheduler, preemption, dispatcher • Scheduling Criteria • Scheduling Algorithms • Scheduling Issues, Implementations and Mechanisms CS422 – Operating Systems Concepts
Burst Cycle • CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait. • process begins with a CPU burst then waits for an I/O operation. When the I/O completes the process will run fora period of time then wait again for another I/O operation. This cycle repeats ending with a final CPU burst which ends with the process voluntarily terminating. • CPU burst distribution – generally consists of many short CPU bursts and few longer bursts. CS422 – Operating Systems Concepts
Histogram of CPU-burst Times From Silberschatz, Galvin and Gagne, “Operating Systems Concepts”, 6th eidtion, Wiley, 2002. CS422 – Operating Systems Concepts
CPU Scheduler • Short-term scheduler • Selects next process to run from among the Ready processes in memory (i.e. the Ready Queue) • Ready queue may be implemented as a FIFO queue, priority queue, tree or random list. • CPU scheduling decisions occur when process: 1. Switches from running to waiting state. 2. Switches from running to ready state. 3. Switches from waiting to ready. • Terminates. • Process (or thread) Preemption: • Scheduling under 1 and 4 is nonpreemptive. • 2 and 3 preemptive. • With non-preemptive when a process get the CPU it keeps running until it either terminates or voluntarily gives up the CPU • With Preemptive schemes the OS may force a process to give up the CPU CS422 – Operating Systems Concepts
Dispatcher • 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. CS422 – Operating Systems Concepts
Scheduling Criteria • CPU utilization – Percent time CPU busy • Throughput – # of processes that complete their execution per time unit • Turnaround time –time to execute a process: from start to completion. sum of • *waiting to be loaded into memory, • waiting in ready queue, • executing • Blocked waiting for an event, for example waiting for I/O • Waiting time – time in Ready queue, directly impacted by scheduling algorithm • Response time – time from submission of request to production of first response. CS422 – Operating Systems Concepts
Optimization Criteria • General goals: • Max CPU utilization • Max throughput • Min turnaround time • Min waiting time • Min response time • May target average times or choose to place bounds on maximum or minimum values. • May also choose to minimize variance in these times. CS422 – Operating Systems Concepts
Scheduling Algorithms • FIFO (or FCFS) • Shortest-Job-First (SJF) • Priority-based • Round-robin • Multilevel Queue • Multilevel feedback queue CS422 – Operating Systems Concepts
FIFO • Easily implemented with a FIFO queue • Process’s PCB initially placed on FIFO Ready queue • When CPU is free the next Process is selected • Problem: Average waiting time may be long with this policy • Consider the case where a long running process precedes a short running process CS422 – Operating Systems Concepts
P1 P2 P3 0 24 27 30 First-Come, First-Served (FCFS) • Example: ProcessBurst Time P1 24 P2 3 P33 • Assume processes arrive as: P1 , P2 , P3 The Gantt Chart for the schedule is: • Waiting time forP1 = 0; P2= 24; P3= 27 • Average waiting time: (0 + 24 + 27)/3 = 17 CS422 – Operating Systems Concepts
P2 P3 P1 0 3 6 30 FCFS Scheduling (Cont.) Suppose processes arrive as: P2 , P3 , P1. • The Gantt chart for the schedule is: • Waiting time for P1 = 6;P2 = 0; P3 = 3 • Average waiting time: (6 + 0 + 3)/3 = 3 • Much better than previous case. • Convoy effector head-of-line blocking • short process behind long process • many I/O bound processes behind CPU-bound process results in inefficient use of I/O resources CS422 – Operating Systems Concepts
Shortest-Job-First (SJR) Scheduling • Process with shortest burst goes next • if tie then use FCFS to break tie • Scheduler must “know” the next CPU burst length of each process in Ready queue • either process declares burst length or system “predicts” next length based on previous usage • SJF is optimal in that it provides the minimum average waiting time for a given set of processes. • Two schemes: • non-preemptive– once CPU assigned, process not preempted until its CPU burst completes. • Preemptive – if a new process with CPU burst less than remaining time of current, preempt. Shortest-Remaining-Time-First (SRTF). CS422 – Operating Systems Concepts
Example of Non-Preemptive SJF • T = 0: RQ = {P1}Select P1 • T = 2: RQ = {P2}No-Preemption • T = 4: RQ = {P3, P2}No-Preemption • T = 5: RQ = {P3, P2 , P4}No-Preemption • T = 7: RQ = {P3, P2 , P4} P1 completes, Select P3 • T = 8: RQ = {P2 , P4} P3 completes, Select P2 • T = 12: RQ = {P4} P2 completes, Select P4 • T = 16: RQ = {} P4 completes ProcessArrival TimeBurst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 • Average Waiting Time:[0 + (8 – 2) + (7 – 4) + (12 – 5)]/4 = [6 + 3 + 7]/4 = 4 P1 P3 P2 P4 0 3 7 8 12 16 P1 P2 P3 P4 Arrivals CS422 – Operating Systems Concepts
Example of Preemptive SJF • T = 0: RQ = {P1}Select P1 • T = 2: RQ = {P2}preemptP1, Select P2 • T = 4: RQ = {P3, P1}preemptP2, Select P3 • T = 5: RQ = {P2, P4 , P1}P3 completes, Select P2 • T = 7: RQ = {P4, P1} P2 completes, Select P4 • T = 11: RQ = {P1} P4 completes, Select P1 • T = 16: RQ = {} P21completes ProcessArrival TimeBurst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 • Average Waiting Time:[(11–2) + (5-4) + (0) + (7-5)]/4 = [ 9 + 1 + 0 + 2]/4 = 3 P1 P2 P3 P2 P4 P1 0 5 7 11 16 Arrivals P1 P2 P3 P4 CS422 – Operating Systems Concepts
Determining Next CPU Burst • Can only estimate the length based on previous usage and applying exponential averaging. • =0: n+1 = n;Recent history does not count. • =1: n+1 = tn; only last burst counts. • Expanding the formula: • n+1 = tn+ (1 - ) tn -1 + … + (1 - ) j tn -j+ … + (1 - ) n+1 0 • < 1 => successive terms have less weight CS422 – Operating Systems Concepts
Exponential Averaging Example CS422 – Operating Systems Concepts
Priority Scheduling • SJF is an example of priority-based scheduling • Associate priority with each process and select highest priority when making scheduling decision • Preemptive or non-preemptive • Priority may be internally defined by OS or externally by the user • statically assigned priority or dynamically set based on execution properties • User may declare relative importance • Problem: Starvation • low priority processes may never execute. • Solution: Aging • as time progresses increase the priority of the process. CS422 – Operating Systems Concepts
Round Robin (RR) • Policy is targeted to time-sharing systems • Similar to FCFS but permit preemption • preempt after fixed time interval and place on tail of ready-queue. • always select next process from head of ready-queue • Each process assigned a time quantum, typically 10-100ms (40ms). • At quantum expiration process moved to tail of Ready Q • if n processes in ready queue, time quantum = q, then each process receives 1/n of CPU time in chunks of at most q units. • No process waits more than (n-1)q time units for CPU. • Performance depends on size of time quantum • q large then behaves like FCFS • q small then appears as dedicated processor with speed 1/n actual – called processor sharing • Want q large compared to context switch time • Turnaround time also depends on quantum • better performance if most processes complete within one time quantum CS422 – Operating Systems Concepts
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3 0 20 37 57 77 97 117 121 134 154 162 Example: RR, Quantum = 20 ProcessBurst Time P153 P217 P368 P424 • The Gantt chart is: • Typically, higher average turnaround than SJF, but better response. -20- -17- -20- -20- -40- -40- -24- -53- -60- -68- CS422 – Operating Systems Concepts
Small Quantum Increased Context Switches From Silberschatz, Galvin and Gagne, “Operating Systems Concepts”, 6th edition, Wiley, 2002. CS422 – Operating Systems Concepts
Turnaround Time Versus Quantum From Silberschatz, Galvin and Gagne, “Operating Systems Concepts”, 6th edition, Wiley, 2002. CS422 – Operating Systems Concepts
Multilevel Queue • Ready queue partitioned into separate queues: • Example: foreground (interactive) and background (batch) • Each queue has its own scheduling algorithm, • Example: foreground – RR, background – FCFS • Scheduling between queues. • Fixed priority scheduling; Possible starvation. • Time slice: • Example: 80% to foreground in RR, 20% to background in FCFS CS422 – Operating Systems Concepts
Multilevel Queue Scheduling CS422 – Operating Systems Concepts
Multilevel Feedback Queue • A process can move between the various queues; aging can be implemented this way. • Multilevel-feedback-queue scheduler defined by: • number of queues • scheduling algorithms for each queue • method used to select when upgrade process • method used to select when demote process • method used to determine which queue a process will enter when that process needs service CS422 – Operating Systems Concepts
Example: Multilevel Feedback Queue • Three queues: • Q0 – time quantum 8ms • Q1 – time quantum 16ms • Q2 – FCFS • Scheduling • A new job enters queue Q0served by FCFS. Then job receives 8 milliseconds. If not finished in 8 milliseconds, moved to Q1. • At Q1 job served by FCFS. Then receives 16 milliseconds. If not complete, preempted and moved to Q2. CS422 – Operating Systems Concepts
Practical Issues • Policy versus mechanism • Platform specific issues • determinism (i.e. real-time) • examples CS422 – Operating Systems Concepts
Policy versus Mechanism • Policies set rules for determining when to switch and which process/thread to run • Mechanisms are used to implement the desired policy. They consists of the data structures and algorithms used to implement policy • Policy and Mechanisms influenced by platform - for example, context switch cost • Policy must balance needs of different application types: • Interactive, Batch and Real-Time CS422 – Operating Systems Concepts
Consider Three Basic Policies • First-In First-Out (FIFO) • runs to completion • Round Robin (RR) • runs for a specified time quantum • Time-Sharing (TS) • Multilevel feedback queues CS422 – Operating Systems Concepts
Platform Issues • Interrupt latency • Clock resolution • Cost of a context switch • saving processor state and loading new • instruction and data cache misses/flushes • memory map cache (TLB) • FPU state CS422 – Operating Systems Concepts
Typical Scheduler Goals • Interactive Systems • shells, GUI. • Spend most of their time waiting for I/O. • Minimize perceived delay (50-150msec) • Batch Systems • compiles, computations. • Optimize throughput. • Real-time Systems • Require predictable behavior. • May require guarantees on throughput, latency or delay. CS422 – Operating Systems Concepts
Some RT Characteristics • Determinism - one measure: delay between interrupt assertion and executing the ISR • Responsiveness - time to service interrupt • User Control • Reliability • Fail-soft operation CS422 – Operating Systems Concepts
Clock Interrupts • How system keeps track of time • implemented using a hardware clock interrupt • interrupt at periodic intervals (clock tick) • typically 10msec, see tick frequency defined by HZ in param.h • High priority, second to power-failure interrupt (NMI). CS422 – Operating Systems Concepts
Interrupt Latency • time to recognize interrupt and execute first instruction of ISR • hardware, for example will CPU finish current instruction. • kernel time to dispatch interrupt to correct ISR • plus time to run ISR • affected by interrupt nesting • plus worst-case interrupt disable time CS422 – Operating Systems Concepts
Clock Interrupt Handler • Update CPU usage for current process • scheduler functions • priority computation, • time-slice expiration (major/minor ticks) • quota policing • update time-of-day and other clocks • callout queue processing • process alarms • run system processes CS422 – Operating Systems Concepts
Callout queue • Queue of functions to be processed at a particular tick • system context, base interrupt priority (sw int) • Example: • packet retransmission, system management functions, device monitoring and polling • Typical to optimize lookup time not insertion time • time-to-fire, • absolute time, • timing wheel (fixed size circular array) • for real-time systems insertion time is important CS422 – Operating Systems Concepts
Alarms • BSD: alarm(), setitimer() • SVR4: alarm(), hrtsys() • Real-time - actual elapsed time • sends SIGALRM • profiling - execution time • sends SIGPROF • virtual time - user mode time • sends SIGVTALRM CS422 – Operating Systems Concepts
BSD Scheduler • Policy - Multilevel Feedback queues • Policy Goal - good response time for interactive tasks while guaranteeing batch job progress • Priority based, dynamically adjusted • Preemptive time-slicing (time quantum) CS422 – Operating Systems Concepts
4.4 BSD Process Scheduling • Scheduling priority range, hi to lo: 0 - 127 • 0-49 kernel mode • 50-127 user mode • 32 run queues (prio/4 = run queue) • priority adjusted based on resource usage. • time quantum set to 0.1 second for over 15 years! • Sleep priorities CS422 – Operating Systems Concepts
Context switch on 4.4 BSD • Synchronous vs. asynchronous • Interprocess: voluntary vs. involuntary • voluntary - process blocks because it requires an unavailable resource. sleep () • involuntary - time slice expires or higher priority process runnable. mi_switch () CS422 – Operating Systems Concepts
Solaris Overview • Multithreaded, Symmetric Multi-Processing • Preemptive kernel - protected data structures • Interrupts handled using threads • MP Support - per cpu dispatch queues, one global kernel preempt queue. • System Threads • Priority Inheritance • Turnstiles rather than wait queues CS422 – Operating Systems Concepts
Hidden Scheduling • Hidden Scheduling - kernel performs work asynchronously on behalf of threads, without considering priority of requester • Examples: STREAMS process and callout queue • Solaris addresses by using system threads • run at kernel priority which is lower than the RT class. • Callout processing is also a problem. Solaris uses two different callout queues: real-time and non-real-time (callout thread). CS422 – Operating Systems Concepts
Priority Inversion • Low priority thread holds a resource required by a higher priority thread. • Partial solution - Priority Inheritance. • High priority thread “lends” its priority to the lower priority thread. • Must be transitive • kernel keeps a synchronization chain CS422 – Operating Systems Concepts
Priority Inheritance in Solaris • Each thread has a global and inherited priority. • Object must keep a reference to the owner • If requester priority > owner, then owner priority raised to that of the requesters • This works with Mutexes, owner always known • Not used for semaphores or condition variables • For reader/write, • owner of record inherits priority, only partial solution CS422 – Operating Systems Concepts