CPU Scheduling
CPU Scheduling
Objectives
P1 P2 P3
0 24 27 30
P2 P3 P1
0 3 6 30
• Commonly, α set to ½
Prediction of the Length of the Next CPU Burst
Examples of Exponential Averaging
• =0
• n+1 = n
• Recent history does not count
• =1
• n+1 = tn
• Only the actual last CPU burst counts
• If we expand the formula, we get:
n+1 = tn+(1 - ) tn -1 + …
+(1 - )j tn -j + …
+(1 - )n +1 0
P1 P2 P4 P1 P3
0 1 5 10 17 26
• Run the process with the highest priority. Processes with the same
priority run round-robin
• Example:
Process a Burst Time Priority
P1 4 3
P2 5 2
P3 8 2
P4 7 1
P5 3 3
• Gantt Chart with time quantum = 2
Multilevel Queue
• The ready queue consists of multiple queues
• Multilevel queue scheduler defined by the following
parameters:
• Number of queues
• Scheduling algorithms for each queue
• Method used to determine which queue a process will enter when
that process needs service
• Scheduling among the queues
Multilevel Queue
• With priority scheduling, have separate queues for each priority.
• Schedule the process in the highest-priority queue!
Multilevel Queue
• Three queues:
• Q0 – RR with time quantum 8 milliseconds
• Q1 – RR time quantum 16 milliseconds
• Q2 – FCFS
• Scheduling
• A new process enters queue Q0 which is served
in RR
• When it gains CPU, the process receives 8
milliseconds
• If it does not finish in 8 milliseconds, the process is
moved to queue Q1
• At Q1 job is again served in RR and receives 16
additional milliseconds
• If it still does not complete, it is preempted and
moved to queue Q2
Thread Scheduling
• Distinction between user-level and kernel-level threads
• When threads supported, threads scheduled, not processes
• Many-to-one and many-to-many models, thread library schedules
user-level threads to run on LWP
• Known as process-contention scope (PCS) since scheduling competition is
within the process
• Typically done via priority set by programmer
• Kernel thread scheduled onto available CPU is system-contention
scope (SCS) – competition among all threads in system
Pthread Scheduling
• API allows specifying either PCS or SCS during thread creation
• PTHREAD_SCOPE_PROCESS schedules threads using PCS scheduling
• PTHREAD_SCOPE_SYSTEM schedules threads using SCS scheduling
• Can be limited by OS – Linux and macOS only allow
PTHREAD_SCOPE_SYSTEM
Pthread Scheduling API
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
int main(int argc, char *argv[]) {
int i, scope;
pthread_t tid[NUM THREADS];
pthread_attr_t attr;
/* get the default attributes */
pthread_attr_init(&attr);
/* first inquire on the current scope */
if (pthread_attr_getscope(&attr, &scope) != 0)
fprintf(stderr, "Unable to get scheduling scope\n");
else {
if (scope == PTHREAD_SCOPE_PROCESS)
printf("PTHREAD_SCOPE_PROCESS");
else if (scope == PTHREAD_SCOPE_SYSTEM)
printf("PTHREAD_SCOPE_SYSTEM");
else
fprintf(stderr, "Illegal scope value.\n");
}
Pthread Scheduling API
• Prior to kernel version 2.5, ran variation of standard UNIX scheduling algorithm
• Version 2.5 moved to constant order O(1) scheduling time
• Preemptive, priority based
• Two priority ranges: time-sharing and real-time
• Real-time range from 0 to 99 and nice value from 100 to 140
• Map into global priority with numerically lower values indicating higher priority
• Higher priority gets larger q
• Task run-able as long as time left in time slice (active)
• If no time left (expired), not run-able until all other tasks use their slices
• All run-able tasks tracked in per-CPU runqueue data structure
• Two priority arrays (active, expired)
• Tasks indexed by priority
• When no more active, arrays are exchanged
• Worked well, but poor response times for interactive processes
Linux Scheduling in Version 2.6.23 +
• Completely Fair Scheduler (CFS)
• Scheduling classes
• Each has specific priority
• Scheduler picks highest priority task in highest scheduling class
• Rather than quantum based on fixed time allotments, based on proportion
of CPU time
• Two scheduling classes included, others can be added
1. default
2. real-time
Linux Scheduling in Version 2.6.23 + (Cont.)
• RR is 23ms:
Queueing Models
• Describes the arrival of processes, and CPU and I/O bursts
probabilistically
• Commonly exponential, and described by mean
• Computes average throughput, utilization, waiting time, etc.
• Computer system described as network of servers, each with
queue of waiting processes
• Knowing arrival rates and service rates
• Computes utilization, average queue length, average wait time, etc.
Little’s Formula
• n = average queue length
• W = average waiting time in queue
• λ = average arrival rate into queue
• Little’s law – in steady state, processes leaving queue must
equal processes arriving, thus:
n=λxW
• Valid for any scheduling algorithm and arrival distribution
• For example, if on average 7 processes arrive per second, and
normally 14 processes in queue, then average wait time per
process = 2 seconds
Simulations
• Queueing models limited
• Simulations more accurate
• Programmed model of computer system
• Clock is a variable
• Gather statistics indicating algorithm performance
• Data to drive simulation gathered via
• Random number generator according to probabilities
• Distributions defined mathematically or empirically
• Trace tapes record sequences of real events in real systems
Evaluation of CPU Schedulers by Simulation
Implementation
Even simulations have limited accuracy
Just implement new scheduler and test in real systems
• High cost, high risk
• Environments vary
Most flexible schedulers can be modified per-site or per-system
Or APIs to modify priorities
But again environments vary