Os 05 Scheduling
Os 05 Scheduling
1 / 25
(Processor) Scheduling
Def. A Scheduler is an OS component wich decides
3 / 25
Offline vs. Online Scheduling
1. Offline
the complete plan is computed before the system is started
Every relevant parameter (such as start time, execution time,
precedence relations, . . . ) of every activity must be known a priori
(beforehand).
When system runs, it simply follows the pre-computed schedule –
no decisions are necessary.
RL example: your (and my) timetable
This is inflexible, but the schedule can be (nearly) optimal (and it
could be verified for some properties).
Typical fields of application: Autonomous and Real-Time Systems
4 / 25
Offline vs. Online Scheduling
2. Online
Decision “Who is next?” is done while the system is running
no time for lengthy computation and optimization → compromise
between optimality of chosen process and time for selection
necessary
flexible: system can adapt to changes in process set, user priority,
events from the outside, . . .
Typical interactive OS like Windows or Linux use online scheduling.
5 / 25
Example: Shortest Job Next (SJN)
Basic Idea: Rapidly complete some short jobs before a long one.
Def. (SJN) Among all ready processes, always choose the shortest job
for execution.
Remarks
Ties must be broken arbitrarily.
usable for uni- and multiprocessor systems
Can be done online and offline.
Knowledge on execution time (te ) of every process necessary
minimizes average turnaround time (t t ) and average waiting time
(t w )
unfair, (processes could starve for online variant)
6 / 25
Shortest Job Next (SJN)
Example: (4 Processes, 1 CPU)
Process P1 P2 P3 P4
te 6 8 7 3
P4 P1 P3 P2
0 3 9 16 24 t
0 + 3 + 9 + 16 3 + 9 + 16 + 24
tw = =7 tv = = 13
4 4
7 / 25
Process Precedence
8 / 25
The Precedence Graph - Example
p21 → p12 , p12 → p22 , p13 → p31 , p14 → p32 , p22 → p32 .
Additionally, all subprocesses of one and the same process must be
executed in their respective order (∀i, j, k : ifj < k ⇝ pij → pik ). That
means p11 → p12 , p12 → p13 , etc.
9 / 25
Precedence Graph for the Example Process Set
p14
p11 p13
5
3 2
p12
p31
2
5 p32
p21
p22 2
5
7
10 / 25
Schedule Generation from Precedence Graph
(n Processors, no preemption)
1 t =0
2 Compute set of ready processes B (contains all processes which
do not have to wait for another process)
3 Be m the number of unoccupied processors. Select m processes
from B for execution according to some priority criterion (such as
SJN).
4 Determine tinc = min(te ) of all selected processes.
5 Plan the selected processes on the (unoccupied) processors in
interval [t, t + tinc ]
6 t := t + tinc
7 If there are processes which have not been planned ⇝ Goto 2
8 Stop
11 / 25
Scheduling of the Example Process Set
n = 2, SJN, No preemption
t B execute
0 p11 , p21 p11 , p21
3 (p21 ) (p21 )
5 p12 p12
7 p13 , p22 p13 , p22
9 (p22 ), p14 , p31 p14
14 p31 p31
19 p32 p31
21 — —
12 / 25
Resulting Schedule for Example
P1 p 11 p 12 p 13 p 14 p 31 p 32
P2 p 21 p 22
0 2 4 6 8 10 12 14 16 18 20 22
13 / 25
Time-triggered vs. Event-triggered Scheduling
Time-triggered Scheduling
No Interrupts (→ no unforeseen code execution of ISRs)
all external components must be actively polled (periodically)
Predictable Timing
Typical for autonomous and Real-Time Systems
Only possible with offline scheduling
Example: Time Division Multiple Access (TDMA)
14 / 25
Time Division Multiple Access (TDMA)
Idea:
CPU Time is divided into “Slices” (or slots) of a uniform length
There is a slot for every process (regardless, whether he needs it
or not)
within its slot, every process can do whatever he wants (if he is
blocked, its slice is wasted)
constant beat is necessary (like a metronome); at every “beat” a
new process is activated (usually by timer interrupt)
no other interrupts allowed; all devices must be polled
P1 P2 P3 P4 P1 P2 P3 P4 P1
...
0 2 4 6 8 t
Slot
Process does not
need its slot.
Period
15 / 25
Time-triggered vs. Event-triggered Scheduling
Event-triggered Scheduling
System reacts to events from the outside (Interrupts)
Certain activities (Processes, threads) are getting ready as
reaction to received interrupt
No guarantee about execution time possible, because time and
rate of interrupts is not known a priori
typical for interactive systems
Examples: Graphical User Interfaces (Events: Mouse Movements,
Click, Key Pressed, but also “Packet received by Network Card”)
16 / 25
When is Scheduling performed?
17 / 25
Priorities
18 / 25
Example 1: Round Robin (RR, Time Slicing)
Idea: Every process gets the CPU for a fixed period of time (Quantum
tq ) in turn.
Main Goal: Fairness
How should we choose tq ?
tq ↓ (small)
▶ Reaction Time (or Latency) of a process ↓
▶ Ratio of useful process fork to time needed for context switch ↓ (that
means: overhead ↑)
tq ↑ (large)
▶ Reaction Time (or Latency) of a process ↑
▶ relative overhead ↓
Reaction time of a process depends on
1 Number of Processes n
2 Length of Quantum tq
3 Duration of Context Switch tcs
RR is oftenly combined with some priority scheme.
19 / 25
Round-Robin Illustrated
t cs
... P1 P2 P3 P1 ...
tq
t rsp
Figure: Parameters for Round Robin
here n = 3
trsp is the worst case response time for a process
trsp = n(tq + tcs )
if events concerning a process are distributed evenly, then the
t
average response time is t rsp = rsp
2
20 / 25
Example 2: The Windows Scheduler
preemptive
32 priority levels: the highest priority thread(s) run(s)
Threads of equal priority are scheduled via Round Robin
Length of Quantum:
▶ Server Variant: 120 ms
▶ Client (Workstation) Variant: 20 ms. . . 60 ms
Quantum is doubled for every foreground thread
Lower Priority Levels (0. . . 15) are dynamic
temporary increase of priority (Priority Boost), if
▶ process completes an I/O operation,
▶ window thread comes into foreground,
▶ process starving
21 / 25
Example 2: The Windows Scheduler
31 Priority
30 tq
"Real−Time"
...
Levels
16
15
14
Variable Levels
...
Base
Priority
1 active waiting active ready active
0 System Level
t
Figure: Priority Thread is preempted
Levels in
Windows Figure: Priority Boost
22 / 25
Example 3: The Classical UNIX Scheduler
Interactive vs. computing processes
23 / 25
What have we learned so far?
1 Why do we schedule?
2 Online vs. Offline Scheduling,
3 SJN as Example for Offline Scheduling
4 What is a Precedence Graph?
5 Time-Triggered vs. Event-Triggered Scheduling
6 Scheduling Time Instants (cooperative vs. preemptive
Multitasking)
7 Priorities: static vs. dynamic, external vs. implicit
8 Example 1: Round Robin
9 Example 2: Windows Scheduling
10 Example 3: Classical UNIX Scheduling
24 / 25
If you want to know more . . .
25 / 25