You are on page 1of 28

CPSC 457

CPSC 457
CPU scheduling part 2

Fall 2017

Contains slides from Mea Wang, Andrew Tanenbaum and Herbert Bos

1
CPSC 457
Overview
● more CPU scheduling algorithms for interactive systems
○ shortest-process-next, exponential smoothing
○ fair-share
○ priority scheduling
□ multilevel queues
□ multilevel feedback queues
□ lottery
● real-time CPU scheduling
○ rate-monotonic
○ earliest-deadline-first
● thread scheduling
○ user-level
○ kernel-level
2
CPSC 457
CPU scheduling on batch systems

● covered in last lecture:


○ first-come-first-serve (FCFS)
○ shortest-job-first (SJF)
○ shortest-remaining-time-next (SRTN)

● many ideas from these can be adapted to interactive and even real-time systems

3
CPSC 457
CPU scheduling on interactive systems

● covered in last lecture:


○ round-robin (RR)
● today ― few more schedulers for interactive systems
○ shortest-process-next scheduling
○ fair-share scheduling
○ priority scheduling
□ lottery scheduling
□ multilevel-queues
□ multilevel feedback queues

4
CPSC 457
Shortest-process-next
● very similar to SJF scheduling
○ uses time-sharing preemption (time slice)
○ ready queue is sorted by a predicted next CPU burst

● prediction can be calculated using exponential smoothing


○ after every burst B, we update the prediction P as follows:

P = a x B + (1 - a) x P'

where
P is the new prediction, initialized to some value (eg. 0)
P' is the previous prediction
a is a smoothing factor, commonly set to ½
5
CPSC 457
Predicting burst via exponential smoothing

B
P
P = a x B + (1 - a) x P'

actual bursts B:
predicted burst P:
6
CPSC 457
Fair-share scheduling

● a scheduling algorithm that takes into account the (importance of) owners of the processes
● it is used to ensure all users of a system get a 'fair' share of the CPU
● it allocates CPU among users/groups, instead of processes
● solves problems like this:
○ user A runs 10 processes, user B runs a single process
○ user A would get 90% of the CPU, user B only gets 10%
● each user is allocated some fraction of the CPU
○ equal share: with N users, each user gets 1/N of the CPU
○ unequal share: important users get a big chunk of CPU time,
and even more important users get an even bigger chunk of CPU time
● all processes belonging to an owner have to share the owner's CPU share

7
CPSC 457
Fair-share scheduling
● example 1:
○ user 1 has 50% CPU share, and 5 processes: A, B, C, D, E
○ user 2 has 50% CPU share, and 1 process: F
○ possible possible scheduling sequences:
A F B F C F D F E F A F B F C F D F E F A F B F C F D F E F …
A B C D E F F F F F A B C D E F F F F F A B C D E F F F F F …

● example 2:
○ user 1 has 75% CPU share, and 5 processes: A, B, C, D, E
○ user 2 has 25% CPU share, and 1 process: F
○ possible scheduling sequence:
A B C F D E A F B C D F E A B F C D E F A B C F …
A B C D E A B C D E A B C D E F F F F F …
8
CPSC 457
Priority scheduling

● what if processes have different different priorities?


○ we need scheduling algorithms to deal with that
● why would some processes be more important than others?
○ eg. priority could be determined by the VIP status of the owner
○ eg. background service (bittorrent) could have lower priority than an interactive one (game)
○ … many other reasons
● important processes should get more CPU time than less important processes
○ the higher the priority of a process, the higher the CPU time
● priorities can be static or dynamic
○ static ― eg. bittorrent priority = 1, game priority = 10
○ dynamic ― eg. OS automatically increases I/O bound processes priority

9
CPSC 457
Implementing priority scheduling

● with non-preemptive scheduling, a simple approach to handle priorities could work


○ eg. consider SJF-like scheduling, but ready queue is sorted by priority
○ all jobs would eventually finish
○ although a newly added job with very high priority might have to wait for an already running
lower priority job to finish first
○ fix?

● handling priorities with time-sharing preemptive scheduling is more complicated


○ eg. consider RR-like scheduling, where ready queue is sorted by priority
○ immediate problem is starvation
□ a high priority CPU-bound process would ensure no other processes get to run at all

10
CPSC 457
Multilevel queues

● preemptive time-sharing scheduling algorithm


