2016-07-04 27 views
3

我有以下来源,希望有SCHED_RR优先90:sched_setscheduler适用于所有线程或主线程?

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); 
} 

而壳“顶”,我可以看到进程有PR与-91,看起来像它的工作原理, 据我所知,在Linux中,线程1和线程和thread3是从主线程不同的任务 ,他们只是共享相同的虚拟内存,我想知道 在这个测试中,我需要添加

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

所有线程1,线程2和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策略?!并再次,如何观察它?!

回答

3

在创建任何新线程之前,您应该检查(并在必要时设置)调度程序继承属性。

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 - 线程正在使用ATTR
    创建 从创建线程继承调度属性; attr中的 调度属性被忽略。

  • PTHREAD_EXPLICIT_SCHED - 正在使用attr创建线程采取 它们的调度属性从由 属性指定的值对象。

如果你想,每一个新创建的线程继承调用任务的调度属性,你应该设置PTHREAD_INHERIT_SCHED(如果尚未设置)。

还要注意:

在新 初始化的线程的继承调度属性的默认设置属性对象是PTHREAD_INHERIT_SCHED

参考

$ man pthread_setschedparam 
$ man pthread_attr_setinheritsched 
  • (Blockquoted材料从Linux的人的页面项目发布3.74的部分复制。)