sched_setscheduler是所有线程或主线程? [英] sched_setscheduler is for all threads or main thread?
问题描述
我有以下来源,希望有SCHED_RR优先90:
INT主(INT ARGC,字符** argv的)
{
为const char * sched_policy [] = {
SCHED_OTHER
SCHED_FIFO
SCHED_RR
SCHED_BATCH
};
结构sched_param SP = {
.sched_priority = 90
};
将为pid_t PID = GETPID();
的printf(PID =(%D)\\ n,PID);
sched_setscheduler(PID,SCHED_RR,&安培; SP);
的printf(调度策略为%s \\ n,sched_policy [sched_getscheduler(PID)]); TID的pthread_t;
在pthread_create(安培; TID,NULL,线程1,(无效*)(长)3);
在pthread_create(安培; TID,NULL,线程,(无效*)(长)3);
在pthread_create(安培; TID,NULL,Thread3,(无效*)(长)3);
而(1)
睡眠(100);
}
而壳顶,我可以看到进程与公关-91,看起来像它的工作原理,
据我所知,在Linux中,线程1和线程和thread3有不同的任务
从主线程,他们只是共享相同的虚拟内存,我想知道
在这个测试中,我需要添加
pthread_setschedparam(pthread_self(),SCHED_RR,&安培; SP);
为所有线程1,线程和thread3使所有这3个可预定
与SCHED_RR?!或者我不需要做呢?!我怎么可以观察
即线程1,线程和线程thread3是SCHED_RR或SCHED_OTHER?!
编辑:
须藤CHRT -v -r 90 ./xxx.exe
将看到:
PID 7187的新的调度策略:SCHED_RR
PID 7187新的调度优先级:90
我怎么能肯定这只是针对主线程?!或PID 7187的所有主题
是SCHED_RR政策?!又一次,如何观察呢?!
您应检查(和设置,如果需要的话)创建任何新的线程之前调度继承属性。
INT pthread_attr_getinheritsched(常量pthread_attr_t * ATTR,为int * inheritsched);
INT pthread_attr_setinheritsched(pthread_attr_t * ATTR,INT inheritsched);
的 pthread_attr_getinheritsched()
将通过 inheritsched
指出变量存储两个可能值之一:
-
PTHREAD_INHERIT_SCHED - 正在使用ATTR结果创建的线程
继承调度从创建线程属性;该
在ATTR调度属性将被忽略。
块引用> -
PTHREAD_EXPLICIT_SCHED - 正在使用ATTR创建线程取
它们的调度从由指定的属性值
属性对象。
块引用>
如果你想,每一个新创建的线程继承调用任务的调度属性,应该设置PTHREAD_INHERIT_SCHED(如果尚未设置)。
另外请注意:
在继承调度器属性的新的默认设置
初始化线程属性对象的 PTHREAD_INHERIT_SCHED
块引用>参考
$男人pthread_setschedparam
$男人pthread_attr_setinheritsched
- (Blockquoted材料从Linux手册的页面项目的释放3.74份复制。)
I have the following source which like to have SCHED_RR priority 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); }
while shell "top" , I can see that process has PR with -91 , look like it works, As I know , in Linux , thread1 and thread2 and thread3 are different tasks from the main thread , they just share the same virtual memory , I like to know in this test , should I need to add
pthread_setschedparam(pthread_self(), SCHED_RR, &sp);
for all thread1,thread2 and thread3 so that all these 3 can be scheduled with SCHED_RR ?! or I don't need to do that ?! and how can I observe that thread1,thread2 and thread3 thread are SCHED_RR or SCHED_OTHER ?!
Edit :
sudo chrt -v -r 90 ./xxx.exe
will see :
pid 7187's new scheduling policy: SCHED_RR pid 7187's new scheduling priority: 90
how can I be sure this is only for main thread ?! or all threads in pid 7187 are SCHED_RR policy ?! and again , how to observe it ?!
解决方案You shall check (and set, if required) the scheduler inheritance attributes before creating any new thread.
int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inheritsched);
int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched);
The
pthread_attr_getinheritsched()
will store in the variable pointed byinheritsched
one of two possible values:
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.
If you want that every newly created thread inherits the scheduler attributes of the calling task, you should set PTHREAD_INHERIT_SCHED (if not already set).
Also note:
The default setting of the inherit-scheduler attribute in a newly initialized thread attributes object is PTHREAD_INHERIT_SCHED
References
$ man pthread_setschedparam $ man pthread_attr_setinheritsched
- (Blockquoted material was copied from parts of release 3.74 of the Linux man-pages project.)
这篇关于sched_setscheduler是所有线程或主线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!