Download presentation
Presentation is loading. Please wait.
Published byWilliam Anderson Modified over 9 years ago
1
Process Scheduling in Linux (Chap. 11, Understanding the Linux Kernel) J. H. Wang Sep. 26, 2008
2
Outline Scheduling Policy Scheduling Algorithm System Calls related to Scheduling
3
Scheduling Policy Based on time-sharing –Time slice Based on priority ranking –Dynamic Classification of processes –Interactive processes –Batch processes –Real-time processes
4
System Calls related to Scheduling System calldescription nice() getpriority() setpriority() sched_getscheduler() sched_setscheduler() sched_getparam() sched_setparam() sched_yield() sched_get_priority_min() sched_get_priority_max() sched_rr_get_interval() change the priority get the maximum group priority set the group priority get the scheduling policy set the scheduling policy and priority get the priority set the priority relinquish the processor voluntarily get the minimum priority value get the maximum priority value get the time quantum for Round-Robin
5
Linux (user) processes are preemptive –When a new process has higher priority than the current –When its time quantum expires, need_resched will be set A preempted process is not suspended since it remains in the TASK_RUNNING state Linux kernel is nonpreemptive –Simpler
6
How Long Must a Quantum Last? Neither too long nor too short –Too short: overhead for process switch –Too long: processes no longer appear to be executed concurrently Always a compromise –The rule of thumb: to choose a duration as long as possible, while keeping good system response time
7
The Scheduling Algorithm Dividing CPU time into epochs –In a single epoch, every process has a specified time quantum whose duration is computed when the epoch begins Each process has a base time quantum, which is the time-quantum assigned by the scheduler if it has exhausted its quantum in the previous epoch –Can be changed by nice(), setpriority() INIT_TASK macro sets the value of initial time quantum of process 0 to DEF_COUNTER –#define DEF_COUNTER (10*HZ/100) (~105ms)
8
Two kinds of priorities –Static priority Real-time: 1-99, never changed –Dynamic priority Conventional processes Base time quantum+# of ticks left before its quantum expires
9
Data Structures used by the Scheduler Fields related to scheduling in process descriptor –need_resched: a flag checked by ret_from_sys_call() –policy: the scheduling class SCHED_FIFO: First-In First-Out real-time SCHED_RR: Round-Robin real-time SCHED_OTHER: conventional time-shared processes
10
–rt_priority: static priority for real-time process (1-99) –counter: # of ticks left before its quantum expires –nice: determines length of time quantum (-20-19) –cpus_allowed: a bit mask specifying the CPUs on which the process is allowed to run –cpus_runnable: a bit mask specifying the CPU that is executing the process –processor: the index of CPU that is executing the process
11
When a new process is created, do_fork() sets the counter field of both current (the parent) and p (the child): –p->counter = (current->counter+1) >> 1; current->counter >>= 1; if (!current->counter) current->need_resched= 1;
12
CPU’s Data Structures aligned_data array of NR_CPUS structures of type schedule_data –curr: a pointer to the process descriptor of the process running on that CPU cpu_curr(n) –last_schedule: the value of the 64-bit Time Stamp Counter when the last process switch was performed on the CPU last_schedule(n)
13
The schedule() Function Direct invocation –When the process must be blocked to wait for resource –When long iterative tasks are executed in device drivers Lazy invocation: by setting the need_resched –When current has used up its quantum, by update_process_times() –When a process is woken up, by reschedule_idle() –When a sched_setscheduler() or sched_yield() system call is issued
14
Actions performed by schedule() before and after a process switch –Details in pp. 357-362 How good is a runnable process? –weight returned by goodness() measures the “goodness” of a process weight = -1: will be selected only if no other runnable processes weight = 0: conventional process that has exhausted its quantum weight= 2-77: a conventional process that has not exhausted its quantum Weight>=1000: a real-time process
15
Scheduling on multiprocessor systems –reschedule_idle(): checks whether the just replaced process should be executed on some other CPU running a lower priority process Is the CPU that was last running p currently idle? Does an idle processor exist that can execute p? Does there exist a processor that can execute p and whose current process has lower dynamic priority than p? –Details on pp.364-365
16
Performance of Scheduling Algorithm (kernel 2.4) The algorithm does not scale well The predefined quantum is too large for high system loads I/O-bound process boosting strategy is not optimal Support for real-time applications is weak (These should have been fixed in new kernel versions…)
17
System Calls Related to Scheduling nice() –Allows processed to change their base priority –Replaced by setpriority() getpriority() and setpriority() –Act on the base priorities of all processes in a given group –which: PRIO_PROCESS, PRIO_PGRP, PRIO_USER –who: the value of pid, pgrp, or uid field to be used for selecting the processes –niceval: the new base priority value
18
System Calls Related to Real-Time Processes sched_getscheduler(), sched_setscheduler() –Queries/sets the scheduling policy sched_getparam(), sched_setparam() –Retrieves/sets the scheduling parameters sched_yield() –To relinquish the CPI volutarily without being suspended sched_get_priority_min(), sched_get_priority_max() –To return the minimum/maximum real-time static priority sched_rr_get_interval() –To write the round-robin time quantum for real-time process
19
Thanks for Your Attention!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.