我有以下来源喜欢SCHED_RR优先级90:
int main(int argc, char** argv)
{
const char *sched_policy[] = {
"SCHED_OTHER",
"SCHED_FIFO",
"SCHED_RR",
"SCHED_BATCH"
};
struct sched_param sp = {
.sched_priority = 90
};
pid_t pid = getpid();
printf("pid=(%d)\n",pid);
sched_setscheduler(pid, SCHED_RR, &sp);
printf("Scheduler Policy is %s.\n", sched_policy[sched_getscheduler(pid)]);
pthread_t tid ;
pthread_create(&tid , NULL, Thread1 , (void*)(long)3);
pthread_create(&tid , NULL, Thread2 , (void*)(long)3);
pthread_create(&tid , NULL, Thread3 , (void*)(long)3);
while(1)
sleep(100);
}
虽然shell“top”,我可以看到进程有-91的PR,看起来很有效,据我所知,在Linux中,thread1和thread2以及thread3是来自主线程的不同任务,它们只是共享相同的虚拟内存,我想知道在这个测试中,我是否需要添加
pthread_setschedparam(pthread_self(), SCHED_RR, &sp);
对于所有thread1,thread2和thread3,以便所有这3个可以使用SCHED_RR进行调度?或者我不需要这样做?!如何观察thread1,thread2和thread3线程是SCHED_RR还是SCHED_OTHER?
编辑:
sudo chrt -v -r 90 ./xxx.exe
等着瞧 :
pid 7187's new scheduling policy: SCHED_RR
pid 7187's new scheduling priority: 90
我怎么能确定这只是主线程?或者pid 7187中的所有线程都是SCHED_RR策略?!再次,如何观察它?!
1 回答
在创建任何新线程之前,您应检查(并设置,如果需要)调度程序继承属性 .
int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inheritsched);
int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched);
pthread_attr_getinheritsched()
将存储在由inheritsched
指向的变量中的两个可能值之一:如果您希望每个新创建的线程都继承调用任务的调度程序属性,则应设置PTHREAD_INHERIT_SCHED(如果尚未设置) .
Also note:
References