When the CPU becomes available, as a result of
the CPU scheduler has the responsibility of deciding which process (or
use the CPU next.
- end of time-slice
1. The CPU-I/O burst cycle
Programs tend to alternate between periods of CPU activity and I/O
I/O bound processes have short CPU bursts. CPU processes have
2. Preemptive vs. nonpreemptive scheduling
In preemptive scheduling, a process may lose control of the CPU due to
Preemption prevents one process from getting too much CPU time.
-- it results in more overhead from context switching and more
problems in accessing shared data.
- end of time slice
- a higher priority process becoming ready
Goals of a scheduling policy:
- Maximize CPU utilization; i.e., keep the CPU busy.
- Maximize throughput; that is, the number of processes completed
- Minimize turnaround time
- Minimize wait time
- Minimize response time for interactive users
- Minimize variability in response time; i.e., be predictable
- Degrade gracefully under heavy loads
Some scheduling policies
1. FCFS (First come, first served)
Problem: One CPU-bound process can hold the CPU indefinitely.
"convoy effect" -- Many I/O bound processes may get stuck
a single CPU-bound process.
2. Round robin
Q: How to set the quantum?
- Preemptive version of FCFS
- Each process gets the CPU for a time slice or quantum.
- Fairer than FCFS -- Each process gets the same percentage of CPU
- No starvation possible.
too high -- degenerates to FCFS
too low -- too much overhead in context switches
Rule of thumb: Choose quantum so that most of the time, a process
up the CPU voluntarily; i.e., before the end of its quantum.
3. Shortest Job First (SJF)
Estimate the length of the next CPU burst for each
schedule processes in increasing order.
- Minimizes average wait time
May be preemptive or nonpreemptive. The preemptive version is
SRTF (Shortest Remaining Time First).
- Difficult to predict burst time
- Starvation of CPU-bound processes may occur. (Aging can be
to alleviate this.)
Aside: Gantt charts can be used to illustrate scheduling behavior.
Suppose that a system has three processes, with the following table of
times and burst times:
A Gantt chart shows a timeline of CPU usage, as follows:
4. Priority scheduling
(HPF -- Highest Priority First)
- How to assign priority?
- external vs. internal
- static vs. dynamic
- Preemptive vs. nonpreemptive
- Starvation problem
- Absolute or "fair share"?
5. Multilevel queue
6. Multilevel feedback queues
- Partition the ready queue into two or more queues with different
of process; e.g., interactive vs. batch vs. real-time vs. system
- Different queues may have different priorities, different
Dynamic prioritization based on CPU burst characteristics
CPU bound ==> lower priority, longer time slice
- Two or more queues, ordered by priority
- Processes start in first queue (highest priority)
- High priority queue has shortest time slice; lowest priority
has longest time slice
- If a process times out, it is demoted to the next lower queue
- Over time, I/O bound processes remain in high priority queue, CPU
processes drift down to lower priority queues
I/O bound ==> higher priority, shorter time slice
A multiprocessor is a computer with more than one identical CPU and a
The scheduling problem for multiprocessors can be viewed as the problem
Goal: Share the workload among all the processors.
Most modern operating systems use symmetric multiprocessing with
private ready queues.
- Symmetric vs. asymmetric multiprocessing
- asymmetric: One processor is master, the others are
slaves. The master runs the scheduling algorithm to schedule work
for others and itself.
- symmetric: Processors are equivalent. Each
- Single ready queue vs. private ready queues (i.e., one per
- Single ready queue
- Simple conceptually
- Problem: Contention in accessing the ready queue.
- Problem: Cache invalidation and repopulation.
- Bias the scheduler so that a process is more likely to run
on the same processor as it did the last time.
- Processor affinity
- soft affinity
- hard affinity
- Private ready queues
- Problem: Load balancing is more difficult.
- Balance load by allowing a process to migrate from one processor to
- Push migration -- A load balancing task pushes processes from overloaded
processors to underloaded ones
- Pull migration -- An idle processor pulls a process from the ready
queue of another processor
Multiprocessor scheduling may also depend on the hardware architecture
- UMA - uniform memory access time
- NUMA - nonuniform memory access time
- multicore, multithreaded processor chips
If the operating system supports kernel threads, then threads are the
basic unit of scheduling.
many-to-one model (many user threads are mapped onto one process
or kernel thread.) The process, as viewed by the kernel, is a
single scheduling unit and competes with other processes for CPU
time. The kernel is unaware of the user threads, so it doesn't
make any scheduling decisions for them. The user thread library
has its own scheduler that determines which thread to run when the
process has use of the CPU.
one-to-one model (one kernel thread per user thread) Each
user thread is mapped onto a kernel thread. The kernel is
responsible for scheduling the kernel threads, so it effectively
schedules the user threads at the same time. The user library
code does not need to take any responsibility for scheduling.
many-to-many model (several user threads are mapped onto several
kernel threads) The kernel schedules the kernel threads.
The user thread library needs a scheduler whose responsibility it is to
map the user threads onto the kernel threads. So we have two
levels of scheduling. For example, if the running user thread
exits, the exit routine calls the user-level scheduler, which finds an
unbound user thread and transfers control to it. The old thread,
the new thread, and the user-level scheduler all run on the same kernel
Q: Should the kernel-level scheduler take into account which
process each thread belongs to when making scheduling decisions?
Pthread contention scope
The Pthread library supports the notion of contention scope, which may be
either process (PCS) or system (SCS). The term indicates whether,
in competing for CPU time, a thread competes with all other threads on
the system or just those owned by the same process.
- PCS - many-to-many or many-to-one
- SCS - Each user-level thread is mapped to a distinct kernel-level
thread. We have one-to-one scheduling.
- Threads are divided into six classes:
- fair share
- fixed priority
- Time-sharing and interactive classes use multilevel feedback
- Real-time tasks have static priorities; other tasks have a base
priority which can be adjusted depending on behavior.
- Higher priority tasks are given longer time slices.
- Two queue structures: active (tasks which have not
exhausted their time slices) and expired (tasks which have).