06 Scheduling
06 Scheduling
Kangjie Lu
February 6, 2024
Computer Science & Engineering, University of Minnesota
1
Last lecture: Threads
2
Scheduling
State transition diagram
3
Scheduling definition
4
Scheduler
• The scheduler is the OS module that manipulates the process (ready and
waiting/blocked) queues, moving jobs to and from them
• The scheduling algorithm determines which jobs are chosen to run next, for
how long, and what queues they wait on
• In general, the scheduler runs:
• When a job switches from running to waiting
• When an interrupt occurs
• Why?
• When a job is created or terminated
5
Scheduling levels
6
Scheduling styles
Scheduler works differently in different systems; in general:
1. Preemptive Scheduling
• The scheduler can interrupt a running job (involuntary context switch)
2. Non-Preemptive (cooperative) Scheduling
• The scheduler waits for a running job to explicitly block (voluntary context
switch)
8
Scheduling goals
In general
• Batch systems
• Strive for job throughput, turnaround time (supercomputers)
• Interactive systems
• Strive to minimize response time for interactive jobs (PC)
9
Job characteristics
10
Starvation
11
Basic scheduling algorithms
What constitutes a good scheduler?
12
What constitutes a good scheduler?
• Be fair
• Give each process a fair share of the CPU
• Be efficient
• Keep the CPU busy all the time
• Maximize throughput
• Service the largest possible number of jobs in a given amount of time
• Minimize response time
• Interactive users should see good performance
• Be predictable
• Same amount of time in each run for the same program
• Minimize overhead
• Don’t waste too many resources in scheduling and context switching
12
What constitutes a good scheduler?
14
First In First Out (FIFO)
• Always do the task that has the shortest remaining amount of work to do
• Can be either preemptive or non-preemptive
• Preemptive SJF is called shortest remaining time first (SRTF)
• Suppose we have five tasks arrive one right after each other, but the first one
is much longer than the others
• Which completes first in FIFO?
• Which completes last in SJF?
15
FIFO vs SJF
• What is the big deal? Don’t they finish at the same time?
16
FIFO vs SJF
17
FIFO vs SJF
18
FIFO vs SJF
19
Problems with SJF
• Long-burst (CPU-intensive) processes are hurt with a long mean waiting time
• Starvation (Particularly for STRF:preemptive)
• Wide variations in response time
• Impossible to know size of CPU burst
• Like choosing person in line without looking inside basket/cart
• How can you make a reasonable guess?
20
Discussion about FIFO
21
Round Robin
22
Round Robin
23
Advantages of Round Robin
• Round robin scheduling is fair: every process gets an equal share of the CPU
• It is easy to implement
• If we know the number of processes on the run queue, we can know the
worst-case response time for a process
24
Problems with Round Robin
25
Round Robin vs FIFO/SJF
27
Comparing basic algorithms
• FIFO
• Good: fairness
• Bad: turnaround time, response time
• SJF
• Good: turnaround time
• Bad: fairness, hard to estimate run-time
• Round Robin
• Good: fairness, response time
• Bad: turnaround time
29
Priority Scheduling
• Basic algorithms assume that all processes are equally important, true?
• Priority Scheduling
• Choose next job based on priority
• Example: Delta Sky Priority
• Can implement SJF: priority = 1/(expected CPU burst)
• Also can be either preemptive or non-preemptive
• Problem?
• Starvation – low priority jobs can wait indefinitely
• Solution?
Dynamic priority—process aging
• Increase priority as a function of waiting time
• Decrease priority as a function of CPU consumption
29
Multilevel Queues (MLQ): combining algorithms
30
Multi-level Feedback Queue
Goals:
• Responsiveness
• Low overhead
• Starvation freedom
• Some tasks are high/low priority
• Fairness (among equal priority tasks)
Not perfect at any of them - Used in Linux (and probably Windows, MacOS)
More details: Arpaci-Dusseau Ch. 18
31
Quiz 1
Which scheduling algorithm allocates the CPU first to the process that requests
the CPU first?
a) FIFO scheduling
b) shortest job scheduling
c) priority scheduling
d) none of the mentioned
32
Quiz 1
Which scheduling algorithm allocates the CPU first to the process that requests
the CPU first?
a) FIFO scheduling
b) shortest job scheduling
c) priority scheduling
d) none of the mentioned
Ans a)
32
Quiz 2
33
Quiz 2
33
Quiz 3
34
Quiz 3
34
Quiz 4
35
Quiz 4
35
Quiz 5
What is
• turnaround time?
• response time?
• and throughput?
36
Summary
• Scheduling
• Goals
• Styles
• Algorithms: pros and cons
37
Next lecture
38
Backup slides
39
MLFQ: Multi-level Feedback Queue
40
MLFQ: Starvation freedom and Gaming
41
References
[1] https://www.cs.ucr.edu/~csong/cs153/l/sched1.pdf
[2] https://www.cs.ucr.edu/~csong/cs153/l/sched2.pdf
[3] http://www-users.cselabs.umn.edu/classes/Fall-
2017/csci5103/notes/week6_7.1_scheduling_post.pdf [4]
http://www-users.cselabs.umn.edu/classes/Spring-
2018/csci4061/notes/processes.pdf [5]
https://www.cs.rutgers.edu/~pxk/416/notes/07-scheduling.html
42