06 - CPU Scheduling
06 - CPU Scheduling
CS 241
Spring 2021
By
Marwa M. A. Elfattah
Main Reference
Operating System Concepts, Abraham Silbrschatz,
10th Edition
CPU Scheduling
Basic Concepts
Scheduling is a fundamental operating-system
function.
• Almost all computer resources are scheduled
before use.
With multiprogramming:
• Several processes are kept in memory at a time.
• When one process has to wait, the operating
system takes the CPU away from that process
and gives the CPU to another process.
Maximize CPU utilization
Process Execution Cycle
Process execution consists of a
cycle of CPU execution (CPU
burst) followed by I/O wait (I/O
burst)
• An I/O-bound program has
many short CPU bursts.
• A CPU-bound program
might have a few long CPU
bursts.
The final CPU burst ends with a
system request to terminate
execution
CPU Scheduler
The CPU scheduler selects from among the
processes in ready queue, and allocates a CPU
core to one of them
• The ready queue is not necessarily a first-in, first-
out (FIFO) queue.
• The records in the queues are generally process
control blocks (PCBs) of the processes
CPU Scheduler
CPU scheduling decisions may take place when a
process:
1. Switches from running to waiting state
2. Switches from running to ready state (for
example, when an interrupt occurs)
3. Switches from waiting to ready
4. Terminates
For situations 1 and 4, a new process (if one exists
in the ready queue) must be selected for execution.
• The scheduling scheme is nonpreemptive.
CPU Scheduler
CPU scheduling decisions may take place when a
process: Nonpreemptive Scheduling
1.The
Switches
processfrom running
keeps to waiting
the CPU until itstate
releases it
either by terminating
2. Switches from runningortobyready
switching
state to the
(for
example, whenwaiting state.occurs)
an interrupt
3. Switches from waiting to ready
Virtually all modern operating systems
4. Terminates
including Windows, MacOS, Linux, and UNIX
For situations 1 and 4,scheduling
use preemptive a new process (if one exists
algorithms.
in the ready queue) must be selected for execution.
• The scheduling scheme is nonpreemptive.
CPU Scheduler
CPU scheduling decisions may take place when a
process:
1. Switches from running to waiting state
2. Switches from running to ready state (for
example, when an interrupt occurs)
3. Switches from waiting to ready
4. Terminates
For situations 2 and 3, however, the scheduling
scheme is preemptive.
CPU Scheduler
CPU scheduling decisions may take
Preemptive Scheduling place when a
process:
Can result in race conditions when data are
1. Switches
shared from running
among to waiting
several state
processes.
2. Switches from running to ready state (for
Consider
example,thewhen
case of
antwo processes
interrupt that share data.
occurs)
While one process is updating the data, it is preempted
3. Switches
so that thefrom
second waiting
processto can
ready
run. The second
4. process then tries to read the data, which are in an
Terminates
inconsistent state.
For situations 2 and 3, however, the scheduling
scheme is preemptive.
Dispatcher
Dispatcher module gives control of
the CPU core to the process selected
by the CPU scheduler; this involves:
• Switching context
• Switching to user mode
• Jumping to the proper location in
the user program to be resumed
Dispatch latency – time it takes for
the dispatcher to stop one process
and start another running
The dispatcher needs to be as fast
as possible
Scheduling Criteria
In which algorithm is judged to be best:
• CPU utilization – keep the CPU as busy as
possible – Need to be Maximized
• Throughput – # of processes that complete their
execution per time unit – Need to be Maximized
Dependent on the process length
• Turnaround time – amount of time to execute a
particular process – Need to be Minimized
The sum of the periods spent waiting in the
ready queue, executing on the CPU, and
doing I/O.
Scheduling Criteria
In which algorithm is judged to be best:
• Waiting time – amount of time a process has
been waiting in the ready queue – Need to be
Minimized
• Response time – amount of time it takes from
when a request was submitted until the first
response is produced – Need to be Minimized
A process can produce some output fairly
early and continue computing new results
while previous results are being output to the
user.
Thread Scheduling
On most modern operating systems it is kernel-level
threads—not processes—that are being scheduled
• Thread library schedules user-level threads to run
on lightweight process (LWP).
• Each Light Weight Process is attached to a kernel
thread.
• It is kernel threads that the operating system
schedules to run on physical core.
CPU Scheduling Algorithm
The problem which of the processes in the
ready queue is to be allocated the CPU’s core.
Simplifying Assumptions:
• Single processing core
One process at a time
• One thread per process
• Processes are independent
First- Come, First-Served (FCFS) Scheduling
The simplest CPU-scheduling algorithm.
• Implemented as a FIFO queue.
• It is nonpreemptive.
EX: Process Burst Time
P1 24
P2 3
P3 3
Suppose the processes arriving order: P1 , P2 , P3
P1 P2 P1 P4 P1 P3
0 1 2 3 6 14 16
0 ≤ t ≤ d ≤ p