Understanding Linux Process Scheduling: Dive into Run Queues, Priority & Swapping, Lecture notes of Linux skills

An in-depth exploration of Linux process scheduling, focusing on the concepts of run queues, priority arrays, and swapping arrays. It covers topics such as processor affinity, basic scheduling algorithm, calculating timeslices, dynamic priority, and interactive processes. The document also discusses the importance of two arrays and the role of Linux in real-time scheduling.

Typology: Lecture notes

2021/2022

Uploaded on 09/27/2022

dreamingofyou
dreamingofyou 🇬🇧

4.3

(16)

233 documents

1 / 40

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Linux Scheduler
Linux Scheduler
Descending to
Reality. . .
Philosophies
Processor
Scheduling
Processor Affinity
Basic Scheduling
Algorithm
The Run Queue
The Highest Priority
Process
Calculating
Timeslices
Typical Quanta
Dynamic Priority
Interactive Processes
Using Quanta
Avoiding Indefinite
Overtaking
The Priority Arrays
Swapping Arrays
Why Two Arrays?
The Traditional
Algorithm
Linux is More
Efficient
Locking Runqueues
Real-Time
Scheduling
Sleeping and Waking
Timers
1 / 40
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download Understanding Linux Process Scheduling: Dive into Run Queues, Priority & Swapping and more Lecture notes Linux skills in PDF only on Docsity!

Linux Scheduler

Descending to Reality...

n

The Linux scheduler tries to be very efficient

n

To do that, it uses some complex datastructures

n

Some of what it does actually contradicts theschemes we’ve been discussing...

Processor Scheduling

n

Have a separate run queue for each processor

n

Each processor only selects processes from itsown queue to run

n

Yes, it’s possible for one processor to be idlewhile others have jobs waiting in their runqueues

n

Periodically, the queues are rebalanced: if oneprocessor’s run queue is too long, someprocesses are moved from it to anotherprocessor’s queue

Processor Affinity

n

Each process has a bitmask saying what CPUsit can run on

n

Normally, of course, all CPUs are listed

n

Processes can change the mask

n

The mask is inherited by child processes (andthreads), thus tending to keep them on thesame CPU

n

Rebalancing does not override affinity

The Run Queue

n

140 separate queues, one for each priority level

n

Actually, that number can be changed at agiven site

n

Actually, two sets, active and expired

n

Priorities 0-99 for real-time processes

n

Priorities 100-139 for normal processes; valueset via nice() system call

The Highest Priority Process

n

There is a bit map indicating which queueshave processes that are ready to run

n

Find the first bit that’s set: u 140 queues ⇒ 5 integers u Only a few compares to find the first thatis non-zero u Hardware instruction to find the first 1-bit u Time depends on the number of prioritylevels, not the number of processes

Typical Quanta

Static Pri Niceness Quantum Highest Static Pri 100 20 800 ms High Static Pri 110

600 ms Normal 120 0 100 ms Low Static Pri 130 + 50 ms Lowest Static Pri 139 + 5 ms

Dynamic Priority

n

Dynamic priority is calculated from staticpriority and average sleep time

n

When process wakes up, record how long itwas sleeping, up to some maximum value

n

When the process is running, decrease thatvalue each timer tick

n

Roughly speaking, the bonus is a number in [ , 10] that measures what percentage of the time the process was sleeping recently; 5 isneutral, 10 helps priority by 5, 0 hurts priorityby 5 DP = max ( , min ( SP − bonus

  • 5 , 139))

Using Quanta

n

At every time tick, decrease the quantum ofthe current running process

n

If the time goes to zero, the process is done

n

If the process is non-interactive, put it aside onthe expired list

n

If the process is interactive, put it at the endof the current priority queue

n

If there’s nothing else at that priority, it willrun again immediately

n

Of course, by running so much is bonus will godown, and so will its priority and its interativestatus

Avoiding Indefinite Overtaking

n

There are two sets of 140 queues, active and expired

n

The system only runs processes from active queues, and puts them on expired queues when they use up their quanta

n

When a priority level of the active queue is empty, the scheduler looks for the next-highestpriority queue

n

After running all of the active queues, the active and expired queues are swapped

n

There are pointers to the current arrays; at theend of a cycle, the pointers are switched

Swapping Arrays

struct prioarray *array = rq->active;if (array->nr_active == 0) { rq->active = rq->expired;rq->expired = array; }

Why Two Arrays?

n

Why is it done this way?

n

It avoids the need for traditional aging

n

Why is aging bad?

n

It’s O( n ) at each clock tick

Linux is More Efficient

n

Processes are touched only when they start orstop running

n

That’s when we recalculate priorities, bonuses,quanta, and interactive status

n

There are no loops over all processes or evenover all runnableprocesses

Locking Runqueues

n

To rebalance, the kernel sometimes needs tomove processes from one runqueue to another

n

This is actually done by special kernel threads

n

Naturally, the runqueue must be locked beforethis happens

n

The kernel always locks runqueues in order ofincreasing address

n

Why? Deadlock prevention! (It is good forsomething...