unix_3_process_scheduling
unix_3_process_scheduling
Tamás Mészáros
http://www.mit.bme.hu/~meszaros/
UNIX scheduling 1 / 23
BME Operating Systems 2015.
Previously....
• Definition of a process (task)
– Running program (solving a particular problem)
UNIX scheduling 2 / 23
BME Operating Systems 2015.
Scheduling in general
• Main task of scheduling
selecting the next task to be run from the set of runnable processes
• Basic notation
– preemptive and cooperative scheduling
– priority
– static and dynamic scheduling
– measuring quality (CPU utilization, throughput, avg. wait time, etc.)
• Basic operation
– maintain a set of runnable tasks (FIFO, red-black binary tree, ...)
– choose the next task to be run
• Requirements
– small overhead, small complexity (O(1), O(N), O(log N))
– optimality (according to the selected measures)
– deterministic, fair, avoids starvation and system breakdown
– there might be special application needs (real-time, batch, multimedia, ...)
UNIX scheduling 3 / 23
BME Operating Systems 2015.
• Practice
UNIX scheduling 4 / 23
BME Operating Systems 2015.
• Priority-based
– every process has a dynamically calculated priority
– the process with the highest priority runs
• Time-sharing
– multiple processes at the same priority level can run in parallel
– each of them is given a time slice (e.g. 10 msec)
– after the time is expired the next process with the same prio will run
UNIX scheduling 6 / 23
BME Operating Systems 2015.
UNIX scheduling 7 / 23
BME Operating Systems 2015.
UNIX scheduling 8 / 23
BME Operating Systems 2015.
UNIX scheduling 9 / 23
BME Operating Systems 2015.
UNIX scheduling 10 / 23
BME Operating Systems 2015.
• Every 10 cycles
– Round-Robin scheduling among the processes at the same priority level
UNIX scheduling 11 / 23
BME Operating Systems 2015.
(see scheduling_examples.xls)
UNIX scheduling 12 / 23
BME Operating Systems 2015.
• Cons
– does not scale well
– no guarantee for processes
– users can not configure scheduling (except for nice)
– no support for multi-processor, multi-core systems
– kernel mode is non-preemptive:
A process running in kernel mode for a long time can hold up the entire
system (priority inversion)
UNIX scheduling 13 / 23
BME Operating Systems 2015.
• Kernel preemption
– it is necessary for multiprocessor scheduling
• Performance, overhead
– scheduling became more and more complex (requirements, hardware)
– scheduling algorithms should scale well
• Threads or processes?
– modern applications use threads a lot (e.g. Java)
– schedulers should focus on threads not on processes
UNIX scheduling 14 / 23
BME Operating Systems 2015.
UNIX scheduling 15 / 23
BME Operating Systems 2015.
UNIX scheduling 16 / 23
BME Operating Systems 2015.
P3
pri: 60 R2
P2
pri: 80
P1 R1
pri: 100
UNIX scheduling 17 / 23
BME Operating Systems 2015.
Linux schedulers
• Before kernel V2: based on the classical UNIX scheduler
• Before V 2.4
– scheduling classes: real-time, non-preemptive, normal
– scheduling algorithm with O(N) complexity
– single runnable queue (no SMP support)
– non-preemptive kernel
• Kernel v2.6 (Ingo Molnár)
– O(1) scheduler (scales very well)
– multiple runnable queues (better SMP support)
– a heuristic algorithm to differentiate between I/O and CPU-bound tasks
• comparing running and waiting (sleeping) times (takes considerable time)
• prefers I/O-bound processes
• 2.6.23 kernel: CFS (Completely Fair Scheduler)
– designed and implemented by Ingo Molnár, some ideas from Con Kolivas
– a new data structure for runnable processes: self-balancing red-black tree
– tries to be fair by calculating a „virtual” runtime for all processes
UNIX scheduling 18 / 23
BME Operating Systems 2015.
UNIX scheduling 19 / 23
BME Operating Systems 2015.
Linux CFS
• It replaces the previous O(1) scheduler with an O(log n) algorithm
• Basic operations
– enqueue_task: New task arrived (nr_running++)
– dequeue_task: Task no longer ready to run (nr_running--)
– pick_next_task: who is the next to run
UNIX scheduling 20 / 23
BME Operating Systems 2015.
• Usage
– AT: execute a task at a given time (at now + 1 day)
– CRON: periodically execute a task (see man crontab)
minute, hour, day of month, month, day of week
0 6 * 1-6,9-12 2 /local/bin/lets_play_soccer
Send an invitation every Tuesday morning at 6am (except during summer)
*/20 * * * * /local/bin/clear_old_temp_cache
Clear temporary and cache files in every 20 minutes
UNIX scheduling 21 / 23
BME Operating Systems 2015.
Summary
• Classical UNIX scheduling
– user mode: priority-based, time-sharing, preemptive
• the process with the highest priority runs first
• round-robin time-sharing scheduling between processes at the same prio.
level
• priority is calculated based on previous CPU usage and the nice value
– kernel mode: fixed priority, non-preemptive
• sleep priority assigned to resources will be given to awaking processes
– simple, avoids starvation, handles I/O jobs very well
– no SMP support, does not scale well, no support for spec. app. needs
UNIX scheduling 22 / 23
BME Operating Systems 2015.
http://www.cs.unc.edu/~jmc/linsched/
http://www.ibm.com/developerworks/library/l-linux-scheduler-simulator/
UNIX scheduling 23 / 23