0% found this document useful (0 votes)
8 views

Os 05 Scheduling

sceg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Os 05 Scheduling

sceg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Operating Systems

Lecture 5: CPU Scheduling

Prof. Dr. Robert Baumgartl


Dresden University of Applied Sciences

1 / 25
(Processor) Scheduling
Def. A Scheduler is an OS component wich decides

Which Activity is to be executed where (on which processsor and core)


when and for how long?

The Scheduler optimizes one of the following system parameters


Average reaction time of all processes
Average turnaround time of all processes (“How long does a process
exist within the system”)
Maximum CPU utilization
Maximum number of simultaneous data streams
Guarantee of a worst case reaction time
Fairness: if there are n processes → every process should get 1/n of
CPU time
Make sure, that every process gets at least some CPU time (avoidance
of starvation)
2 / 25
Some more on Scheduling

Finding an optimal schedule is usually NP-hard


there could be other schedulers, too! (e. g., the I/O scheduler
which plans mass storage accesses)
There are numerous research papers, algorithms, projects and
even a branch of mathematics: Scheduling Theory
Scheduling can be done online (during system executing) and
offline (before system starts)

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

Resulting Schedule for SJN:

P4 P1 P3 P2

0 3 9 16 24 t

0 + 3 + 9 + 16 3 + 9 + 16 + 24
tw = =7 tv = = 13
4 4

(Can you find a schedule with lower t w ?)

7 / 25
Process Precedence

What if some process Pi must be run before some other process


Pj (e. g. a sensor data collector must run before the processing
process)?
Notation: Pi → Pj , “Pi has precedence over Pj ”
If there are many precedences within the process set, we use a
precedence graph.

8 / 25
The Precedence Graph - Example

The Process Set consists of three processes, which consist of


subprocesses as follows (te are given in parentheses):

P1 : { p11 (3), p12 (2), p13 (2), p14 (5) }


P2 : { p21 (5), p22 (7) }
P3 : { p31 (5), p32 (2) }
Further, there are explicit precedences between the subprocesses:

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.

TODO: Create the precedence graph!

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 — —

Table: Relevant Points in Time for Example Schedule

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

Result: Gantt Diagram (named after business consultant (!) Henry


L. Gantt)
Turnaround Time of the Process Set: 21 (abstract time units)
Prozessors are not fully utilized (Reason: Precedences) ⇝
Amdahl’s Law P
te 31
Utilization u = = ≈ 0.74.
n tt 2 · 21

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?

Four different strategies, when to do re-scheduling:


1 anytime by the OS (preemptive Multitasking), e. g. ,
▶ when a process blocks or ends,
▶ when a process gets ready (e. g. in reaction to an interrupt or
someone has returned a resource),
▶ when the OS descides, it is time (because a process consumed its
time quantum).
2 decided by the OS, but only at certain so-called Preemption
Points
3 the process has to give up CPU voluntarily (by issuing a system
call) (cooperative Multitasking),
4 Never within a process’ lifetime – only after process completion
(run-to-completion)

17 / 25
Priorities

Parameter of every process which expresses its “importance”


at every scheduling instance, the process(es) with highest priority
is/are chosen for execution
Priorities from within the system or from the outside:
▶ external – defined by the user or
▶ implicit – derived from some other parameter (e. g. , SJN, remaining
execution time, its deadline . . . )
Priorities could be constant or variable
▶ static priorities – easy to analyze, inflexible, efficient
▶ dynamic priorities – difficult to analyze, flexible, overhead!

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

preemptive, Time Slicing


tries to distinguish between two process classes
1 interactive processes (“I/O-bound”)
2 (primarily) computing processes (“compute-bound”)
Compute-bound processes use up their full quantum (they don’t
do system calls)
Interactive processes frequently block before the end of their
quantum (because the do I/O, which blocks)
if the OS detects that the process gehts a (slight) increase of its
priority, such that it is executed more frequently
→ UNIX favors interactive processes (a bit)
▶ interactive processes have a better I/O performance (reaction time,
throughput)
▶ compute-bound processes have a slight disadvantage (but that
usually does not matter)

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 . . .

Jean-Pierre Lozi et al. “The Linux Scheduler: a Decade of Wasted


Cores”. In: Proceedings of the Eleventh European Conference on
Computer Systems (EUROSYS’16). London, Apr. 2016
Nikita Ishkov. “A complete guide to Linux process scheduling”.
MA thesis. University of Tampere, School of Information Sciences,
Feb. 2015

25 / 25

You might also like