简体   繁体   English

sched_setscheduler是针对所有线程还是主线程?

[英]sched_setscheduler is for all threads or main thread?

I have the following source which like to have SCHED_RR priority 90 : 我有以下来源喜欢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);
}

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 虽然shell“top”,我可以看到进程有-91的PR,看起来很有效,据我所知,在Linux中,thread1和thread2以及thread3是来自主线程的不同任务,它们只是共享相同的虚拟内存,我想知道在这个测试中,我是否需要添加

pthread_setschedparam(pthread_self(), SCHED_RR, &sp);

for all thread1,thread2 and thread3 so that all these 3 can be scheduled with SCHED_RR ?! 对于所有thread1,thread2和thread3,以便所有这3个可以使用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 ?! 如何观察thread1,thread2和thread3线程是SCHED_RR还是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 ?! 或pid 7187中的所有线程都是SCHED_RR策略?! 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 by inheritsched one of two possible values: pthread_attr_getinheritsched()将存储在inheritsched指向的变量中,其中包含两个可能的值之一:

  • PTHREAD_INHERIT_SCHED - Threads that are created using attr PTHREAD_INHERIT_SCHED - 使用attr创建的线程
    inherit scheduling attributes from the creating thread; 从创建线程继承调度属性; the scheduling attributes in attr are ignored. attr中的调度属性被忽略。

  • PTHREAD_EXPLICIT_SCHED - Threads that are created using attr take their scheduling attributes from the values specified by the attributes object. PTHREAD_EXPLICIT_SCHED - 使用attr创建的线程从属性对象指定的值中获取其调度属性。

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). 如果您希望每个新创建的线程都继承调用任务的调度程序属性,则应设置PTHREAD_INHERIT_SCHED(如果尚未设置)。

Also note: 另请注意:

The default setting of the inherit-scheduler attribute in a newly initialized thread attributes object is PTHREAD_INHERIT_SCHED 新初始化的线程属性对象中的inherit-scheduler属性的默认设置是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.) (Blockquoted材料是从Linux man-pages项目的3.74版本的部分复制而来的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM