You are on page 1of 67

COMPUTER ORGANIZATION AND DESIGN

The Hardware/Software Interface

Chapter 5
CPU Scheduling

Yunmin Go

School of CSEE
Agenda
◼ Basic Concepts
◼ Scheduling Algorithms
◼ Thread Scheduling
◼ Multiple-Processor Scheduling
◼ Real-Time CPU Scheduling
◼ Operating System Example

CPU Scheduling - 2
Basic Concepts
◼ Motivation: maximum CPU utilization obtained with multiprogramming
and multitasking
◼ Resources (including CPU) are shared among processes

Process P0
run wait run wait run wait run

Process P1
run wait run wait run wait run

Multiprogramming

CPU Scheduling - 3
CPU-IO Burst Cycle
◼ Process execution consists of cycle of CPU execution and I/O wait
◼ First and last bursts are CPU bursts

◼ Types of processes
◼ I/O-bound process
◼ Consists of many short CPU bursts
◼ CPU-bound process
◼ Consists of a few long CPU bursts

CPU burst followed by I/O burst

CPU Scheduling - 4
Histogram of CPU-burst Time
◼ CPU burst distribution is of main concern
◼ Large number of short bursts
◼ Small number of longer bursts

CPU Scheduling - 5
CPU Scheduler
◼ CPU scheduler (=short term scheduler)
◼ CPU scheduler selects a process from ready queue and allocates the a CPU
core to one of them

◼ Implementation of ready queue


◼ FIFO queue, priority queue, a tree, or simply an ordered linked list
◼ Each process is represented by PCB

Ready process
selection CPU
queue

CPU Scheduling - 6
When Scheduling Occurs?
◼ 1. Switches from running to waiting state
◼ ex) I/O request or invocation of wait()
◼ 2. Switches from running to ready state
◼ ex) interrupt
◼ 3. Switches from waiting to ready
◼ ex) completion of I/O
◼ 4. Terminates

◼ For situations 1 and 4, there is no choice → Nonpreemtive


◼ For situations 2 and 3, there is a choice → Preemptive

CPU Scheduling - 7
Preemptive Scheduling
◼ Non-preemptive (or cooperative) scheduling
◼ Scheduling can occur at 1, 4 only
◼ Running process is not interrupted

◼ Preemptive scheduling
◼ Scheduling can occurs at 1, 2, 3, 4
◼ Scheduling may occur while a process is running
◼ ex) interrupt, process with higher priority
◼ Requires H/W support and shared data handling
◼ Preemptive scheduling can result in race conditions when data are shared
among several processes

CPU Scheduling - 8
Dispatcher
◼ Dispatcher: a 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

CPU Scheduling - 9
Scheduling Criteria
◼ CPU utilization: keep the CPU as busy as possible
◼ Throughput: # of processes completed per time unit

◼ Turnaround time: interval from submission of a process to its

completion
◼ Waiting time: sum of periods spent waiting in ready queue

◼ Response time: time from submission of a request to first response

→ Importance of each criterion vary with systems

◼ Measure to optimize
◼ Average / minimum / maximum value
◼ Variance
CPU Scheduling - 10
Agenda
◼ Basic Concepts
◼ Scheduling Algorithms
◼ Thread Scheduling
◼ Multiple-Processor Scheduling
◼ Real-Time CPU Scheduling
◼ Operating System Example

CPU Scheduling - 11
Scheduling Algorithms
◼ First-come, first-served (FCFS) scheduling
◼ Shortest-job-first (SJF) scheduling
◼ Priority scheduling
◼ Round-robin scheduling
◼ Multilevel queue scheduling
◼ Multiple feedback-queue scheduling

CPU Scheduling - 12
First-Come, First-Served Scheduling
◼ Process that requests CPU first, is allocated CPU first
◼ Non-preemptive scheduling
◼ Simplest scheduling method
◼ Sometimes average waiting time is quite long
◼ CPU, I/O utilities are inefficient
◼ Ex) Three processes arrived at time 0
Process Burst Time Waiting Time Average waiting time:
P1 24 0 (0 + 24 + 27) / 3 = 17 msec
P2 3 24 What if the processes arrive in the order:
P3 3 27 P2 , P3 , P1?

