Scheduling
2024-12-12
What does the OS need to implement in order to enable multitasking?
What does the dispatcher have to do?
We already know…
running
process and assigns it to the process, which is the first one in the queueready
and blocked
, the dispatcher removes the corresponding process control blocks from the status lists and accordingly inserts them newrunning
always imply a switch of the process, which is currently executed by the CPUIf a process switches into the state running
or from the state running
to another state, the dispatcher needs to…
store the context (register contents) of the executed process in the process control block (PCB)
assign the CPU to another process
restore the context (register contents) of the process, which will be executed next, from its process control block (PCB)
Which process is executed if no process is on the runqueue?
ready
an idle process gets the CPU assignedThe scheduler of an OS specifies the order in which the dispatcher puts the processes in the state ready
The best scheduling policy (or scheduling algorithm) depends on the use case
The scheduling policy is always a tradeoff between different scheduling criteria
What might be a scheduling criteria?
Scheduling criteria
Scheduling criteria are among others CPU load, response time (latency), turnaround time, throughput, efficiency, real-time behavior (compliance with deadlines), waiting time, overhead, fairness, consideration of priorities, even resource utilization
When to interrupt a running process?
Non-preemptive scheduling or cooperative scheduling
Examples: Windows 3.x, MacOS 8/9, Windows 95/98/Me (for 16-Bit processes)
Preemptive scheduling
Examples: Linux, MacOS X, Windows 95/98/Me (for 32-Bit processes), Windows NT (incl. XP/Visa/7/8/10/11), FreeBSD, RIOT
Preemptive Scheduling in RIOT
In RIOT a running process is only removed from the run queue if a process with a higher priority becomes ready to run.
How can we measure
the performance of a
scheduling policy?
Process | CPU time |
A | \(24\) ms |
B | \(2\) ms |
Which order seems to be preferable?
Execution | Runtime | Average | Waiting time | Average | ||
order | A | B | runtime | A | B | waiting time |
\(P_{A},P_{B}\) | 24 ms | 26 ms | \(\frac{24+26}{2} = 25\) ms | 0 ms | 24 ms | \(\frac{0+24}{2} = 12\) ms |
\(P_{B},P_{A}\) | 26 ms | 2 ms | \(\frac{2+26}{2} = 14\) ms | 2 ms | 0 ms | \(\frac{0+2}{2} = 1\) ms |
ready
gets the CPU assignedSource: William Stallings. Operating Systems. 4th edition. Prentice Hall (2001). P.401
ready
Process | CPU time | Priority |
---|---|---|
A | 8 ms | 15 |
B | 4 ms | 3 |
C | 7 ms | 4 |
D | 13 ms | 8 |
Runtime of the processes
Process | A | B | C | D |
---|---|---|---|---|
Runtime | 32 | 4 | 11 | 24 |
Avg. runtime = \(\frac{32+4+11+24}{4} = 17.75\mbox{ ms}\)
Waiting time of the processes
Process | A | B | C | D |
---|---|---|---|---|
Waiting time | 24 | 0 | 4 | 11 |
Avg. waiting time = \(\frac{24+0+4+11}{4} = 9.75\mbox{ ms}\)
Process | CPU time | Creation time |
---|---|---|
A | 8 ms | 0 ms |
B | 4 ms | 1 ms |
C | 7 ms | 3 ms |
D | 13 ms | 5 ms |
Runtime of the processes
Process | A | B | C | D |
---|---|---|---|---|
Runtime | 8 | 11 | 16 | 27 |
Avg. runtime = \(\frac{8+11+16+27}{4} = 15.5\mbox{ ms}\)
Waiting time of the processes
Process | A | B | C | D |
---|---|---|---|---|
Waiting time | 0 | 7 | 9 | 14 |
Avg. waiting time = \(\frac{0+7+9+14}{4} = 7.5\mbox{ ms}\)
Process | CPU time | Creation time |
---|---|---|
A | 8 ms | 0 ms |
B | 4 ms | 1 ms |
C | 7 ms | 3 ms |
D | 13 ms | 5 ms |
Runtime of the processes
Process | A | B | C | D |
---|---|---|---|---|
Runtime | 8 | 31 | 25 | 16 |
\(\frac{8+31+25+16}{4} = 20\mbox{ ms}\)
Waiting time of the processes
Process | A | B | C | D |
---|---|---|---|---|
Waiting time | 0 | 27 | 18 | 3 |
\(\frac{0+27+18+3}{4} = 12\mbox{ ms}\)
ready
replaces the currently executed processes from the CPU
Process | CPU time | Creation time |
---|---|---|
A | 8 ms | 0 ms |
B | 4 ms | 1 ms |
C | 7 ms | 3 ms |
D | 13 ms | 5 ms |
Runtime of the processes
Process | A | B | C | D |
---|---|---|---|---|
Runtime | 32 | 24 | 20 | 13 |
\(\frac{32+24+20+13}{4} = 22.25\mbox{ ms}\)
Waiting time of the processes
Process | A | B | C | D |
---|---|---|---|---|
Waiting time | 24 | 20 | 13 | 0 |
\(\frac{24+20+13+0}{4} = 14.25\mbox{ ms}\)
Which scheduling strategy may be well suited for generic user space applications?
ready
Process | CPU time | |
---|---|---|
A | 8 ms | |
B | 4 ms | |
C | 7 ms | |
D | 13 ms |
Runtime of the processes
Process | A | B | C | D |
---|---|---|---|---|
Runtime | 26 | 14 | 24 | 32 |
Avg. runtime = \(\frac{26+14+24+32}{4} = 24\mbox{ ms}\)
Waiting time of the processes
Process | A | B | C | D |
---|---|---|---|---|
Waiting time | 18 | 10 | 17 | 19 |
Avg. waiting time = \(\frac{18+10+17+19}{4} = 16\mbox{ ms}\)
ready
Process | CPU time | |
---|---|---|
A | 8 ms | |
B | 4 ms | |
C | 7 ms | |
D | 13 ms |
Runtime of the processes
Process | A | B | C | D |
---|---|---|---|---|
Runtime | 19 | 4 | 11 | 32 |
\(\frac{19+4+11+32}{4} = 16.5\mbox{ ms}\)
Waiting time of the processes
Process | A | B | C | D |
---|---|---|---|---|
Waiting time | 11 | 0 | 4 | 19 |
\(\frac{11+0+4+19}{4} = 8.5\mbox{ ms}\)
ready
in the queue
ready
have a shorter remaining execution time, the process with the shortest remaining execution time gets the CPU assignedready
are compared with the running process only when a new process is created!Process | CPU time | Creation time |
---|---|---|
A | 8 ms | 0 ms |
B | 4 ms | 3 ms |
C | 7 ms | 16 ms |
D | 13 ms | 11 ms |
Runtime of the processes
Process | A | B | C | D |
---|---|---|---|---|
Runtime | 12 | 4 | 7 | 21 |
\(\frac{12+4+7+21}{4} = 11\mbox{ ms}\)
Waiting time of the processes
Process | A | B | C | D |
---|---|---|---|---|
Waiting time | 4 | 0 | 0 | 8 |
\(\frac{4+0+0+8}{4} = 3\mbox{ ms}\)
ready
Process | CPU time | |
---|---|---|
A | 8 ms | |
B | 4 ms | |
C | 7 ms | |
D | 13 ms |
Runtime of the processes
Process | A | B | C | D |
---|---|---|---|---|
Runtime | 21 | 32 | 28 | 13 |
\(\frac{21+32+28+13}{4} = 23.5\mbox{ ms}\)
Waiting time of the processes
Process | A | B | C | D |
---|---|---|---|---|
Waiting time | 13 | 28 | 21 | 0 |
\(\frac{13+28+21+0}{4} = 15.5\mbox{ ms}\)
ready
in the queue
ready
have a longer remaining execution time, the process with the longest remaining execution time gets the CPU assignedready
are compared with the running process only when a new process is created!Process | CPU time | Creation time |
---|---|---|
A | 8 ms | 0 ms |
B | 4 ms | 6 ms |
C | 7 ms | 21 ms |
D | 13 ms | 11 ms |
Runtime of the processes
Process | A | B | C | D |
---|---|---|---|---|
Runtime | 32 | 4 | 7 | 20 |
\(\frac{32+4+7+20}{4} = 15.75\mbox{ ms}\)
Waiting time of the processes
Process | A | B | C | D |
---|---|---|---|---|
Waiting time | 24 | 0 | 0 | 7 |
\(\frac{24+0+0+7}{4} = 7.75\mbox{ ms}\)
Fair variant of SJF/SRTF/LJF/LRTF
The response ratio is calculated for each process \[\mbox{Response ratio} = \frac{\mbox{Estimated execution time} + \mbox{Waiting time}}{\mbox{Estimated execution time}}\]
Response ratio value of a process after creation: 1.0
After process termination or if a process becomes blocked, the CPU is assigned to the process with the highest response ratio
Just as with SJF/SRTF/LJF/LRTF, the execution times of the processes must be estimated via by statistical recordings
It is impossible that processes starve \(\Longrightarrow\) HRRN is fair
ready
state are arranged according to their deadline
ready
ready
Process | CPU time | Deadline |
---|---|---|
A | 8 ms | 25 |
B | 4 ms | 18 |
C | 7 ms | 9 |
D | 13 ms | 34 |
Runtime of the processes
Process | A | B | C | D |
---|---|---|---|---|
Runtime | 19 | 11 | 7 | 32 |
Avg. runtime = \(\frac{19+11+7+32}{4} = 17.25\mbox{ ms}\)
Waiting time of the processes
Process | A | B | C | D |
---|---|---|---|---|
Waiting time | 11 | 7 | 0 | 19 |
Avg. waiting time = \(\frac{11+7+0+19}{4} = 9.25\mbox{ ms}\)
ready
state is split into multiple sublists
Priority | Process class | Scheduling policy |
1 | Real-time processes (time-critical) | Priority-driven scheduling |
2 | Interactive processes | Round Robin |
3 | Compute-intensive batch processes | First Come First Served |
Source: William Stallings. Operating Systems. 4th edition. Prentice Hall (2001). P.413
Many modern operating systems use variants of multilevel feedback scheduling for the scheduling of the processes.
Examples: Linux for regular processes (until Kernel 2.4), Mac OS X, FreeBSD, NetBSD, and the Windows NT family
SCHED_OTHER
process
If a process gets replaced from the CPU core, the vruntime value is increased by the time the process did run on the CPU core
The nodes (processes) in the tree move continuously from right to left
\(\Longrightarrow\) fair distribution of CPU resources
If a process got the CPU core assigned, it can run until its vruntime value has reached the targeted portion of \(1/n\) of the available CPU time
The scheduler aims for an equal vruntime value for all processes
The CFS only takes care of regular (i.e., non-real-time) processes that are assigned to the scheduling policy SCHED_OTHER
nice
values) of the processesnice
value
Scheduling | Fair | CPU time | Takes priorities | ||
NP | P | must be known | into account | ||
Priority-driven scheduling | X | X | no | no | yes |
First Come First Served = FIFO | X | yes | no | no | |
Last Come First Served | X | X | no | no | no |
Round Robin | X | yes | no | no | |
Shortest/Longest Job First | X | no | yes | no | |
Shortest Remaining Time First | X | no | yes | no | |
Longest Remaining Time First | X | no | yes | no | |
Highest Response Ratio Next | X | yes | yes | no | |
Earliest Deadline First | X | X | yes | no | no |
Static multilevel scheduling | X | no | no | yes (static) | |
Multilevel feedback scheduling | X | yes | no | yes (dynamic) | |
Completely Fair Scheduler | X | yes | no | yes |
SCHED_FIFO
(priority-driven scheduling, non-preemptive)SCHED_RR
(preemptive)SCHED_DEADLINE
(EDF scheduling, preemptive)SCHED_OTHER
(default Linux time-sharing scheduling) implemented as…
You should now be able to answer the following questions:
Operating Systems - Scheduling - WS 24/25