● ready queue is partitioned into separate queues, eg.:
○ foreground queue, for interactive processes, such as browser, game
○ background queue, for non-interactive process, such as weather widget, web server
● each queue can have a different scheduling algorithm, eg.:
○ foreground queue - using RR with time slice of 10ms
○ background queue - using RR with time slice of 100ms
● a process is permanently assigned to one of the queues
● scheduling is done based on queues

11
CPSC 457
Multilevel queues

option 1: static priority scheduling: highest priority

● each queue has a different, but fixed priority system processes


● scheduler processes jobs from the highest priority
queue, until it is empty interactive processes

● once empty, switch to the next highest priority


background processes
queue, etc.
● starvation is a problem batch processes

● is this different from single ready queue?


lowest priority

12
CPSC 457
Multilevel queues

option 2: each queue gets a fixed CPU share foreground queue


80% CPU
● very similar to fair share scheduling
● example: 2 queues: background queue
20% CPU
○ background queue gets 20% CPU
○ foreground queue gets 80% CPU
● processes in foreground queue would share 80% of CPU
● processes in background queue would share 20% of CPU
● not a very dynamic solution

13
CPSC 457
Multilevel feedback queue scheduling
● similar to multilevel queues
○ multiple queues, each representing a different priority
○ scheduling is done based on queues
○ each process belongs to a queue
● but a process can move between queues (up or down in priority)
○ minimizes the starvation problem, reduces convoy effect *
○ can dynamically react to a job changing from CPU-bound to IO-bound
CPU-bound processes moved to low priority queue, IO-bound to high priority queue
● scheduler defined by
○ number of queues
○ scheduling algorithms for each queue, including different time slices
○ when to move a process between queues *
○ which queue a process will be assigned when the process needs service
14
CPSC 457
Multilevel feedback queue scheduling

Example:
● three queues: Q1, Q2 and Q3
● all three queues use RR scheduling with time-slices of 8ms, 16ms and 32ms
● jobs in Q1 are processed first, then Q2, then Q3
● new jobs added to Q1
● if job in Q1 does not finish in 8ms, Q1 : quantum = 8ms
it is demoted to queue 2
● if job in Q2 does not finish in 16ms,
it is demoted to queue 3 Q2 : quantum = 16ms

● if job in Q2 does I/O, it is promoted to Q1


● if job in Q3 does I/O, it is promoted to Q2
Q3 : quantum = 32ms
15
CPSC 457
Lottery scheduling (probabilistic scheduling)
● preemptive time-sharing algorithm
● each process gets some number of 'lottery tickets'
○ number of tickets based on process priority (min. tickets = 1)
higher priority → more tickets
● scheduler picks a random number and process with that ticket 'wins' a time slice of CPU
○ higher priority process has a higher chance of running
○ over long time, job's priority will determine the job's total CPU share
● cooperating processes may exchange tickets
○ allowing dynamic fine-tuning the priorities based on needs
○ eg. in producer/consumer setting the producers could swap tickets with consumers
● starvation is not a problem (why?)
● some technical issues with implementing efficient algorithm for large # tickets, # jobs
16
CPSC 457
Real-Time OS CPU scheduling
● programs are generally expressed as a set of event handlers / tasks responding to events
○ eg. handling interrupts from a device or timer
○ a task in RTOS must respond within a fixed amount of time from time of event (deadline)
○ correctness depends both on the logical result as well as the response time
● tasks deadline types:
○ hard deadline: must meet its deadline, miss will cause system failure, needs Hard RTOS
eg. missing interrupt in a pacemaker device
○ soft deadline: occasional miss is acceptable, needs Soft RTOS, eg. game lag, LOD
○ also firm deadline - between hard/soft, infrequent miss is tolerable, but value of task
completion is 0 after deadline, eg. in automated manufacturing few bad products are OK
● task types:
○ periodic: occurring at regular interval/period
○ aperiodic: occurring unpredictably, deadlines can be for start and/or finish
17
CPSC 457
Rate-monotonic scheduling algorithm (RM)

● simple algorithm, well suited for periodic tasks:


○ task has a static priority based on the inverse of the period of the task
○ shorter period = higher priority, longer period = lower priority
● usually preemptive
○ an event associated with a higher priority task will preempt lower priority task
● a simple formula can be used to determine whether a set of tasks is schedulable
○ schedulable: a schedule exists where no deadlines are missed
○ a new task could be rejected if the system would become unschedulable
● very common RTOS scheduler

