From the user's point of view, the priority is very simple, but from-20 ~ A number between 19. However, the processing of these priorities inside the kernel is quite complicated.
Priority kernel Representation
In the user space, you can call nice to change the priority of the current process. For Linux systems, the priority ranges from-20 ~ 19. The smaller the value, the higher the priority of the process. As to why we choose such a strange range, the truth has been drowned in history.
Use a much more normal representation in the kernel, from 0 to 139. The smaller the value, the lower the priority. 0 ~ 99 is used by real-time processes. Nice value-20 ~ 19 can be mapped to the range 100 to 139, which means nice can only set the priority of non-real-time processes. The priority of real-time processes must be higher than that of normal processes.
Computing priority
All of the above are static priorities of processes. Sometimes we need to consider the following three priorities:
The Dynamic Priority of a process is task_struct-> Prio, general priority is task_struct-> normal_prio, and static priority is task_struct-> static_prio.
Dynamic Priority and normal priority are calculated through static priority:
P-> PRIO = inclutive_prio (P );
The Helper function inclutive_prio performs the following operations:
/* * Calculate the current priority, i.e. the priority * taken into account by the scheduler. This value might * be boosted by RT tasks, or might be boosted by * interactivity modifiers. Will be RT if the task got * RT-boosted. If not then it returns p->normal_prio. */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;}
This function returns a dynamic priority and modifies the normal priority of a process within the function.
For common processes, we can see that static_prio, normal_prio and PRIO are identical. The Priority Calculation Methods of real-time processes are different. The calculation depends on rt_priority. Because higher priority indicates higher priority, the internal priority representation of the kernel is the opposite, A lower value indicates a higher priority.
Normal_prio is very simple. Only P-> static_prio is returned. The reason why a function is used to encapsulate this simple function is historical. When the original O (1) scheduler, the calculation of common priorities involves a lot of complicated technical work.
Load Weight Calculation
Priority is important, but the load weight stored in task-struct-> Se. load is considered during process scheduling.
The set_load_weight function is used to calculate the load weight based on the process type and its static priority.
For common processes, the kernel maintains a table from-20 ~ 19 each priority corresponds to one item in the table
static const int prio_to_weight[40] = { /* -20 */ 88761, 71755, 56483, 46273, 36291, /* -15 */ 29154, 23254, 18705, 14949, 11916, /* -10 */ 9548, 7620, 6100, 4904, 3906, /* -5 */ 3121, 2501, 1991, 1586, 1277, /* 0 */ 1024, 820, 655, 526, 423, /* 5 */ 335, 272, 215, 172, 137, /* 10 */ 110, 87, 70, 56, 45, /* 15 */ 36, 29, 23, 18, 15,};
The multiplier between adjacent values in the array is 1.25, with a total of 40 items. prio_to_weight [0] corresponds to priority 100, and prio_to_wright [39] corresponds to priority 139. Instead, the process load is twice that of prio_to_weight [0. In fact, we do not need to remember these strange numbers, as long as we know that the real-time process has a higher weight.