Gantt Chart P1 P2 P3
0 24 27 30

CPU Scheduling - 13
Shortest-Job-First Scheduling
◼ Assign to the process with the smallest next CPU burst
◼ Ex) Four processes arrived at time 0
Process Burst Time Waiting Time
P1 6 3 Average waiting time:
P2 8 16 (3 + 16 + 9 + 0) / 4 = 7 msec
P3 7 9
P4 3 0

P4 P1 P3 P2
0 3 9 16 24

◼ SJF algorithm is optimal in minimum waiting time


◼ Problem: difficult to know length of next CPU burst

CPU Scheduling - 14
Shortest-Job-First Scheduling
◼ Predicting next CPU burst from history
◼ Exponential averaging
𝜏𝑛+1 = 𝛼𝑡𝑛 + 1 − 𝛼 𝜏𝑛
◼ 𝑡𝑛 : actual length of n-th CPU burst
◼ 𝜏𝑛 : prediction for n-th CPU burst
◼ 𝛼 : a coefficient between 0 and 1
◼ 𝛼 = 0 : recent history has no effect
◼ 𝛼 = 1 : only recent history matters
◼ Usually, 𝛼 = 0.5
◼ If we expand the formula, we get:
𝜏𝑛+1 = 𝛼𝑡𝑛 + 1 − 𝛼 𝛼𝑡𝑛−1 + ⋯ + 1 − 𝛼 𝑗 𝛼𝑡𝑛−𝑗 + ⋯ + 1 − 𝛼 𝑛+1 𝜏
0

◼ Since both 𝛼 and 1 − 𝛼 are less than or equal to 1, each successive term has less weight than
its predecessor
CPU Scheduling - 15
Shortest-Job-First Scheduling
◼ Prediction of CPU Burst
n+1 = tn + (1-) n

CPU Scheduling - 16
Shortest-Job-First Scheduling
◼ Preemptive version of SJF scheduling
◼ Shortest-remaining-time-first scheduling

Process Arrival Time Burst Time Waiting Time


P1 0 8 9 (= 10 – 1)
Average waiting time:
P2 1 4 0 (= 1 – 1)
(9 + 0 + 15 + 2) / 4 = 26 / 4 = 6.5
P3 2 9 15 (= 17 – 2)
P4 3 5 2 = (5 – 3)

P1 P2 P4 P1 P3
0 1 5 10 17 26

CPU Scheduling - 17
Round-Robin Scheduling
◼ Similar to FCFS, but it’s preemptive
◼ Designed for time-sharing systems
◼ CPU time is divided into time quantum (or time slice)
◼ A time quantum is 10~100 msec.
Cf. switching latency: 10 sec
◼ Ready queue is treaded as circular queue
◼ CPU scheduler goes around the ready queue and allocate CPU time up to 1
time quantum

CPU Scheduling - 18
Round-Robin Scheduling
◼ Example of RR scheduling with time quantum = 4
◼ Ex) Three processes arrived at time 0
Process Burst Time Waiting Time
P1 24 6 Average waiting time:
P2 3 4 (6 + 4 + 7) / 3 = 5.66
P3 3 7

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

CPU Scheduling - 19
Round-Robin Scheduling
◼ Performance of RR scheduling heavily depends on size of time
quantum
◼ Time quantum is small: processor sharing
◼ Time quantum is large: FCFS
◼ Context switching overhead depends on size of time quantum

CPU Scheduling - 20
Round-Robin Scheduling
◼ Turnaround time also depends on
size of time quantum
◼ Average turnaround time is not
proportional nor inverse-proportional to
size of time quantum
◼ Average turnaround time is improved if
most processes finish their next CPU
burst in a single time quantum
◼ However, too long time quantum is not
desirable
◼ A rule of thumb: about 80% of CPU burst
should be shorter than time quantum

