setscheduler()在Linux内核

问题描述:

大家,我发现这里面static int setscheduler(pid_t pid, int policy, struct sched_param *param)setscheduler()在Linux内核

p->policy = policy; 

if (policy != SCHED_OTHER) p->prio = MAX_USER_RT_PRIO-1 - p->rt_priority; 

p是指向与当前PID(以上参数)的任务描述符 因此,如果它的政策是不SCHED_OHTER(这意味着SCHED_FIFO或SCHED_RR),但为什么我们改变p-> prio这种方式呢?究竟是什么意思rt_priority?在此先感谢

简短的回答rt_priority意味着实时优先级和SCHED_RRSCHED_FIFO它决定过程将需要多少时间获得。

龙答案

所有Linux首先实现了一种叫Realtime Process SchedulingSCHED_RRSCHED_FIFO)。在这些政策下运营的流程始终优先于其他流程。 Linux提供99个实时优先级,编号为1(最低)到99(最高)。

在内核中,如果我正确地记得“数字越小意味着更好的偏好” - 越小p->prio意味着更好的优先级

这里我们什么sched_setscheduler样子:

int sched_setscheduler(pid_t pid, int policy, const struct sched_param *param); 

sched_param

struct sched_param { 
    int sched_priority;  /* Scheduling priority */ 
}; 

的整数sched_priority,为SCHED_RRSCHED_FIFO手段 “实时优先” 的策略,我认为是rt_priority。所以内核做正确的事

  • 采取优先机器最多
  • 从该值减去实时优先级。调度器的优先级越高(它减去的越多)。