of 21 /21
Operating Systems Lecture Notes Operating Systems Lecture Notes CPU Scheduling CPU Scheduling Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002

Operating Systems Lecture Notes CPU Scheduling Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002

Embed Size (px)

Citation preview

Operating Systems Lecture NotesOperating Systems Lecture Notes

CPU SchedulingCPU Scheduling

Matthew DaileySome material © Silberschatz, Galvin, and Gagne, 2002

CPU SchedulingCPU Scheduling

Main Question: Which process to select from ready queue?

– Preemption vs. non-preemption– Evaluating scheduling algorithms– Algorithms (FCFS, SJF, RR, Priority, Multilevel Queue)

Readings: Silberschatz et al., chapter 6

headtail

ready queueregisters

...

registers

...

PCB7 PCB9

registers

...

PCB1

CPU Scheduling: What Policy?CPU Scheduling: What Policy?

Multiprogramming goal: keep busy. Run some process at all times

Policy determines how you allocate resources to processes

No single policy is best for all situations!

Best policy depends on goal of the system

– Single-user desktop PC

– Compute server for scientific applications

– Interactive time-sharing system

CPU Burst --- I/O Burst CycleCPU Burst --- I/O Burst Cycle

Empirically, most programs alternate between CPU bursts and I/O bursts.

This makes multiprogramming desirable.

CPU Burst TimesCPU Burst Times

Varies with application and hardware.

But histogram almost always looks exponential.

Most bursts short, a few are long.

Short-term schedulerShort-term scheduler

Selects next job from ready queue.– Ready queue not necessarily FIFO!

Must run when:– A process switches from running to waiting (I/O, wait(), etc.)– A process terminates

If preemptive, can also run when:– A process switches from running to ready (Interrupt)– A process switches from waiting to ready (I/O completion)

Preemptive scheduler: Pros and ConsPreemptive scheduler: Pros and Cons

Preemption seems a good choice to avoid this. BUT:– Interrupt during shared user data update

• Problem: Can cause inconsistency or corruption• Remedy: Synchronization (Chapter 7)

– Interrupt during system call• Problem: Could cause kernel data inconsistency or

corruption• Remedy: Disable interrupts during kernel data updates• Must keep disable time super short or might miss interrupts

Headache for app programmers

Headache for OS designer

OS’s like Windows 3.1 and early Apple MacOS were non-preemptive. Badly-behaved apps could kill these systems.

Preemption increases complexity for OS designer AND programmers

Note on Dispatch LatencyNote on Dispatch Latency

The dispatcher is a piece of kernel code that:

– Switches context

– Flips protection bit to user mode

– Jumps to correct location in user program

Dispatch latency is the time it takes from the interrupt to the final jump

Scheduling algorithm: Evaluation criteriaScheduling algorithm: Evaluation criteria

Generally want to maximize– CPU utilization % of time CPU is in use– Throughput # of processes completed per unit time

Generally want to minimize– Turnaround time time from submission to completion

admit time + ready time + CPU time + I/O time

– Wait time amount of time spend in ready queue

– Response time time from submission to first output(important in interactive systems)

Usually optimize average but there are other choices.

Algorithm 1: First Come, First Served (FCFS)Algorithm 1: First Come, First Served (FCFS)

Characteristics:– A non-preemptive scheme.– Like Bangkok Bank when only one service desk is open.– One long-running process can clog the queue for a long time.

Example:Process Arrival Burst time

P1 0 24 P2 1 3 P3 2 3

Avg wait time: ( 0 + 24 + 27 ) / 3 = 17 msBUT opposite order wait time = ???

P1 P2 P3

24 27 300

Gantt Chart

( 0 + 3 + 6 ) / 3 = 3 ms. Much improved!

Algorithm 2: Non-Preemptive Shortest Job First (SJF)Algorithm 2: Non-Preemptive Shortest Job First (SJF)

Characteristics:– Always assign job with shortest next CPU burst to CPU– Provably optimizes average wait time among non-preemptive algorithms.

Example:Process Arrival Burst P1 0 7 P2 2 4 P3 4 1 P4 5 4

Avg wait time: ( 0 + 6 + 3 + 7 ) / 4 = 4 msFCFS wait time: ( 0 + 5 + 7 + 7 ) / 4 = 4.75 ms

Gantt Chart

P1 P3 P2

73 160

P4

8 12

Algorithm 3: Preemptive Shortest Job First (SJF)Algorithm 3: Preemptive Shortest Job First (SJF)

Characteristics:– Like Shortest Job First, but running job can be preempted.– Provably optimizes average wait time among preemptive algorithms.

Example:Process Arrival Burst P1 0 7 P2 2 4 P3 4 1 P4 5 4

Average waiting time = (9 + 1 + 0 +2)/4 = 3 ms

Reason that SJF is provably optimal: moving a shorter process earlier always decreases short process’ wait time more than it increases long process’ wait time.

Gantt Chart

2 110

P4

7

P2 P1

16

P1 P3P2

4 5

Implementing SJFImplementing SJF

SJF is great, but how do we implement it?

– We don’t know a priori how long a job’s burst time is– We have to try to predict the burst time

Define 4.inclusive. 1 and between be Let 3.

burst CPUnext the for value predicted 2.

burst CPU of length actual 1.

01

ατ =

=

+n

thn nt

( ) .11 nnn t ταατ −+==

Burst time prediction with exponential averageBurst time prediction with exponential average

Priority SchedulingPriority Scheduling

Characteristics:– Always schedule the ready process with highest priority– Priority scheduling can be preemptive or non-preemptive

SJF is a special case of priority scheduling:– process priority = the inverse of remaining CPU time

Priority Scheduling IssuesPriority Scheduling Issues

Priorities backwards or forwards?– Unix: -20 is “highest” priority, +20 is “lowest”– Show output of the “top” Unix program on the ITLAB server

Where do the priorities come from?– Usually internally derived by the OS– Sometimes externally derived by users/managers

Problem: Low-priority processes suffer from starvation. They may have to wait indefinitely.

Solution: Process aging (gradually increase priority of old processes)

Round-RobinRound-Robin

Characteristics:– For time-sharing systems– Similar to FCFS but preemptive– Ready queue is a circular queue– Define a short time quantum, e.g. 20 ms

Algorithm:

Before starting a process, set timer to generate interrupt after quantum expires If (CPU burst time < quantum) then process gives up CPU voluntarily else timer generates interrupt after quantum expires

interrupt causes context switch to kernel moderunning process is moved to tail of the ready queueswitch to next process in queue

Round Robin Example [Quantum = 20]Round Robin Example [Quantum = 20]

Process Arrival Burst TimeP1 0 53P2 1 17P3 2 68P4 3 24

Notice the long wait times but short response times

Issue: What’s the “right” time quantum?Too short: too many context switches (too much overhead)Too long: approaches FCFS performanceRule of thumb: large enough to handle 80% of the CPU bursts

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

Gantt Chart

Variations on scheduling: Multilevel queuesVariations on scheduling: Multilevel queues

Use more than one ready queue– e.g. “foreground queue” for interactive programs and “background queue”

system maintenance, batch programs

Use different scheduling algorithm for each queue– e.g. RR for the foreground queue, FCFS for background queue

New problem: how to split time between the queues?– Absolute priority: can cause starvation– Time division: e.g. 80% foreground, 20% background

Variations on scheduling: Multilevel feedback queuesVariations on scheduling: Multilevel feedback queues

Don’t fix a process in a queue: let it move.

One example: several queues with different priorities– Let I/O bound processes float upward (higher priority)– Move CPU hogs downward (lower priority)– Move processes waiting a too long gradually upward

Flexible system but complex implementation.

What have we learned?What have we learned?

What CPU scheduling isPreemptive vs. non-preemptive schedulingHow to evaluate different algorithmsSome of the important scheduling algorithms:

– FCFS– SJF (both preemptive and non-preemptive versions)

• Provably optimal, but impossible to implement• Can be approximated with burst time prediction

– Priority scheduling– Round-robin