CPU Scheduling - 21
Priority Scheduling
◼ CPU is allocated the process with the highest priority
◼ Each process has its priority
◼ In this text, lower number means higher priority
◼ Equal-priority processes: FCFS
◼ Ex) Five processes arrived at time 0
Process Burst Time Priority Waiting Time
P1 10 3 6
Average waiting time:
P2 1 1 0 (6 + 0 + 16 + 18 + 1) / 5 = 8.2
P3 2 4 16
P4 1 5 18
P5 5 2 1

CPU Scheduling - 22
Priority Scheduling
◼ Priority can be assigned internally and externally
◼ Internally: determined by measurable quantity or qualities
◼ Time limit, memory requirement, # of open files, ratio of I/O burst and CPU burst, …
◼ Externally: importance, political factors
◼ Priority scheduling can be either preemptive or non-preemptive.
◼ Major problems
◼Indefinite blocking (= starvation) of processes with lower priorities
→ Solution: aging (gradually increase priority of processes waiting for long time)

CPU Scheduling - 23
Priority Scheduling
◼ Priority scheduling with Round-Robin scheduling
◼ Run the process with the highest priority
◼ Processes with the same priority run round-robin
◼ Ex) 2 msec time quantum for RR
Process Burst Time Priority Waiting Time
P1 4 3 22 (=20+2)
P2 5 2 11 (=7+2+2) Average waiting time:
(22 + 11 + 12 + 0 + 24) / 5 = 13.8
P3 8 2 12 (=9+2+1)
P4 7 1 0
P5 3 3 24 (=22+2)

CPU Scheduling - 24
Multilevel Queue Scheduling
◼ Classify processes into different groups and apply different
scheduling
◼ Memory requirement, priority, process type, …
◼ Partition ready queue into several separate queues

CPU Scheduling - 25
Multilevel Queue Scheduling
◼ Each queue has its own scheduling algorithm
◼ Scheduling among queues
◼ Fixed-priority preemptive scheduling
◼ A process in lower priority queue can run only when all of higher priority queues all empty
◼ Time-slice among queues
Ex) foreground queue (interactive processes): 80% of the CPU time for RR
background queue (batch processes): 20% of the CPU time for FCFS

CPU Scheduling - 26
Multilevel Feedback-Queue Scheduling
◼ Similar to multilevel queue scheduling, but a process can move
between queues
◼ Idea: separate processes according to characteristics of their CPU
bursts.
◼ If a process uses too much CPU time, move it to lower priority queue
◼ I/O-bound, interactive processes are in higher priority queues

CPU Scheduling - 27
Multilevel Feedback-Queue Scheduling
◼ Example of multilevel feedback queue
◼ Ready queue consists of three queues (0~2)
◼ Q0 : RR with time quantum 8 msec
◼ Q1 : RR with time quantum 16 msec
◼ Q2 : FCFS
→ A new process is put in Q0.
If it exceeds time limit, it moves to
lower priority queue

CPU Scheduling - 28
Multilevel Feedback-Queue Scheduling
◼ Parameters to define a multilevel feedback-queue scheduler
◼ # of queues
◼ Scheduling algorithm for each queue
◼ Method to determine when to upgrade a process to higher priority queue
◼ Method to determine when to demote a process to lower priority queue
◼ Method to determine which queue a process will enter when it needs service

→ The most complex algorithm

CPU Scheduling - 29
Agenda
◼ Basic Concepts
◼ Scheduling Algorithms
◼ Thread Scheduling
◼ Multiple-Processor Scheduling
◼ Real-Time CPU Scheduling
◼ Operating System Example

CPU Scheduling - 30
Thread Scheduling
◼ On most modern operating systems it is kernel-level threads (not
processes) that are being scheduled by the operating system
◼ User-level threads are managed by a thread library
◼ To run on a CPU, user-level threads must ultimately be mapped to an
associated kernel-level thread
◼ This mapping may be indirect and may use lightweight process (LWP)

