Sei sulla pagina 1di 43

Scheduling

(fredk@arl.wustl.edu, http://www.arl.wustl.edu/~fredk)

Fred Kuhns

Department of Computer Science and Engineering Washington University in St. Louis

WASHINGTON UNIVERSITY IN ST LOUIS

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

Overview of scheduling module:


Basic Concepts burst cycle, scheduler, preemption, dispatcher Scheduling Criteria Scheduling Algorithms Scheduling Issues, Implementations and Mechanisms
CSE522 Advanced Operating Systems
2

Fred Kuhns (7/2/2012)

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.

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

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.

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. 4. 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

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

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.

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

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.

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

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

Multilevel feedback queue

Policy versus Mechanism


Policies set rules for determining when to switch and which process/thread to run next

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

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

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

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

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

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

11

Typical Scheduler Goals


Interactive: examples are shells and GUI.
Spend most of their time waiting for I/O. Minimize perceived delay (50-150msec)

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.

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

12

Periodic Execution of OS: Clock Interrupts


Common requirement is for a system to track the passage of time:
processes request timed event notifications time-sharing of resources periodic system housekeeping

Implemented using a hardware clock interrupt

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.

High priority, only NMI (non-maskable interrupt) are higher.


Fred Kuhns (7/2/2012) CSE522 Advanced Operating Systems
13

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

plus time to run ISR


affected by interrupt nesting

plus worst-case interrupt disable time

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

14

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
Fred Kuhns (7/2/2012) CSE522 Advanced Operating Systems
15

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

for real-time systems insertion time is important


Typical to optimize lookup time not insertion time
time-to-fire, absolute time, timing wheel (fixed size circular array)

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

16

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
Fred Kuhns (7/2/2012) CSE522 Advanced Operating Systems
17

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

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 20 years. Sleep priorities

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

19

Scheduling Related Attributes

PROC structure
p_priority (kernel pri) p_usrpri (user pri) p_estcpu p_slptime

Predefined Process Priorities


PSWP PVM PINOD PRIBIO PVFS PZERO PSOCK PWAIT PLOCK PPAUSE PUSER 0 4 8 16 20 22 24 32 36 40 50 while swapping process wait for memory wait for file control info wait on disk I/O wait for kernel-level FS lock baseline priority wait on socket wait for child to exit wait for user-level FS lock wait for signal to arrive base user priority

...

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

20

Calculation of Priority
Proc structure:
p_estcpu - estimate of cpu utilization p_nice - user-settable (-20 to 19, default = 0)

priority (p_usrpri) recalculated every 4 clock ticks

p_estcpu incremented each clock tick process is running


every second CPU usage is decayed
sleeping processes are ignored

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

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

Decay calculation - schedcpu () each second


p_estcpu=((2load_avg)/(2load_avg+1))p_estcpu+p_nice

ex, load=1, p_estcpu = 0.66T4 + + 0.13T0

Processes sleeping for > 1 second


p_slptime set to 0 then incremented by 1 ea. second p_estcpu = p_estcpu((2load)/(2load+1))p_slptime
Fred Kuhns (7/2/2012) CSE522 Advanced Operating Systems
22

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 ()

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

23

BSD - Voluntary Context Switch


Invoke sleep() with a wait channel (resource address) and (kernel) priority
sleeping process organized as an array of queues. Wait channel is hashed to find bin. sleep() raises priority level splhigh (block interrupts). Set kernel priority (p_priority) wakeup(): remove process(es) from queue splhigh and recalculate user priority (p_usrpri).

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

24

BSD - Involuntary context switch


Results from an asynchronous event kernel schedules an AST, need_resched (), and sets global flag want_resched

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.

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

25

BSD Interprocess Context Switch


Change user and kernel context All user mode state located in
kernel-mode HW state - stored in PCB (u area) user-mode HW state - kernel stack (u area) proc structure (not swapped)

kernel changes the current process pointers and loads new state.

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

26

Selecting a process to run


cpu_switch (), called by mi_switch ()
block interrupts and check whichqs for nonempty run queue. If non, unblock interrupts and loop. Remove first process in queue. If queue is not empty then reset bit in whichqs clear curproc and want_resched set new process (context switch) and unblock interrupts

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

27

Limitations of the BSD Model


Limited scalability No resource guarantees

non-deterministic, unbounded delays (priority inversion)


limited application control over priorities

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

28

Scheduler Implementations
SVR4 Solaris Mach Digital UNIX Other RT

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

29

SVR4 Scheduler
Redesigned from traditional approach Separate policy from implementation
Define scheduling classes

define framework with well defined interfaces


Attempt to bound dispatch latencies Enhance application control

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

30

SVR4 Scheduling Classes


Scheduler represents an abstract base class
defines interface performs class independent processing

Derived classes are specific implementations of a scheduling policy


priority computation, range of priorities, whether quantums are used or if priorities can vary dynamically.

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

31

SVR4 - Issues Addressed


Dispatch Latencies - time between a process becoming runnable and when it executes.
Does not include interrupt processing. Includes

nonpreemptive kernel processing and context switch time.

Kernel preemption points - PREMPT checks kprunrun at well defined points in the kernel runrun also used as in traditional implementations

Response time - total time from event to application response.

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

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

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

33

SVR4 - Scheduler Interface

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

SVR4 Class Implementations


Time-Sharing: event driven scheduling.
priority changed dynamically, round-robin within priority, timeslice and priority static parameter table.

System - fixed priority (FIFO). System tasks.

Real-time: fixed priority and time quantum.

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

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

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-realtime (callout thread).

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

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

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

38

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/writer,
owner of record inherits priority, only partial solution
Fred Kuhns (7/2/2012) CSE522 Advanced Operating Systems
39

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

dedicate one processor per thread. Minimize barrier synchronization delay


CSE522 Advanced Operating Systems
41

Fred Kuhns (7/2/2012)

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

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

42

Digital UNIX - MP
Per processor dispatch queues Scheduler will move threads between queues to balance load soft affinity

Fred Kuhns (7/2/2012)

CSE522 Advanced Operating Systems

43

Potrebbero piacerti anche