setscheduler() in Linux Kernel
everyone, I found this inside static int setscheduler(pid_t pid, int policy, struct sched_param *param)
:
p->policy = policy;
if (policy != SCHED_OTHER) p->prio = MAX_USER_RT_PRIO-1 - p->rt_priority;
开发者_高级运维
p is a pointer to the task descriptor with current pid (parameter above) so if it's policy is not SCHED_OHTER (it means SCHED_FIFO or SCHED_RR) but why do we change p->prio such way? what exactly does it mean rt_priority? thanks in advance
Short answer: rt_priority
means real-time priority and for SCHED_RR
and SCHED_FIFO
it decides how much time the process will get.
Long answer
First of all Linux implements something called Realtime Process Scheduling
(SCHED_RR
and SCHED_FIFO
). Processes operating under these policies always have priority over other processes.
Linux provides 99 realtime priority levels, numbered 1 (lowest) to 99 (highest).
In the kernel, if I recall correctly "smaller numbers mean better preference" - smaller p->prio
means better priority.
Here us what sched_setscheduler
looks like:
int sched_setscheduler(pid_t pid, int policy, const struct sched_param *param);
And sched_param
struct sched_param {
int sched_priority; /* Scheduling priority */
};
The integer sched_priority
, for a policy of SCHED_RR
or SCHED_FIFO
means "realtime priority" which I assume is rt_priority
. So the kernel does the right thing
- Take the maximmum priority
- Subtract the real time priority from that value. The bigger the priority (the more it subtracts) the better treatment by the scheduler.
精彩评论