Chapter 2: Process Management and Scheduling
0 99 100 139
Higher Priority
Realtime Normal
− 20 Nice + 19
Figure 2-14: Kernel priority scale.
The following macros are used to convert between the different forms of representation (MAX_RT_PRIO
specifies the maximum priority of real-time processes, andMAX_PRIOis the maximal priority value for
regular processes):
<sched.h>
#define MAX_USER_RT_PRIO 100
#define MAX_RT_PRIO MAX_USER_RT_PRIO
#define MAX_PRIO (MAX_RT_PRIO + 40)
#define DEFAULT_PRIO (MAX_RT_PRIO + 20)
kernel/sched.c
#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
Computing Priorities
Recall that it is not sufficient to consider just the static priority of a process, but that three priorities
must be taken into account: dynamic priority (task_struct->prio), normal priority
(task_struct->normal_prio), and static priority (task_struct->static_prio). These priorities
are related to each other in interesting ways, and in the following I discuss how.
static_priois the starting point of the calculations. Assume that it has been already set and that the
kernel now wants to compute the other priorities. This is done by a one-liner:
p->prio = effective_prio(p);
The auxiliary functioneffective_prioperforms the following operations:
kernel/sched.c
static int effective_prio(struct task_struct *p)
{
p->normal_prio = normal_prio(p);
/*
* If we are RT tasks or we were boosted to RT priority,
* keep the priority unchanged. Otherwise, update priority
* to the normal priority:
*/
if (!rt_prio(p->prio))
return p->normal_prio;
return p->prio;
}
First of all, the normal priority is computed and stored innormal_priority. This side effect allows for
setting bothprioandnormal_priowith a single function invocation. Another helper function,rt_prio,