Professional Documents
Culture Documents
Brindha M Harini K S
❑ The Linux operating system is preemptive.
❑ An example of threads can be seen in almost every video games, as there are multiple
threads handling various modules of the game – one for user input, one for graphics,
one for sound processing and another for AI.
Process Scheduling Types
• Real time Processes
– Deadlines that have to be met
– Should never be blocked by a low prioritytask
• Normal Processes
– Interactive
• Constantly interact with their users, therefore spend a lot of time waiting for key presses and
mouse operations.
• When input is received, the process must wake up quickly (delay must be
between 50 to 150 ms)
– Batch
• Do not require any user interaction, often run in the background.
3
History (Schedulers For Normal Processes)
Timeslice:
❑ Time allocated for each thread/task to run on CPU in a certain time interval/timeout
❑ CPU cycles divided in proportion to thread’s weight
Runtime:
❑ Accumulative thread time on CPU.
❑ Once runtime > timeslice, thread is preempted.
Runqueue:
❑ Queue of threads waiting to be executed by CPU
❑ Queue sorted by runtime
CPU T T T T
A Running Queue :
❑ A running queue (rq) is created for
each processor (CPU).
❑ It is defined in kernel/sched.c as
struct _runqueue.
❑ Goes through the entire “ global runqueue” to determine the next task to be run.
❑ The time taken w as proportional to the number of active processes in the system
❑ A Global runqueue - All CPUs had to wait for other CPUs to finish execution.
❑ This meant a task could be scheduled on any processor -- w hich can be good for load
balancing but bad for memory caches.
Global Runqueue
…
…
…
…
…
Problems
• Context Switching requires access to runqueue
• Only one core can access/manipulate runqueue at any one time
• Other cores must wait to get new threads
O(1) Scheduler – linux 2.6
Core 0 Thread load Core 1 Thread load Core 0 Thread load Core 1 Thread load
Runqueue Runqueue Runqueue Runqueue
Thread A 20 Thread B 15 Thread A 20 Thread B 15
(W=80, 25%) (W=25, 60%) (W=80, 25%) (W=25, 60%)
- - Thread C 10 Thread E 10 Thread C 10
(W=25, 40%) (W=20, 50%) (W=25, 40%)
- - Thread D 5 - - Thread D 5
(W=10, 50%) (W=10, 50%)
- - Thread E 10 - - - -
(W=20, 50%)
11
O(1) scheduler
Priority Arrays
❑ are the data structures that provide O(1) scheduling by mapping
each running task to a priority queue
The time taken to find a task to execute depends not on the number
of active tasks but instead on the number of priorities
priority
: :
priority
: :
102 102
101 101
high
100 100
13
The Scheduling Policy
• Pick the first task from the lowest numbered run queue
• When done put task in the appropriate queue in the expired run
queue
138 138
priority
: :
: :
execute
102 102
101 101
100 100
14
The Scheduling Policy
• Once active run queues are complete
– Make expired run queues active and vice versa
priority
: :
priority
: :
102 102
101 101
high
100 100
15
Dynamic Priority and Run Queues
• Dynamic priority used to determine which run queue to put the task
• No matter how ‘nice’ you are, you still need to wait on run queues ---
prevents starvation
execute
102 102
101 101
100 100
16
Completely Fair Scheduler (Single-Core)
CPU Core
Time elapsed (s)
Thread A 0 . 05
CPU Core
Time elapsed (s)
Thread B 0 . 15
CPUCore
Time elapsed (s)
Thread C 0 . 35
CPUCore
Time elapsed (s)
Thread D 0.60
CPUCore
Time elapsed (s)
Thread E 1.00
CPU Core
Time elapsed (s)
Thread F 0. 20
CPU Core
Time elapsed (s)
Thread A 0.24
❑ The scheduler supports real time scheduling quite well, it will do its best to
meet predetermined dead lines but does not guarantee it.
❑ RT tasks priority range is [0,99], this tasks will always preempt user
tasks as user tasks are ranged [100,139]
Scheduling schemes CPU T1 T2
1. SCHED_FIFO – As the name implies, first in, first out. Timeslices are irrelveant in this
scheme, the tasks with the highest priority runs until is finishes.
2. SCHED_RR – Round Robin, tasks are scheduled by priority, task s in the same priority
run in a round robin fashion for an pre-allotted timeslice.
3. SCHED_BATCH – “batch” style of execution of processes. →lowest priority tasks ( nice +19 )
THANKS..