sched_setscheduler是用于所有线程还是主线程?
作者:互联网
我有以下来源,希望具有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);
对于所有线程1,线程2和线程3,以便可以安排所有这3个线程
与SCHED_RR?!还是我不需要这样做?以及我如何观察
线程1,线程2和线程3线程是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政策吗?再三,如何观察呢?
解决方法:
在创建任何新线程之前,应检查(并设置(如果需要))调度程序继承属性.
int pthread_attr_getinheritsched(const pthread_attr_t * attr,int * inheritsched);
int pthread_attr_setinheritsched(pthread_attr_t * attr,int继承)
pthread_attr_getinheritsched()将存储在继承的指向的变量中,这是两个可能值之一:
>
PTHREAD_INHERIT_SCHED – Threads that are created using attr
inherit scheduling attributes from the creating thread; the
scheduling attributes in attr are ignored.
>
PTHREAD_EXPLICIT_SCHED – Threads that are created using attr take
their scheduling attributes from the values specified by the
attributes object.
如果希望每个新创建的线程都继承调用任务的调度程序属性,则应设置PTHREAD_INHERIT_SCHED(如果尚未设置).
另请注意:
The default setting of the inherit-scheduler attribute in a newly
initialized thread attributes object is PTHREAD_INHERIT_SCHED
参考文献
$man pthread_setschedparam
$man pthread_attr_setinheritsched
>(带引号的材料是从Linux手册页项目3.74发行版的一部分中复制的.)
标签:thread-priority,scheduled-tasks,c-3,linux 来源: https://codeday.me/bug/20191118/2028290.html