18
CPSC 457
Earliest-deadline-first scheduling algorithm (EDF)

● general idea is simple, but more complex to implement than rate-monotonic


○ a task with a shorter deadline has a higher priority (dynamic)
i.e. scheduler picks a task with the earliest deadline
● usually preemptive scheduler
○ an event for higher priority task will preempt lower priority task
● optimal dynamic priority scheduling
○ if a schedule exists, EDF will find it
○ works better for aperiodic tasks than RM
○ can achieve 100% CPU utilization
● simple formula can determine whether tasks can be scheduled without missing deadlines

19
CPSC 457
EDF example
Event
Task Deadline Burst
time
1 0 30 7
2 1 8 5
3 2 20 11
4 3 5 2

Gantt chart:

1 2 2 4 4 2 2 2 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1

20
CPSC 457
Thread scheduling

User-level threads:

● kernel assigns a quantum to process

● threads within process share the


quantum

● each process has its own thread


scheduler

● no clock to interrupt a thread that


runs too long
Example:
● very fast context switch
● 50ms time slice
● 5ms CPU burst per thread
● Possible: A1 A2 A3 A1 A2 A3
● Not possible: A1 B1 A2 B2 A3 B3 21
CPSC 457
Thread scheduling

Kernel-level threads:

● kernel assigns quantum to threads

● a full context switch required

● a thread blocking on I/O will not


suspend the entire process

● modern schedulers will often pick


next thread from the same process to
reduce context switch cost Example:
● 50ms time slice
● 5ms CPU burst per thread
● Possible: A1 A2 A3 A1 A2 A3
● Also possible: A1 B1 A2 B2 A3 B3
22
CPSC 457
Scheduling algorithms
Operating System Preemption Algorithm
Amiga OS Yes Prioritized RR scheduling
FreeBSD Yes Multilevel feedback queue
Linux kernel before 2.6.0 Yes Multilevel feedback queue
Linux kernel 2.6.0–2.6.23 Yes O(1) scheduler
Linux kernel after 2.6.23 Yes Completely Fair Scheduler
classic Mac OS pre-9 None Cooperative scheduler
Preemptive scheduler for MP tasks, and cooperative for
Mac OS 9 Some
processes and threads
macOS Yes Multilevel feedback queue
NetBSD Yes Multilevel feedback queue
Solaris Yes Multilevel feedback queue
Windows 3.1x None Cooperative scheduler
Preemptive scheduler for 32-bit processes, and
Windows 95, 98, Me Half
cooperative for 16-bit processes
Windows NT (including 2000, XP,
Yes Multilevel feedback queue
Vista, 7, and Server)
https://en.wikipedia.org/wiki/Scheduling_(computing) 23
CPSC 457
Other schedulers
● long-term scheduling
○ important in batch systems
○ decides which programs to run, which ones to delay
○ typical goals are to achieve good mix of CPU-bound and IO-bound processes
● medium-term scheduling
○ decides which programs to swap in/out when running low on resources,
or when a process is idle for too long
● short-term scheduling aka CPU scheduling
○ responsible for allocating CPU to processes in memory
ie. managing ready/blocking/waiting states
● I/O scheduling
○ ordering the I/O queues to increase throughput
24
CPSC 457
Recap

Operation
Scheduling Algorithms
Environments
• First come, first serve
Batch systems • Shortest job first
• Shortest remaining time next
• Round robin
• Shortest process next
Interactive • Fair share
systems • Lottery
• Multilevel queue
• Multilevel feedback queue

Real-time • Rate-monotonic scheduling


systems • Earliest-deadline-first

25
CPSC 457
Summary

● scheduling algorithm goals


● scheduling algorithms:
○ shortest-process-next, fair-share, lottery, multilevel queue, multilevel feedback queue
● real-time CPU scheduling:
○ rate-monotonic, earliest-deadline-first
● thread scheduling

Reference: 2.4.1 - 2.4.6 (Modern Operating Systems)


5.2 - 5.4 (Operating System Concepts)

26
CPSC 457
Review

● Why is the length of the time quantum important for RR?


● Will processes experience starvation when using RR scheduling?
Yes or No
● In a multi-level feedback queue, processes are guaranteed to be free from starvation.
True or False
● When using user-level threads, a blocking system call will suspend the entire process?
True or False

27
CPSC 457
Questions?

28

You might also like