Saturday, November 22, 2014

Linux Process Priority

Each process has a static priority in the task_struct which is its nice value (between -20 to 19 which lower number signifies higher priority).  It is static because it does not change throughout the lifetime of the process.  Schedule pick the next task to run based on the dynamic priority.

The dynamic priority is a function of the static priority and also the interactivity of processes.  Dynamic priority uses effective_prio() which add a bonus or penalty value (in range of -5 to +5) based on the interactivity of the task.  This bonus value is added to the static priority to form the dynamic priority.

Kernel tracks how active (interactive) a process via the sleep_avg field in the task_struct.  When a task is created, it receives a high sleep_avg value.  The time a task spent sleeping will be add to sleep_avg (up to MAX_SLEEP_AVG = 10 msec).  Each time a task runs, the corresponding time will be subtract from sleep_avg.  A task with high sleep_avg is I/O bound and a zero sleep_avg value means the task is processor-bound.

The size of timeslice is proportional to the task's priority.  Higher priority task receive longer timeslice.  When a task spawn a child, the child share the timeslice with the parent.  This is to prevent task keep spawning child to get unlimited supply of processor time.

No comments: