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
CS422 – Operating Systems Concepts
2

Fred Kuhns (01/17/09)

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
3

Fred Kuhns (01/17/09)

Alternating CPU And I/O Bursts

From Silberschatz, Galvin and Gagne, “Operating Systems Concepts”, 6th eidtion, Wiley, 2002.
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts
4

Histogram of CPU-burst Times

From Silberschatz, Galvin and Gagne, “Operating Systems Concepts”, 6th eidtion, Wiley, 2002.
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts
5

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. 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
CS422 – Operating Systems Concepts
6

Fred Kuhns (01/17/09)

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 (01/17/09)

CS422 – Operating Systems Concepts

7

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 (01/17/09) CS422 – Operating Systems Concepts
8

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 (01/17/09) CS422 – Operating Systems Concepts
9

Scheduling Algorithms
• • • • • • FIFO (or FCFS) Shortest-Job-First (SJF) Priority-based Round-robin Multilevel Queue Multilevel feedback queue

Fred Kuhns (01/17/09)

CS422 – Operating Systems Concepts

10

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
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts
11

First-Come, First-Served (FCFS)
• Example: Process Burst Time P1 24 P2 3 P3 3 • Assume processes arrive as: P1 , P2 , P3 The Gantt Chart for the schedule is:
P1 0 24 P2 27 P3 30

• Waiting time for P1 = 0; P2 = 24; P3 = 27 • Average waiting time: (0 + 24 + 27)/3 = 17
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts
12

FCFS Scheduling (Cont.)
Suppose processes arrive as: P2 , P3 , P1 . • The Gantt chart for the schedule is:
P2 0 3 P3 6 P1 30

• Waiting time for P1 = 6; P2 = 0; P3 = 3 • Average waiting time: (6 + 0 + 3)/3 = 3 • Much better than previous case. • Convoy effect or 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 Concepts Fred Kuhns (01/17/09) 13 CS422 – Operating Systems

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

Fred Kuhns (01/17/09)

14

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} P2Fred Kuhns (01/17/09) completes, Select P4 Process P1 P2 P3 P4 Arrival Time 0.0 2.0 4.0 5.0 Burst Time 7 4 1 4

• Average Waiting Time: [0 + (8 – 2) + (7 – 4) + (12 – 5)]/4 = [6 + 3 + 7]/4 = 4

P1 0 3 7

P3 8

P2 12

P4 16
15

P1

P2

P3 P4

Arrivals

CS422 – Operating Systems Concepts

Example of Preemptive SJF
• T = 0: RQ = {P1} Select P1 • T = 2: RQ = {P2} preempt P1 , Select P2 • T = 4: RQ = {P3, P1} preempt P2 , 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
Fred Kuhns (01/17/09)
Arrivals P1

Process P1 P2 P3 P4

Arrival Time 0.0 2.0 4.0 5.0

Burst Time 7 4 1 4

• Average Waiting Time: [(11–2) + (5-4) + (0) + (7-5)]/4 = [ 9 + 1 + 0 + 2]/4 = 3

P1 0

P2

P3 5

P2 7

P4 11

P1 16
16

P2

P3 P4

CS422 – Operating Systems Concepts

Determining Next CPU Burst
• Can only estimate the length based on previous usage and applying exponential averaging.

1. t n = actual lenght of n th CPU burst 2. τ n +1 = predicted value for the next CPU burst 3. α , 0 ≤ α ≤ 1 4. Define : τ n +1 = α t n + (1 − α )τ n .

∀ α =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
CS422 – Operating Systems Concepts
17

∀ α < 1 => successive terms have less weight
Fred Kuhns (01/17/09)

Exponential Averaging Example
Exponential averaging
9.00 8.00 Burst Duration 7.00 6.00 5.00 4.00 3.00 2.00 1.00 0.00
21 33 13 17 25 29 37 41 45

Actual Alpha (0) 0.2 Alpha (1) 0.35 Alpha (2) 0.5 Alpha (3) 0.65 Alpha (4) 0.8 Average

Burst Number
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts
18

49

1

5

9

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.
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts
19

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 – better performance if most processes complete within one time quantum
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts
20

• Turnaround time also depends on quantum

Example: RR, Quantum = 20
Process Burst Time P1 53 P2 17 P3 68 P4 24 • The Gantt chart is:
P1 0
-20-

P2

-17-

P3

-20-

P4

-20-

P1

-40-

P3

-40-

P4

-24-

P1

-53-

P3

-60-

P3

-68-

20

37

57

77

97 117

121 134 154 162

• Typically, higher average turnaround than SJF, but better response.
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts
21

Small Quantum

Increased Context Switches

From Silberschatz, Galvin and Gagne, “Operating Systems Concepts”, 6th edition, Wiley, 2002.
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts
22

Turnaround Time Versus Quantum

From Silberschatz, Galvin and Gagne, “Operating Systems Concepts”, 6th edition, Wiley, 2002.
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts
23

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

Fred Kuhns (01/17/09)

CS422 – Operating Systems Concepts

24

Multilevel Queue Scheduling

Fred Kuhns (01/17/09)

CS422 – Operating Systems Concepts

25

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

Fred Kuhns (01/17/09)

26

Example: Multilevel Feedback Queue
• Three queues: – Q0 – time quantum 8ms – Q1 – time quantum 16ms – Q2 – FCFS • Scheduling – A new job enters queue Q0 served 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.
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts
27

Practical Issues
• Policy versus mechanism • Platform specific issues • determinism (i.e. real-time) • examples

Fred Kuhns (01/17/09)

CS422 – Operating Systems Concepts

28

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
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts
29

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 (01/17/09)

CS422 – Operating Systems Concepts

30

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 (01/17/09)

CS422 – Operating Systems Concepts

31

Typical Scheduler Goals
• Interactive Systems
– shells, GUI. – Spend most of their time waiting for I/O. – Minimize perceived delay (50-150msec) – compiles, computations. – Optimize throughput. – Require predictable behavior. – May require guarantees on throughput, latency or delay.
CS422 – Operating Systems Concepts
32

• Batch Systems

• Real-time Systems

Fred Kuhns (01/17/09)

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

Fred Kuhns (01/17/09)

CS422 – Operating Systems Concepts

33

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

Fred Kuhns (01/17/09)

CS422 – Operating Systems Concepts

34

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

Fred Kuhns (01/17/09)

CS422 – Operating Systems Concepts

35

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 (01/17/09) CS422 – Operating Systems Concepts
36

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 – time-to-fire, – absolute time, – timing wheel (fixed size circular array)

• Typical to optimize lookup time not insertion time

• for real-time systems insertion time is important

Fred Kuhns (01/17/09)

CS422 – Operating Systems Concepts

37

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 (01/17/09) CS422 – Operating Systems Concepts
38

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)

Fred Kuhns (01/17/09)

CS422 – Operating Systems Concepts

39

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
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts
40

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 (01/17/09)

CS422 – Operating Systems Concepts

41

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 (01/17/09) CS422 – Operating Systems Concepts
42

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

Fred Kuhns (01/17/09)

CS422 – Operating Systems Concepts

43

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 (01/17/09)

CS422 – Operating Systems Concepts

44

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

Fred Kuhns (01/17/09)

CS422 – Operating Systems Concepts

45