2016-07-04 48 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的部分複製。)