Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
(fredk@arl.wustl.edu, http://www.arl.wustl.edu/~fredk)
Fred Kuhns
Washington
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
Burst Cycle
CPUI/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.
Scheduling
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.
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
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.
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.
Fred Kuhns (7/2/2012) CSE522 Advanced Operating Systems
6
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.
Scheduling Algorithms
FIFO (or FCFS) Easily implemented Average waiting time may be long with this policy Convoy effect or head-of-line blocking: short process behind long process; many Shortest-Job-First (SJF)
I/O bound processes behind CPU-bound process results in inefficient use of I/O resources
Priority-based
optimal in that it provides the minimum average waiting time for a given set of processes can use exponential averaging to estimate next burst size non-preemptive and preemptive versions preemptive or non-preemptive static or dynamic priorities problem: Starvation. Solution: Aging. time-sharing, define quantum, bounded wait times (n-1)/q large q => FCFS, small q => dedicated processor of speed F/n (processor sharing) ready queue partitioned into bands or priorities use feedback to move process between queues
Fred Kuhns (7/2/2012) CSE522 Advanced Operating Systems
8
Round-robin
Multilevel Queue
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 balance needs of different application types: Interactive, Batch and Real-Time
Time-Sharing (TS)
Multilevel feedback queues
10
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
11
Batch:
compiles long running computations. Optimize throughput: can tolerate large scheduling latencies but want to maximize the number of jobs completed over some given time interval.
Real-time
Require predictable behavior. May require guarantees on throughput, latency or delay real-time does not equal fast, it implies temporal constraints.
12
generally set to a periodic rate (clock tick), typically 10msec, see tick frequency defined by HZ in param.h however may use a random timeout interval or synchronize to some other external event.
Interrupt Latency
time to recognize interrupt and execute first instruction of ISR
hardware: CPU finish current instruction, acknowledge interrupt over bus software: dispatch interrupt to correct ISR
14
Callout queue
Queue of functions to be processed at a particular tick System context, base interrupt priority (sw int) Example:
16
Alarms
BSD: alarm(), setitimer()
SVR4: alarm(), hrtsys() Real-time - actual elapsed time
sends SIGALRM
BSD Scheduler
Policy - Multilevel Feedback queues
Policy Goal - good response time for interactive tasks while guaranteeing batch job progress How achieve:
leverage priority-based scheduler by adjusting priorities in response to I/O events Preemptive time-slicing (time quantum), usage based priority assignment
Mechanisms:
Priority-based: always run highest priority process Dynamically adjustable process priorities Clock interrupt to vary priorities
Fred Kuhns (7/2/2012) CSE522 Advanced Operating Systems
18
19
PROC structure
p_priority (kernel pri) p_usrpri (user pri) p_estcpu p_slptime
...
20
Calculation of Priority
Proc structure:
p_estcpu - estimate of cpu utilization p_nice - user-settable (-20 to 19, default = 0)
21
BSD Formulas
Priority calculation (PUSER = 50)
p_usrpri = PUSER + (p_estcpu/4) + 2 p_nice p_estcpu += 1; // each clock tick process is running
23
24
current proc checks this flag before returning to user mode. If set, then call mi_switch ()
Note: BSD does not preempt process in kernel mode.
25
kernel changes the current process pointers and loads new state.
26
27
28
Scheduler Implementations
SVR4 Solaris Mach Digital UNIX Other RT
29
SVR4 Scheduler
Redesigned from traditional approach Separate policy from implementation
Define scheduling classes
30
31
Kernel preemption points - PREMPT checks kprunrun at well defined points in the kernel runrun also used as in traditional implementations
32
SVR4 - Class-Independent
Responsible for
context switching, run queue management and preemption.
Highest (global) priority always runs priority range: 0 - 160, each with own queue
Default allocations:
real-time 100-159, system 60-99, timesharing 0-59
33
CL_TICK CL_FORK, CL_FORKRET CL_ENTERCLASS, CL_EXITCLASS CL_SLEEP, CL_WAKEUP CL_PREEMPT CL_YIELD CL_SETRUN
...
Fred Kuhns (7/2/2012) CSE522 Advanced Operating Systems
34
35
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
Fred Kuhns (7/2/2012) CSE522 Advanced Operating Systems
36
Hidden Scheduling
Hidden Scheduling - kernel performs work asynchronously on behalf of threads, without considering priority of requester
Examples: STREAMS process and callout queue
Callout processing is also a problem. Solaris uses two different callout queues: real-time and non-realtime (callout thread).
37
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
38
MACH
Inherited base scheduling priority which is combined with a CPU usage factor CPU usage factor decayed 5/8 each second inactive threads set own priority after waking up. Clock handler charges current thread. Every 2 seconds, system thread scans run queue and recomputes priorities (addresses starvation) Fixed quantums, preemptive scheduling handoff scheduling - used by IPC
Fred Kuhns (7/2/2012) CSE522 Advanced Operating Systems
40
MACH MP Support
No cross processor interrupts processor sets thread runs on any of the processors from the assigned set. Processor allocation can be handled by a user-level server
Gang scheduling
Digital UNIX
Time-sharing and real-time classes: SCHED_OTHER, SCHED_FIFO, SCHED_RR, Highest priority process is always run Priority range: 0-60 (0-29 TS, 20-31 SYS, 32-63 RT) nonpreemptive kernel no control for priority inversion
42
Digital UNIX - MP
Per processor dispatch queues Scheduler will move threads between queues to balance load soft affinity
43