You are on page 1of 30

Linux Process Scheduling

kernel v2.4 Ronen Talmon

Objectives
Fast process response time Good throughput Avoidance of process starvation Different process priorities according to various needs Etc

Dynamic Priority Adjustment


The scheduler keeps track of what processes are doing and adjusts their priorities periodically
Processes running for a long time are penalized Processes that have been denied the use of CPU are boosted

Process Classification
Traditional process classification:
I/O Bound CPU Bound

Alternative classification:
Interactive Batch Real time

In Linux a combination of both.

Process Classification in Linux


Real time programs are explicitly recognized Linux does not distinguish between interactive and batch programs Linux implicitly favors I/O bound processes over CPU bound (to be shown)

The Scheduling Algorithm


CPU time is divided into epochs* In a single epoch, every process has a specified time quantum The time quantum value is the max. CPU time portion assigned Quantum duration is restarted when the epoch begins * ,

The Scheduling Algorithm (2)


When a process has exhausted its quantum it is preempted A process can be selected several times in a single epoch, as long as its quantum has not been exhausted.
For example, if a process suspends itself to wait for I/O.

The Scheduling Algorithm (3)


The epoch ends when all runnable processes have exhausted their quantum The scheduler restarts the quantum of all processes, and a new epoch begins. The only thing left how to select a process to run?!

How good a runnable process?


The heart of the scheduling algorithm is to identify the best candidate among all processes Based on collected data on each process:
counter The number of CPU ticks left before its quantum expires nice The process quantum when a new epoch begins

How good a runnable process?


Conventional process:

priority = counter + 20 nice *


Real time process:

priority = counter + 1000


*As promised implicitly favors I/O process

10

Example
Process 1 CPU Bound:
CPU CPU CPU CPU
0 1 2 3 4

Process 2 I/O Bound:


CPU
0 1

I/O
2 3

CPU
4 5

Base time quantum =

( 20 nice ) / 4 + 1

11

Example

nice base (-20-19) quantum Process 1 Process 2 4 0

priority

mode

12

Example

nice base (-20-19) quantum Process 1 Process 2 4 0 5 6

priority

mode

13

Example
nice Process 1 Process 2 4 0 counter 5 6 priority 21 26 mode ready ready

priority = counter + 20 - nice

14

Example
nice Process 1 Process 2 4 0 counter 5 6 priority 21 26 mode ready ready

schedule process 2

15

Example
nice Process 1 Process 2 4 0 counter 5 5 priority 21 25 mode ready waiting

schedule process 1

2 1

16

Example
nice Process 1 Process 2 4 0 counter 4 5 priority 20 25 mode ready waiting

schedule process 1

2 1 1

17

Example
nice Process 1 Process 2 4 0 counter 3 5 priority 19 25 mode ready ready

schedule process 2

2 1 1 2

18

Example
nice Process 1 Process 2 4 0 counter 3 4 priority 19 24 mode ready ready

schedule process 2

2 1 1 2 2

19

Example
nice Process 1 Process 2 4 0 counter 3 3 priority 19 23 mode ready waiting

schedule process 1

2 1 1 22 1

20

Example
nice Process 1 Process 2 4 0 counter 2 3 priority 18 23 mode ready waiting

schedule process 1

2 1 1 22 1 1

21

Example
nice Process 1 Process 2 4 0 counter 1 3 priority 17 23 mode ready ready

schedule process 2

2 1 1 22 1 1 2

22

Example
nice Process 1 Process 2 4 0 counter 1 2 priority 17 22 mode ready ready

schedule process 2

2 1 1 22 1 1 2 2

23

Example
nice Process 1 Process 2 4 0 counter 1 1 priority 17 21 mode ready waiting

schedule process 1

2 1 1 22 1 1 2 2 1

24

Example
nice Process 1 Process 2 4 0 counter 0 1 priority 0* 21 mode ready waiting

*counter 0 => priority 0

2 1 1 22 1 1 2 2 1

25

Example
nice Process 1 Process 2 4 0 counter 0 1 priority 0 21 mode ready waiting

All runnable processes have exhausted their quantum The epoch ends
2 1 1 22 1 1 2 2 1

26

Example
New epoch begins restarts the quantum of all processes:

counter / 2 + ( 20 nice ) / 4 + 1*
Note:
Each time I/O bound process was ready, it was scheduled Favors I/O bound process longer quantum in next epoch * Base time quantum is assigned if the process has exhausted its quantum in the previous epoch

27

Performance Pitfalls
Does not scale well: (i.e. large no. of processes)
Inefficient to recompute all dynamic priorities at once
(but it is not frequent once in each epoch)

I/O bound processes are seldom boosted


(interactive applications have long response time)

Real time applications support is weak

28

Performance Pitfalls
I/O bound process boosting strategy is not optimal
Interactive processes are not different from (non interactive) I/O bound processes.

System responsiveness depends heavily on system load


The predefined time quantum is too large for high system load

29

Related System Calls


nice()
allows processes to change their base priority.

getpriority() / setpriority()
Same as nice() Act on a base priorities of all processes in a given group.

30

You might also like