0% found this document useful (0 votes)
65 views21 pages

Linux Scheduling 2002

Linux uses three main scheduling policies - SCHED_FIFO for real-time processes with static priority and unlimited CPU time, SCHED_RR for real-time processes that introduces time-slicing with static priority, and SCHED_OTHER for all other dynamic priority processes using time-slicing in epochs. The schedule() function is called when need_resched is set, and selects the next process to run by looping through the runqueue and choosing the one with the highest calculated "goodness" value based on priority, counter, and whether it is the same memory context as the previous process.

Uploaded by

arasu@ymail.com
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views21 pages

Linux Scheduling 2002

Linux uses three main scheduling policies - SCHED_FIFO for real-time processes with static priority and unlimited CPU time, SCHED_RR for real-time processes that introduces time-slicing with static priority, and SCHED_OTHER for all other dynamic priority processes using time-slicing in epochs. The schedule() function is called when need_resched is set, and selects the next process to run by looping through the runqueue and choosing the one with the highest calculated "goodness" value based on priority, counter, and whether it is the same memory context as the previous process.

Uploaded by

arasu@ymail.com
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Linux Scheduling

Scheduling Policy and Algorithms, the schedule() Function


of the Linux Kernel version 2.4.20

Patrick Stahlberg
<Patrick.Stahlberg@hadiko.de>

$Id: speech.mgp,v 1.7 2002/12/17 10:59:17 patrick Exp $


Structure of this talk

What is scheduling, why do we need it?


concepts related to scheduling

How is scheduling done in Linux?


policy
algorithms
Part One

What is scheduling?
What is scheduling?

Distribution of the resource ’processor’ to the competing


tasks

In this talk: only uniprocessor-scheduling


Lifecycle of a process
New Processes

Ready Queue Active Process


Classification of processes

Interactive processes
Batch processes
Real-time processes

I/O-bound
CPU-bound

These classifications are independent


Process Preemption

Ability of an OS to take away CPU control of a process before it does


this voluntarily.

Processes are assigned processing time quanta, a process will be


preempted when its quantum duration is passed.

Scenario: a high-priority task enters the TASK_RUNNING state while a


low-priority task is active --> the low-priority task is preempted

Linux features preemptive processes but not (yet) a preemptive kernel


Measures of good Scheduling (1)

Fairness, equal treatment of processes

Prevent "Starvation" of processes

Use processing power efficiently

Minimize overhead caused by scheduling itself


Measures of good Scheduling (2)

For a Multiuser-Multitasking-OS:

Interactive processes should have quick response times

Desirable: intelligent treatment of I/O- and CPU-bound processes


Part Two

Linux scheduling policy and algorithms


When is the scheduler called?

Direct invocation
During System Calls
Mostly indirectly via sleep_on()
e.g. when waiting for a resource

Lazy invocation
After System Calls or interrupts
if need_resched is set
e.g. after sched_set_scheduler()
The timer interupt also sets need_resched, making sure that schedule()
is called frequently
Data structures used by the scheduler
need_resched
A flag set by interrupt handling routines
When set, ret_from_intr() calls schedule()

policy
Scheduling policy, see following slide

rt_priority
Static priority field for real-time processses

priority
Base time quantum (SCHED_RR)
Base priority (SCHED_OTHER)

counter
Scheduling classes

Linux provides three different scheduling algorithms called


‘scheduling classes’

Each process can be assigned one scheduling class

Scheduling classes are: SCHED_FIFO, SCHED_RR,


SCHED_OTHER
The SCHED_FIFO scheduling class

Real-time processes

Unlimited CPU time for processes given that there is no


higher-priority process

Static priority

p1
p2
p3

t1 t2
The SCHED_RR scheduling class

Real-time processes

Enhancement of SCHED_FIFO that introduces time slicing

Static priority

p1
p2
p3

t1 t2 t3 t4 t5
The SCHED_OTHER scheduling class

All other processes

Dynamic priority

Time slicing

Time slicing is using epochs


Epochs

Each non-realtime process is assigned a time quantum at


the beginning of an epoch.

The epoch ends when all processes in the runqueue have


used up their time quantum.
The schedule() function

Very much simplified:

If previous process is a SCHED_RR process which has


exhausted its time slice: assigns a new time slice to it and
puts it at the end of runqueue.

Main scheduling loop:


Loops through items of runqueue
Calculates a ‘goodness’ value for each one of them
Remembers the first task with highest goodness value

Does a context switch to the chosen task.


Goodness of a process

Calculated by the goodness() function

Goodness of real-time tasks is always higher than


goodness of a SCHED_OTHER task (1000 + rt_priority)

Goodness is calculated like this for SCHED_OTHER tasks:

if (p->mm == prev->mm)
return p->counter + 1 + 20 - p->nice;
else
return p->counter + 20 - p->nice;
Literature:
kernel/sched.c
http://en.tldp.org/LDP/tlk/kernel/processes.html#tth_sEc4.3
sched_setscheduler(2)
http://www.kernelnewbies.org/documents/schedule/
http://www.ora.com/catalog/linuxkernel/chapter/ch10.html
End. Questions?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy