CSCI315
Operating Systems- CPU Scheduling
Dr. Reem Ibrahim
Dr. Reem Ibrahim
Operating System Concepts, Avi Silberschatz, Peter Baer Galvin, Greg Gagne,
John Wiley & Sons, ISBN 978-1-118-06333-0 , 9th Ed., 2012.
CPU Scheduling
Basic Concepts
Scheduling Criteria
Scheduling Algorithms
Multiple-Processor Scheduling
Real-Time CPU Scheduling
Operating Systems Examples
2 Operating System Dr. Reem Ibrahim
CPU Scheduling
Basic Concepts
CPU Scheduling is a process of determining which
process will own CPU for execution while another
process is on hold. The main task of CPU scheduling is to
make sure that whenever the CPU remains idle, the OS
at least select one of the processes available in the ready
queue for execution. The selection process will be
carried out by the CPU scheduler.
CPU–I/O Burst Cycle – Process execution consists of a
cycle of CPU execution and I/O wait
CPU burst followed by I/O burst
CPU burst distribution is of main concern
CPU burst: the amount of time the process uses the processor
before it is no longer ready waiting for some input or
interrupted by some other process
I/O Burst :It is the amount of time, a process waits for input-
output before needing CPU time.
3 Operating System Dr. Reem Ibrahim
CPU Scheduling
CPU Scheduler
Short-term scheduler selects from among the processes
in ready queue, and allocates the CPU to one of them
Queue may be ordered in various ways
CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
Preemptive nonPreemptive
Scheduling Scheduling
4 Operating System Dr. Reem Ibrahim
Non-preemptive & Preemptive
Scheduling
5 Operating System Dr. Reem Ibrahim
CPU Scheduling
Dispatcher
Dispatcher module gives control of the CPU to the process selected by
the short-term scheduler; this involves:
Switching context
Switching to user mode
Jumping to the proper location in the user program to restart that program
Dispatch latency – time it takes for the dispatcher to stop one process
and start another running.
6 Operating System Dr. Reem Ibrahim
CPU Scheduling
Scheduling Criteria
CPU utilization – keep the CPU as busy as possible MAX
Throughput – # of processes that complete their execution per time
unit MAX
Turnaround time – amount of time to execute a particular
process MIN
Waiting time – amount of time a process has been waiting in the
ready queue MIN
Response time – amount of time it takes from when a request was
submitted until the first response is produced, not output (for time-
sharing environment) MIN
7 Operating System Dr. Reem Ibrahim
Scheduling Algorithms
8 Operating System Dr. Reem Ibrahim
Scheduling Algorithms
First-Come, First-Served Scheduling
Process Burst Time
P1 24
P2 3
P3 3
Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:
P1 P2 P3
0 24 27 30
9 Operating System Dr. Reem Ibrahim
Scheduling Algorithms
First-Come, First-Served Scheduling
Suppose that the processes arrive in the order:
P2 , P3 , P1
The Gantt chart for the schedule is:
P2 P3 P1
0 3 6 30
One of the major drawbacks of this scheme is that the waiting time and
the average turnaround time is often quite long.
10 Operating System Dr. Reem Ibrahim
Scheduling Algorithms
Shortest-Job-First (SJF) Scheduling
Associate with each process the length of its next CPU burst
Use these lengths to schedule the process with the shortest time
SJF is optimal – gives minimum average waiting time for a given set of
processes
The difficulty is knowing the length of the next CPU request
Could ask the user
Can be done by using the length of previous CPU bursts, using exponential
averaging
1. t n actual length of n CPU burst
th
2. n 1 predicted value for the next CPU burst
3. , 0 1
4. Define : n 1 t n 1 n .
Commonly, α set to ½
11 Operating System Dr. Reem Ibrahim
Scheduling Algorithms
Shortest-Job-First (SJF) Scheduling
n 1 t n 1 n .
=0
n+1 = n
Recent history does not
count
=1
n+1 = tn
Only the actual last CPU
burst counts
12 Operating System Dr. Reem Ibrahim
Scheduling Algorithms
Shortest-Job-First (SJF) Scheduling (Non-Preemptive)
ProcessArriva l Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
SJF scheduling chart
P4 P1 P3 P2
0 3 9 16 24
13 Operating System Dr. Reem Ibrahim
Scheduling Algorithms
Shortest-Job-First (SJF) Scheduling
Preemptive version called shortest-remaining-time-first
Example of Shortest-remaining-time-first
Adding the concepts of varying arrival times and preemption to the analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Preemptive SJF Gantt Chart
P1 P2 P4 P1 P3
0 1 5 10 17 26
14 Operating System Dr. Reem Ibrahim
Scheduling Algorithms
Shortest-Job-First (SJF) Scheduling(Preemptive)
P1 P2 P4 P1 P3
0 1 5 10 17 26
15 Operating System Dr. Reem Ibrahim
Scheduling Algorithms
Priority Scheduling
A priority number (integer) is associated with each process
The CPU is allocated to the process with the highest priority (smallest
integer highest priority)
Preemptive
Nonpreemptive
SJF is priority scheduling where priority is the inverse of predicted next CPU
burst time
Problem Starvation – low priority processes may never execute
Solution Aging – as time progresses increase the priority of the process
16 Operating System Dr. Reem Ibrahim
Scheduling Algorithms
Priority Scheduling
Example of Priority Scheduling
Priority scheduling Gantt Chart
17 Operating System Dr. Reem Ibrahim
Scheduling Algorithms
Round Robin (RR) Scheduling
Each process gets a small unit of CPU time (time quantum q),
usually 10-100 milliseconds. After this time has elapsed, the
process is preempted and added to the end of the ready queue.
If there are 𝑛 processes in the ready queue and the time
quantum is 𝑞, then each process gets 1/𝑛 of the CPU time in
chunks of at most 𝑞 time units at once.
No process waits more than (𝑛−1)𝑞time units.
Timer interrupts every quantum to schedule next process
Performance
q large FIFO
q small q must be large with respect to context switch, otherwise
overhead is too high
18 Operating System Dr. Reem Ibrahim
Scheduling Algorithms
Round Robin (RR) Scheduling
The Gantt chart is:
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
19 Operating System Dr. Reem Ibrahim
Scheduling Algorithms
Round Robin (RR) Scheduling
Typically, higher average turnaround than SJF, but better response
q should be large compared to context switch time
q usually 10ms to 100ms, context switch < 10 usec
Time Quantum and Context Switch Time
20 Operating System Dr. Reem Ibrahim
Scheduling Algorithms
Multilevel Queue
Ready queue is partitioned into separate queues, eg:
foreground (interactive)
background (batch)
Process permanently in a given queue
Each queue has its own scheduling algorithm:
foreground – RR
background – FCFS
Scheduling must be done between the queues:
Fixed priority scheduling; (i.e., serve all from foreground then from background).
Possibility of starvation.
Time slice – each queue gets a certain amount of CPU time which it can schedule
amongst its processes; i.e., 80% to foreground in RR, 20% to background in FCFS.
21 Operating System Dr. Reem Ibrahim
Scheduling Algorithms
Multilevel Feedback Queue
A process can move between the various queues; aging can
be implemented this way
Multilevel-feedback-queue scheduler defined by the
following parameters:
number of queues
scheduling algorithms for each queue
method used to determine when to upgrade a process
method used to determine when to demote a process
method used to determine which queue a process will enter
when that process needs service
22 Operating System Dr. Reem Ibrahim
Scheduling Algorithms
Multilevel Queue
Example of Multilevel Feedback Queue
Three queues:
Q0 – RR with time quantum 8 milliseconds
Q1 – RR time quantum 16 milliseconds
Q2 – FCFS
Scheduling
A new job enters queue Q0 which is served FCFS
When it gains CPU, job receives 8 milliseconds
If it does not finish in 8 milliseconds, job is moved to queue Q1
At Q1 job is again served FCFS and receives 16 additional milliseconds
If it still does not complete, it is preempted and moved to queue Q2
23 Operating System Dr. Reem Ibrahim
Scheduling Algorithms
Example of Multilevel Feedback Queue
24 Operating System Dr. Reem Ibrahim
Multiple-Processor Scheduling
CPU scheduling more complex when
multiple CPUs are available
Homogeneous processors within a
multiprocessor
Asymmetric multiprocessing – only
one processor accesses the system data
structures, alleviating the need for data Asymmetric multiprocessing
sharing (Master-Slave)
Symmetric multiprocessing (SMP) –
each processor is self-scheduling, all
processes in common ready queue, or
each has its own private queue of ready
processes
Symmetric multiprocessing
Currently, most common
25 Operating System Dr. Reem Ibrahim
Multiple-Processor Scheduling
Processor affinity – process has affinity for
processor on which it is currently running.
Try to avoid migration of processes from one
processor to another and try to keep a
process running on the same processor. This is
known as Processor affinity.
Soft affinity : refers to an operating
system's policy of keeping a process
running on the same processor, and it will
not ensure that it will do so.
Hard affinity : It enables a process to
NUMA and CPU Scheduling
define a subset of processors on which it
may execute.
The main-memory architecture of a system
can affect processor affinity issues.
NUMA : non-uniform memory access.
26 Operating System Dr. Reem Ibrahim
Multiple-Processor Scheduling
If SMP, need to keep all CPUs loaded for efficiency.
Load Balancing is the phenomena which keeps
the workload evenly distributed across all processors in an SMP system.
Push migration – periodic task checks load on each processor, and if found
pushes task from overloaded CPU to other CPUs
Pull migration – idle processors pulls waiting task from busy processor
27 Operating System Dr. Reem Ibrahim
Multicore Processors
Recent trend to place multiple
processor cores on same physical chip
Faster and consumes less power
Multiple threads per core also
growing
Takes advantage of memory stall to make
progress on another thread while memory
retrieve happens.
When processor accesses memory then it
spends a significant amount of time waiting
for the data to become available. This
situation is called MEMORY STALL
Multithreaded Multicore System
28 Operating System Dr. Reem Ibrahim
Real-Time CPU Scheduling
Real-time systems are systems that
carry real-time tasks. These tasks need
to be performed immediately with a
certain degree of urgency.
Soft real-time systems – no
guarantee as to when critical real-time
process will be scheduled
Hard real-time systems – task
must be serviced by its deadline
Two types of latencies affect
performance
1. Interrupt latency – time from arrival of
interrupt to start of routine that services
interrupt
2. Dispatch latency – time for schedule to take
current process off CPU and switch to another
29 Operating System Dr. Reem Ibrahim
Real-Time CPU Scheduling
Priority-based Scheduling
For real-time scheduling, scheduler must support preemptive, priority-
based scheduling
But only guarantees soft real-time
For hard real-time must also provide ability to meet deadlines
Processes have new characteristics: periodic ones require CPU at
constant intervals
Has processing time t, deadline d, period p
0≤t≤d≤p
Rate of periodic task is 1/p
30 Operating System Dr. Reem Ibrahim
Real-Time CPU Scheduling
Rate-Monotonic Scheduling
A priority is assigned based on the inverse of its period
Shorter periods = higher priority;
Longer periods = lower priority
P1 is assigned a higher priority than P2.
Example: We have two processes, P1 and P2. The periods for P1 and P2
are 50 and 100, respectively—that is, p1 = 50 and p2 = 100. The processing
times are t1 = 20 for P1 and t2 = 35 for P2. The deadline for each process
requires that it complete its CPU burst by the start of its next period
31 Operating System Dr. Reem Ibrahim
Real-Time CPU Scheduling
Rate-Monotonic Scheduling
Another Example: Assume that process P1 has a period of p1
= 50 and a CPU burst of t1 = 25. For P2, the corresponding
values are p2 = 80 and t2 = 35
Missed the deadline for P2 although the total CPU utilization
of the two processes is (25/50)+(35/80) = 0.94
32 Operating System Dr. Reem Ibrahim
Real-Time CPU Scheduling
Earliest Deadline First Scheduling (EDF)
Priorities are assigned according to deadlines:
the earlier the deadline, the higher the priority;
the later the deadline, the lower the priority
Process P1 has the earliest deadline, so its initial priority is
higher than that of process P2.
Whereas rate-monotonic scheduling allows P1 to preempt P2
at the beginning of its next period at time 50, EDF scheduling
allows process P2 to continue running.
33 Operating System Dr. Reem Ibrahim
Operating System Examples
Windows scheduling
Windows uses priority-based preemptive scheduling
Highest-priority thread runs next
Dispatcher is scheduler
Thread runs until (1) calls a blocking system call, such as for I/O, (2) uses time slice, (3)
preempted by higher-priority thread
Real-time threads can preempt non-real-time
32-level priority scheme
Variable class is 1-15, real-time class is 16-31
Priority 0 is memory-management thread
Queue for each priority
If no run-able thread, runs idle thread
Windows API identifies several priority classes to which a process can belong
REALTIME_PRIORITY_CLASS, HIGH_PRIORITY_CLASS,
ABOVE_NORMAL_PRIORITY_CLASS,NORMAL_PRIORITY_CLASS,
BELOW_NORMAL_PRIORITY_CLASS, IDLE_PRIORITY_CLASS
All are variable except REALTIME
34 Operating System Dr. Reem Ibrahim
Operating System Examples
Windows scheduling
A thread within a given priority class has a relative priority
TIME_CRITICAL, HIGHEST, ABOVE_NORMAL, NORMAL, BELOW_NORMAL, LOWEST,
IDLE
Priority class and relative priority combine to give numeric priority
Base priority is NORMAL within the class
If quantum expires, priority lowered, but never below base
If wait occurs, priority boosted depending on what was waited for
Foreground window given 3x priority boost
Windows 7 added User-Mode Scheduling (UMS)
Applications create and manage threads independent of kernel
For large number of threads, much more efficient
UMS schedulers come from programming language libraries like
C++ Concurrent Runtime (ConcRT) framework
35 Operating System Dr. Reem Ibrahim
Operating System Examples
Windows scheduling
The priority of each thread is based on both the priority class it belongs to and its relative
priority within that class. The values of the priority classes appear in the top row. The left
column contains the values for the relative priorities. For example, if the relative priority
of a thread in the ABOVE NORMAL PRIORITY CLASS is NORMAL, the numeric priority of that
thread is 10.
Priority classes
NORMAL relative priority
for each priority class are as follows:
Values for the relative priorities
REALTIME PRIORITY CLASS—24
HIGH PRIORITY CLASS—13
ABOVE NORMAL PRIORITY CLASS—10
NORMAL PRIORITY CLASS—8
BELOW NORMAL PRIORITY CLASS—6
IDLE PRIORITY CLASS—4
Windows thread priorities
36 Operating System Dr. Reem Ibrahim