CH 5
CH 5
Objectives
• Introduce real-time scheduling and describe and practice with some known algorithms
Nonpreemptive scheduling allows only cases 1 and 4. No process is halted for the
sake of another process, until itself requests I/O or terminates. This is usually a bad
strategy especially for interactive systems.
• Waiting time. Amount of time a process spends waiting in the ready queue minimize.
• Response time. Amount of time it takes from when a request arrives until the first
response is produced. This measure is particularly relevant for interactive systems. It
is the time it takes to start responding, not to complete the whole activity. minimize
We usually minimize or maximize the average for one or more of these criteria.
In some cases, the minimum or maximum might be more important than the average.
For example, for a realtime system, minimize the maximum waiting time (the waiting
time must be bounded for realtime applications)…
FCFS Scheduling (First- Come First-Served)
Simple algorithm. Nonpreemptive. The ready queue implemented as FIFO (of PCB
pointers). Main disadvantage: average waiting time too high and sensitive to the order of
arrival of jobs.
Example: single-core system, 3 processes almost simultaneously (at t=0) with bursts:
P1: 24, P2: 3, P3: 3
Gantt Chart
a) Order of arrival P1 P2 P3.
b) Order of arrival P2 P3 P1
The Convoy effect: when many IO-bound processes have to wait a CPU-bound process. They all
have short CPU bursts but all have to wait the CPU-bound process which has a long CPU burst.
This cycle repeats over and over, because of this non-preemptive scheduling. This is particularly
bad for interactive systems.
SJF Scheduling (Shortest Job First)
This is the optimal solution because it minimizes the average waiting time.
Problem: burst times are not known in advance; they need to be estimated / predicted.
Also, this strategy is non-preemptive.
Gantt
Chart
How to estimate the Length of the next CPU burst of a process? Based on its history.
Each time a process is scheduled, its burst time is measured and its expected burst is
re-estimated.
The exponential moving average is a forecasting method that adjusts the “guess” τ
after each new measure tn in the following way:
τn+1 = α.tn + (1 – α) τn where 0 ≤ α ≤ 1
Example: 4 processes with the following arrival times and burst times:
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Gantt
Chart
The main advantage of RR (and preemptive methods in general) is a better response time.
That is, the time an arriving process has to wait before it gets its first quantum.
Avg. waiting time =(6+4+7)/3 = 5.67 Avg. response time =(0+4+7)/3 = 3.67
Nonpreemptive Priority. The running process is not preempted when a higher priority process
arrives. It continues running until a “normal reason” of scheduling occurs.
Preemptive Priority. The running process is preempted if a higher priority process arrives. To
avoid “starvation”, either we use aging (increase priority with time) or use RR per level: a separate
FIFO queue for each priority level. This approach is called “Multilevel Queue”
Example. Non-preemptive
Non-Preemptive priority:
arrival p1 p2 p3 p4 p5 p6
prio 40 30 30 30 5 10
burst 20 25 25 15 10 10
0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115
p1 p2 p3 p4 p5 p6
• Push migration: A periodic task checks the load on each processor, and might push a
task from an overloaded processor to under-loaded one.
• Pull migration. an idle processor pulls a waiting task from another, busy processor
When a thread runs on a processor, it “populates” its cache. The migration of the thread
to another processor has a cost (repopulating the cache). We say that the thread has
affinity for the processor it is running on (it hates migration).
• Hard Realtime. Typical for embedded systems. Timing constraints are strict.
• Soft Realtime: Typical for multimedia streaming. timing requirements are soft.
A model for realtime processes is that they are periodic; with the following parameters:
• Period p or rate 1/p (rate of events)
• deadline d ≤ p (to finish event processing)
• processing time t ≤ d
Admission Control
is a protocol by which a new process declares its timing requirements to the scheduler, which
admits the process if its requirements can be met, rejects it otherwise.
Usually, the realtime app declares the “worst case” parameters: lowest p, d, and highest t.
Rate Monotonic Scheduling
Assigns each periodic task a static priority based on its rate. High rate => high priority
It is optimal for static priority scheduling: if any set of processes is not admissible by the rate-
monotonic scheduler, it is not admissible by any other scheduler with static priorities
Admission rule: CPU utilization < N(21/N – 1) where N is the number of processes.
Ex. two periodic processes: P1: p1=50=d1, t1=20. P2: p2=100=d2, t2=35. P1 higher priority
CPU utilization = t1/p1 + t2/p2 = 75% < 2(21/2 – 1) = 83%
Ex. 5 processes P1-P5 arrive at t=0 with burst times 10, 29, 3, 7, 12 ms. Compare the average
waiting time for FCFS, SJF and RR (q = 5ms).
RR: 23ms
• Queuing theory is an analytic, mathematical method that relates arrival rates and service times
of processes with their average waiting times. Based on Little’s formula: n = λ W (n: number of
processes in queue, λ: arrival rate, W: avg. waiting time)
• Simulation can also be used to evaluate and compare performance of scheduling algorithms.
Task arrival and service times can be generated from a trace or random distribution profile.
Linux Scheduling
Linux schedules tasks. A task can be either a thread or a single threaded process.
A Linux based system has many Scheduling classes with different priorities. The standard comes
with 2 classes: Normal and realtime. Within each class, further priority levels are defined using a
number in the range [0, 139]. (0 is highest priority)
Realtime tasks have static priority levels (range [0, 99]). Within each level, scheduler uses either non-
preemptive (called FIFO in Linux) or preemptive RR.
For normal tasks (range [100, 139]), Linux uses CFS (Completely Fair Scheduling): each task has a
variable called vruntime, tracking how long that task has used CPU. Scheduler selects the task
with smallest vruntime. The goal is to achieve “fairness”.
To take priority levels into account, CFS gives normal tasks with higher priority a higher proportion
of CPU time. For this, the vruntime of task is scaled by a decay factor based on its
nice_value = priority – 120 (number in the range [-20, 19])
.
> nice + 10 some_prog launches a program “nicely”, with low priority. Good for long batch jobs..
The quantum in Linux is not constant but dynamic, calculated so that each runnable task receive at
least one q during a period called Target Latency. More tasks higher T.L.
Linux supports load balancing but avoids migration across scheduling domains (set of cores with same NUMA
memory, or L2 cache). Migration is done only in extreme cases of load imbalance.
Windows Scheduling
Windows scheduler, called Dispatcher, uses
preemptive priority, with 32 priority levels.
Notes:
• The categories on the first row are modifiable in the windows task manager, under “details” tab.
• In Windows API, they are also settable when a thread is created, and modifiable using the
system call SetPriorityClass().
Textbook readings
From textbook “OS concepts 10th edition”
Reading list
5.1.1, 5.1.2, 5.1.3, 5.2, 5.3.1, 5.3.2, 5.3.3, 5.3.4
5.5.1, 5.5.2, 5.5.3, 5.5.4, 5.6.2, 5.6.3, 5.6.4, 5.6.5
5.7.1, 5.7.2, 5.8.1
Recommended Exercises
5.1, 5.2, 5.3 to 5.5, 5.7, 5.8, 5.11, 5.14, 5.15, 5.16,
5.17, 5.18, 5.22, 5.28 5.35