2017-10-19 85 views
1

在下面的代碼片段中,我創建了6個線程。各有不同的優先級。全局優先級數組中提到了優先級。我正在根據線索索引在每個線程內連續增加全局變量。如果線程優先級更高,我期待計數更高。但我的輸出不遵循優先概念pl。請參閱下面顯示的輸出順序。我在Ubuntu 16.04和Linux內核4.10上嘗試了這一點。Linux線程優先級,行爲不正常

O/P, 

Thread=0 

Thread=3 

Thread=2 

Thread=5 

Thread=1 

Thread=4 

pid=32155 count=4522138740 

pid=32155 count=4509082289 

pid=32155 count=4535088439 

pid=32155 count=4517943246 

pid=32155 count=4522643905 

pid=32155 count=4519640181 

代碼:

#include <stdio.h> 
#include <pthread.h> 
#define FAILURE -1 
#define MAX_THREADS 15 
long int global_count[MAX_THREADS]; 
/* priority of each thread */ 
long int priority[]={1,20,40,60,80,99}; 

void clearGlobalCounts() 
{ 
     int i=0; 
     for(i=0;i<MAX_THREADS;i++) 
       global_count[i]=0; 
} 

/** 
    thread parameter is thread index 
**/ 
void funcDoNothing(void *threadArgument) 
{ 
     int count=0; 
     int index = *((int *)threadArgument); 
     printf("Thread=%d\n",index); 
     clearGlobalCounts(); 
     while(1) 
     { 
       count++; 
       if(count==100) 
       {  
         global_count[index]++; 
         count=0; 
       } 
     } 
} 

int main() 
{ 
     int i=0; 
     for(int i=0;i<sizeof(priority)/sizeof(long int);i++) 
      create_thread(funcDoNothing, i,priority[i]); 
     sleep(3600); 
     for(i=0;i<sizeof(priority)/sizeof(long int);i++) 
     { 
       printf("pid=%d count=%ld\n",getpid(), 
           global_count[i]); 
     } 
} 

create_thread(void *func,int thread_index,int priority) 
{ 
    pthread_attr_t attr; 

    struct sched_param schedParam; 
    void *pParm=NULL; 
    int id; 
    int * index = malloc(sizeof(int)); 
    *index = thread_index; 
    void *res; 
    /* Initialize the thread attributes */ 
    if (pthread_attr_init(&attr)) 
    { 
      printf("Failed to initialize thread attrs\n"); 
      return FAILURE; 
    } 

    if(pthread_attr_setschedpolicy(&attr, SCHED_FIFO)) 
    { 
      printf("Failed to pthread_attr_setschedpolicy\n"); 
      return FAILURE; 
    } 

    if (pthread_attr_setschedpolicy(&attr, SCHED_FIFO)) 
    { 
      printf("Failed to setschedpolicy\n"); 
      return FAILURE; 
    } 

    /* Set the capture thread priority */ 
    pthread_attr_getschedparam(&attr, &schedParam);; 
    schedParam.sched_priority = sched_get_priority_max(SCHED_FIFO) - 1; 
    schedParam.sched_priority = priority; 
    if (pthread_attr_setschedparam(&attr, &schedParam)) 
    { 
      printf("Failed to setschedparam\n"); 
      return FAILURE; 
    }  
    pthread_create(&id, &attr, (void *)func, index); 
} 

回答

0

pthread_attr_setschedparam文檔說:

爲了通過 pthread_attr_setschedparam()方面的參數設定爲具有效果調用 在pthread_create時(3) ,調用者必須使用pthread_attr_setinheritsched(3) 來設置 的inherit-scheduler attrib將屬性對象attr的屬性設置爲PTHREAD_EXPLICIT_SCHED。

所以你必須調用pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED),例如:

if (pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) != 0) { 
      perror("pthread_attr_setinheritsched"); 
    } 
    pthread_create(&id, &attr, (void *)func, index); 

注:您的代碼會產生大量的編譯器警告,你需要修復那些。您不想嘗試測試具有大量未定義行爲的代碼 - 如某些警告所示。您應該將睡眠時間(3600)降低到幾秒鐘,因爲當您在SCHED_FIFO下運行線程時,它們會佔用您的CPU,並且機器在運行時會凍結。

+0

感謝輸入,行爲似乎好多了。我糾正了所有警告。 3600只是爲了確保最初的調度延遲不會影響計數。 O/P現在是,PID = 31068計數= 0 PID = 31068計數= 15 PID = 31068計數= 56961864 PID = 31068計數= 56964895 PID = 31068計數= 57002126 PID = 31068計數= 56688136仍最高優先級任務行爲並不那麼一致。我嘗試了30秒的睡眠,我會嘗試更高的延遲。現在我已經以root身份運行程序,爲什麼會有這種限制? –