我正在 SUSE Linux Real Time 上编写 C++ 程序,并试图收集有关其循环 (RR) 调度策略的计时统计信息。我可以通过
将策略设置为 RR#include <sched.h>
...
struct sched_param sparms;
sparms.sched_priority = 99;
if (sched_setscheduler(0, SCHED_RR, &sparms) < 0){
perror("sched_setsched");
exit(1);
}
并且能够通过 sched_rr_get_interval()
确定时间片。操作系统文档指出,可以通过进程的 nice 值更改时间量程。但是,这样做没有任何效果。我通过 nice()
更改了 nice 值,并确认它确实通过 getpriority()
更改了,但 sched_rr_get_interval()
同时返回.
有人知道发生了什么吗?
编辑:添加了下面的代码。 setpriority()
无效。
...
#include <stdio.h>
#include <unistd.h>
#include <sys/resource.h>
...
// Set scheduler to RR
struct sched_param sparms;
sparms.sched_priority = 99;
if(sched_setscheduler(0, SCHED_RR, &sparms) < 0){
fprintf(stderr, "Error setting thread scheduling policy & priority.");
exit(-1);
}
timespec qtm;
sched_rr_get_interval(0, &qtm);
fprintf(stderr, "Time quantum(thread 1): %lds %ldns.\n", qtm.tv_sec, qtm.tv_nsec);
fprintf(stderr, "Current nice value: %d\n", getpriority(PRIO_PROCESS, 0));
if(nice(90) < 0) fprintf(stderr, "Error setting time quantum.\n");
//setpriority(PRIO_PROCESS, 0, 10);
fprintf(stderr, "New nice value: %d\n", getpriority(PRIO_PROCESS, 0));
sched_rr_get_interval(0, &qtm);
fprintf(stderr, "New time quantum(thread 1): %lds %ldns.\n", qtm.tv_sec, qtm.tv_nsec);
...
编辑:操作系统版本:2.6.33.7-rt29-0.5-rt
最佳答案
似乎已经从 Linux 实时调度程序中删除了控制循环时间片的能力。
查看源码(kernel/sched_rt.c
),timeslice counter在过期时恢复到一个常量值(DEF_TIMESLICE
,定义为0.1s) task_tick_rt()
,所以没办法动态改变。 get_rr_interval()
(由 sched_rr_get_interval()
调用)始终返回该常量值
如果您需要更小的时间片,我想您必须自己使用 FIFO 调度程序并在适当的时间调用 sched_yield()
,或者破解内核以满足您的需求。
关于c++ - 无法在实时linux中更改循环时间量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6763386/