CPU Scheduling - 31
Contention Scope
◼ Process-contention scope (PCS)
◼ Competition for LWP among user threads
◼ Many-to-one model or many-to-many model
◼ Priority based (set by programmer)
<Many-to-One model>

◼ System-contention scope (SCS)


◼ Competition for CPU among kernel threads

<Many-to-Many model>

CPU Scheduling - 32
Scheduler Activation and LWP
◼ Connection between user / kernel threads through LWP

CPU Scheduling - 33
Pthread Scheduling
◼ In thread creation with Pthreads, we can specify PCS or SCS
◼ PCS: PTHREAD_SCOPE_PROCESS
◼ In many-to-one, only PCS is possible
◼ SCS: PTHREAD_SCOPE_SYSTEM
◼ In one-to-one model, only SCS is possible
◼ Note: On certain system, only certain values are allowed
◼ Linux and macOS systems allow only PTHREAD_SCOPE_SYSTEM

◼ Related functions
◼ pthread_attr_setscope(pthread_attr_t *attr, int scope)
◼ pthread_attr_getscope(pthread_attr_t *attr, int *scope)

CPU Scheduling - 34
Example: Pthread Scheduling
#include <pthread.h> // set the scheduling algorithm to PCS or SCS
#include <stdio.h> pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
#define NUM_THREADS 5 // create the threads
void *runner(void *param); for (i = 0; i < NUM_THREADS; i++)
pthread_create(&tid[i], &attr,runner,NULL);
int main(int argc, char *argv[]) { // now join on each thread
int i, scope; for (i = 0; i < NUM_THREADS; i++)
pthread_t tid[NUM THREADS]; pthread_join(tid[i], NULL);
pthread_attr_t attr; }
// get the default attributes
pthread_attr_init(&attr); // Each thread will begin control in this function
// first inquire on the current scope void *runner(void *param)
if (pthread_attr_getscope(&attr, &scope) != 0) {
fprintf(stderr, "Unable to get scheduling scope\n"); // do some work
else { pthread_exit(0);
if (scope == PTHREAD_SCOPE_PROCESS) }
printf("PTHREAD_SCOPE_PROCESS\n");
else if (scope == PTHREAD_SCOPE_SYSTEM)
printf("PTHREAD_SCOPE_SYSTEM\n");
else
fprintf(stderr, "Illegal scope value.\n");
}
CPU Scheduling - 35
Agenda
◼ Basic Concepts
◼ Scheduling Algorithms
◼ Thread Scheduling
◼ Multiple-Processor Scheduling
◼ Real-Time CPU Scheduling
◼ Operating System Example

CPU Scheduling - 36
Multi-Processor Scheduling
◼ CPU scheduling more complex when multiple CPUs are available
◼ There are many trials in multiple-processor scheduling, but no
generally best solution
◼ Multiprocess may be any one of the following architectures:
◼ Multicore CPUs
◼ Multithreaded cores
◼ NUMA systems
◼ Heterogeneous multiprocessing

CPU Scheduling - 37
Multi-Processor Scheduling
◼ Symmetric vs. Asymmetric Multiprocessing

Symmetric multiprocessing
(SMP) is where each processor
is self scheduling

CPU Scheduling - 38
Multi-Processor Scheduling
◼ Two possible strategies
◼ All threads may be in a common ready queue
◼ Needs to ensure two processors do not choose the same thread nor lost from the queue
◼ Each processor may have its own private queue of threads

CPU Scheduling - 39
Multicore Processors
◼ Multicore processor: multiple processor cores on same physical
chip
◼ Recent trend
◼ Faster and consumes less power than systems in which each processor has
its own physical chip
◼ Scheduling issues on multi-core processor
◼ Memory stall: for various reasons, memory access spends significant amount
time waiting for the data to become available.
Ex) accessing data not in cache

CPU Scheduling - 40
Multithreaded Multicore System
◼ Scheduling issues on multi-core processor
◼ Remedy: multithreaded processor cores
◼ Two or more hardware threads are assigned to each core
◼ If one thread has a memory stall, switch to another thread!

CPU Scheduling - 41
Multithreaded Multicore System
◼ Chip-multithreading (CMT) assigns
each core multiple hardware threads.
◼ Intel refers to this as hyperthreading (a.k.a
simultaneous multithreading or SMT)
◼ Coarse-grained multithreading
◼ Fine-grained multithreading

◼ Ex) On a quad-core system with 2 hardware


threads per core, the operating system sees
8 logical processors.

CPU Scheduling - 42
Multithreaded Multicore System
◼ Two levels of scheduling

Level 1: the operating system


deciding which software thread to
run on a logical CPU

Level 2: How each core decides


which hardware thread to run on
the physical core.

CPU Scheduling - 43
Load Balancing
◼ Load balancing: attempts to keep workload evenly distributed
across all processors
◼ Necessary for system where each processor has its own ready queue (It’s true
for most modern OS’s)
◼ Two general approaches
◼ Push migration
◼ Periodic task checks load on each processor
◼ If unbalance is found, pushes task from overloaded CPU to other CPUs
◼ Pull migration
◼ Idle processors pulls waiting task from busy processor
Note: Push and pull migration can be implemented in parallel.
→ Linux CFS, ULE scheduler for FreeBSD
CPU Scheduling - 44
Processor Affinity
◼ Overhead of migration of processes from one processor to another
processor
◼ All contents of cache should be invalidated and re-populated

◼ Processor affinity: keeping a process running on the same


processor to avoid migration overhead
◼ Soft affinity: Although OS attempts to keep a process running on the same
processor, a process may migrate between processors.
◼ Hard affinity: It is possible to prevent a process from being migrated to other
processor

CPU Scheduling - 45
Processor Affinity
◼ NUMA(Non-Uniform Memory Access) and CPU scheduling
◼ A CPU has faster access to some parts of main memory than to other parts

CPU Scheduling - 46
Agenda
◼ Basic Concepts
◼ Scheduling Algorithms
◼ Thread Scheduling
◼ Multiple-Processor Scheduling
◼ Real-Time CPU Scheduling
◼ Operating System Example

CPU Scheduling - 47
Real-time CPU Scheduling
◼ Real-time Operating Systems (RTOS)
◼ OS intended to serve real-time systems
Ex) Antirock brake system (ABS) requires latency of 3~5 msec
◼ Soft real-time systems
◼ No guarantee as to when a critical real-time process will be scheduled.
◼ Guarantee only that the process will be given preference over noncritical
processes.
◼ Hard real-time systems
◼ A task must be serviced by its deadline
◼ Service after the deadline has expired is the same as no service at all.

CPU Scheduling - 48
Minimizing Latency
◼ Most real-time systems are event-driven system
◼ When an event occurs, the system must respond to and service it as quickly
as possible
◼ Event latency: the amount of time that elapses from when an event
occurs to when it is serviced
◼ Interrupt latency, dispatch latency

CPU Scheduling - 49
Interrupt Latency
◼ Interrupt latency: the period of time from the arrival of interrupt at
the CPU to the start of the routine that services the interrupt.

CPU Scheduling - 50
Dispatch Latency
◼ Dispatch latency: the amount of time required for the scheduling
dispatcher to stop one process and start another
◼ Preemptive kernels are the most effective technique to keep dispatch latency
low
◼ Conflict phase of dispatch latency
◼ 1. Preemption of any process running
in kernel mode
◼ 2. Release by low-priority process of
resources needed by high-priority
processes

CPU Scheduling - 51
Priority-based Scheduling
◼ The most important feature of RTOS is to respond immediately to a
real-time process
◼ The scheduler should support priority-based preemptive algorithm
◼ Most OS assign highest priority to real-time processes
◼ Preemptive, priority-based scheduler only guarantees soft real-time
functionality.
◼ Hard real-time systems must further guarantee that real-time tasks
will be serviced in accord with their deadline requirements
◼ Requires additional scheduling features.

CPU Scheduling - 52
Periodic Process
◼ Periodic processes require the CPU at constant intervals (periods).
◼ Processing time: 𝑡
◼ Deadline: 𝑑 (by which it must be serviced by the CPU)
◼ Period: 𝑝
◼ Rate of a periodic task: 1/𝑝

0≤𝑡≤𝑑≤𝑝

CPU Scheduling - 53
Scheduling with Deadline Requirement
◼ A process may have to announce its deadline requirements to the
scheduler
◼ Using an admission-control algorithm, the scheduler does one of two
things:
◼ Admits the process, guaranteeing that the process will complete on time, or
◼ Rejects the request as impossible if it cannot guarantee that the task will be
serviced by its deadline.

CPU Scheduling - 54
Rate-Monotonic Scheduling
◼ Rate-monotonic scheduling algorithm
◼ Schedules periodic tasks using a static priority policy with preemption
◼ Each periodic task is assigned a priority inversely based on its period
◼ Shorter periods → high priority
◼ Longer periods → lower priority
◼ The processing time of a periodic process is assumed the same for each CPU
burst

Process 𝑃1 : 𝑝1 = 50, 𝑡1 = 20, Process 𝑃2 : 𝑝2 = 100, 𝑡2 = 35

CPU Scheduling - 55
Rate-Monotonic Scheduling
◼ Rate-monotonic scheduling is considered optimal
◼ If a set of processes cannot be scheduled by this algorithm, it cannot be
scheduled by any other algorithm that assigns static priorities.
◼ Limitation
◼ CPU utilization is bounded, and it is not always possible to maximize CPU
resources fully.
◼ Worst-case CPU utilization: 𝑁(21/𝑁 − 1)
◼ 𝑁: # of processes
Process 𝑃1 : 𝑝1 = 50, 𝑡1 = 25, Process 𝑃2 : 𝑝2 = 80, 𝑡2 = 35
Process P2 misses
finishing its deadline
at time 80

CPU Scheduling - 56
Earliest-Deadline-First Scheduling
◼ Earliest-deadline-first (EDF) scheduling
◼ Priorities are assigned according to deadline
◼ The earlier the deadline, the higher the priority
◼ The later the deadline, the lower priority

Process 𝑃1 : 𝑝1 = 50, 𝑡1 = 25, Process 𝑃2 : 𝑝2 = 80, 𝑡2 = 35

CPU Scheduling - 57
Earliest-Deadline-First Scheduling
◼ Requirements
◼ Does not require that processes be periodic, nor must a process require a
constant amount of CPU time per burst
◼ When a process becomes runnable, it must announce its deadline
requirements to the system
◼ Theoretically optimal
◼ Theoretically, it can schedule processes so that each process can meet its
deadline requirements and CPU utilization will be 100 percent.

CPU Scheduling - 58
Proportional Share Scheduling
◼ Proportional share schedulers
◼ Allocates 𝑇 shares among all applications.
◼ An application can receive 𝑁 shares of time, thus ensuring that the application
will have 𝑁 ∕ 𝑇 of the total processor time
◼ Must work in conjunction with an admission-control policy to guarantee that an
application receives its allocated shares of time

CPU Scheduling - 59
POSIX Real-Time Scheduling
◼ Scheduling classes for real-time threads
◼ SCHED_FIFO: FCFS policy
◼ No time slicing among threads of equal priority
◼ The highest-priority real-time thread at the front of the FIFO queue will be granted the CPU
until it terminates or blocks
◼ SCHED_RR: a round-robin policy.
◼ Similar to SCHED_FIFO
◼ Time slicing among threads of equal priority.
◼ SCHED_OTHER: system-specific
◼ Implementation is undefined
◼ POSIX API for getting/setting scheduling policy
◼ pthread_attr getschedpolicy(pthread_attr_t *attr, int *policy);
◼ pthread_attr setschedpolicy(pthread_attr_t *attr, int policy);
CPU Scheduling - 60
Example: POSIX Real-Time Scheduling
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
// set the scheduling policy - FIFO, RR, or OTHER
int main(int argc, char *argv[]) if (pthread_attr_setschedpolicy(&attr, SCHED_FIFO) != 0)
{ fprintf(stderr, "Unable to set policy.\n");
int i, policy; // create the threads
pthread_t_tid[NUM_THREADS]; for (i = 0; i < NUM_THREADS; i++)
pthread_attr_t attr; pthread_create(&tid[i],&attr,runner,NULL);
// get the default attributes // now join on each thread
pthread_attr_init(&attr); for (i = 0; i < NUM_THREADS; i++)
// get the current scheduling policy pthread_join(tid[i], NULL);
if (pthread_attr_getschedpolicy(&attr, &policy) != 0) }
fprintf(stderr, "Unable to get policy.\n");
else { // Each thread will begin control in this function
if (policy == SCHED_OTHER) void *runner(void *param)
printf("SCHED_OTHER\n"); {
else if (policy == SCHED_RR) // do some work ...
printf("SCHED_RR\n"); pthread_exit(0);
else if (policy == SCHED_FIFO) }
printf("SCHED_FIFO\n");
}

CPU Scheduling - 61
Agenda
◼ Basic Concepts
◼ Scheduling Algorithms
◼ Thread Scheduling
◼ Multiple-Processor Scheduling
◼ Real-Time CPU Scheduling
◼ Operating System Example

CPU Scheduling - 62
Linux Scheduler
◼ History
◼ Traditional UNIX scheduling algorithm (ver. < 2.5)
◼ Not adequate support for SMP system
◼ Not scale well as # of tasks increases
◼ O(1) scheduling algorithm (ver. ≥ 2.5)
◼ O(1) complexity
◼ Increased support for SMP
◼ Poor response time interactive processes
◼ Completely Fair Scheduler (CFS) (ver. ≥ 2.6)

CPU Scheduling - 63
Linux Scheduler
◼ Based on scheduling classes
◼ Each class is assigned a specific priority
◼ Default scheduling class (CFS algorithm)
◼ Static priority 100 ~ 139
◼ Real-time scheduling class (SCHED_FIFO, SCHED_RR)
◼ Static priority 0 ~ 99
◼ If necessary, new scheduling classes can be added
◼ Allows different scheduling algorithms based on the needs of the system (e.g.
server vs. mobile)
◼ Scheduler selects highest-priority task belonging to highest-priority class

CPU Scheduling - 64
Linux CFS Scheduler
◼ CFS scheduler assigns a proportion of CPU processing time to each
task
◼ The portion is calculated from nice value
◼ Nice value: relative priority in [-20, +19], default is 0
◼ Higher value represents lower priority (‘nice’ to other tasks)
◼ Proportions of CPU time are allocated from the value of targeted latency
◼ Targeted latency: interval of time during which every runnable task should run at least
once
◼ Can increase if the number of active tasks in the system grows beyond a certain threshold

CPU Scheduling - 65
Linux CFS Scheduler
◼ Records how long each task has run
◼ per task variable vruntime (virtual run time)
◼ Normal priority tasks: vruntime = <actual physical run time>
◼ vruntime of each task is associated with a decay factor
◼ Lower priority task: higher decay factor
◼ Higher priority task: lower decay factor

➔ Higher priority tasks are likely to have smaller vruntime


◼ The scheduler simply selects the task that has the smallest
vruntime value
◼ Higher-priority task can preempt a lower-priority task.

CPU Scheduling - 66
Linux CFS Scheduler
◼ I/O-bound tasks: run only for short periods before blocking for
additional I/O
→ vruntime will be lower
◼ CPU-bound tasks: exhaust its time period whenever it has an
opportunity to run on a processor
→ vruntime will be higher

→ Gives I/O-bound tasks higher priority than CPU-bound tasks

CPU Scheduling - 67

You might also like