2012-12-20 29 views
6

我無法用pthread_attr_setschedparam()設置Pthread的優先級。我試圖解決這個問題,但無法做到這一點。我也諮詢了我的課本,它也使用了相同的功能。我從書中複製了這段代碼。你能告訴我如何設置線程優先級嗎?無法設置的Pthread優先

下面是代碼:

void *Func(void *arg); 
int main() 
{ 
pthread_t tid[5]; 

pthread_attr_t *tattr; 
struct sched_param param; 
int pr,error,i; 

do 
{ 
if((tattr=(pthread_attr_t *)malloc(sizeof(pthread_attr_t)))==NULL) 
{ 
    printf("Couldn't allocate memory for attribute object\n"); 
} 
}while(tattr==NULL); 

if(error=pthread_attr_init(tattr)) 
{ 
    fprintf(stderr,"Attribute initialization failed with error %s\n",strerror(error)); 
} 

for(i=0;i<5;i++) 
{ 
    //scanf("%d",&pr); 
     error = pthread_attr_getschedparam(tattr,&param); 

     if(error!=0) 
     { 
      printf("failed to get priority\n"); 
     } 

     param.sched_priority=10; 
     error=pthread_attr_setschedparam(tattr,&param); 

     if(error!=0) 
     { 
      printf("failed to set priority\n"); 
     } 
/* 
    if(i%2==0) 
    { 
     if(error=pthread_attr_setdetachstate(tattr,PTHREAD_CREATE_DETACHED)) 

     { 
      fprintf(stderr,"Failed to set thread attributes with error %s\n",strerror(error)); 
     } 
    } 
    else 
     if(error=pthread_attr_setdetachstate(tattr,PTHREAD_CREATE_JOINABLE)) 
     { 
      fprintf(stderr,"Failed to set thread attributes with error %s\n",strerror(error)); 
     } 
*/  
     pthread_create(&tid[i],tattr,Func,NULL); 


     printf("waiting for thread %d\n",i); 
} 

free(tattr);// release dynamically allocated memory 

printf("All threads terminated\n"); 
return 0; 
} 

void *Func(void *arg) 
{ 
printf("inside\n"); 

pthread_attr_t *tattr=(pthread_attr_t *)arg; 
int state,error; 

struct sched_param param; 

error=pthread_attr_getdetachstate(tattr,&state); 

if(error==0 && state==PTHREAD_CREATE_DETACHED) 
{ 
    printf(" My state is DETACHED\n"); 
} 
else 
    if(error==0 && state==PTHREAD_CREATE_JOINABLE) 
    { 
     printf(" My state is JOINABLE\n"); 
    } 

error=pthread_attr_getschedpolicy(tattr,&param); 

if(error==0) 
{ 
    printf(" My Priority is %d\n",param.sched_priority); 
} 

return NULL; 
} 
+0

你得到什麼結果呢?你怎麼知道它不工作? – Celada

+0

我打印'未能獲得優先級'來檢查線程的優先級是否設置。並始終打印,這意味着沒有設置優先級。 – Alfred

+1

你是否以root身份運行?只有root可以提高優先級(減少nice值) – anishsane

回答

4

pthread_attr_setschedparam呼叫與 「無效參數」 失敗。您的程序將以默認的linux調度策略SCHED_OTHER開始。您無法更改SCHED_OTHER的優先級。

從人(2)sched_setscheduler

SCHED_OTHER只能在靜態優先級使用0.1 SCHED_OTHER是非常適合那些不需要特殊的靜態優先級的所有進程的 標準Linux分時調度實時機制。

如果您在嘗試更改優先級時將pthread屬性中的策略更改爲另一種計劃,則程序將工作。喜歡的東西

for (i = 0;i < 5;i++) 
{ 
    policy = SCHED_RR; 

    error = pthread_attr_setschedpolicy(tattr, policy); 

    // insert error handling 

    error = pthread_attr_getschedparam(tattr, &param); 

    // insert error handling 

    // yada yada yada ... 
} 
+0

也把一個'pthread_exit'主及/或'pthread_join'相應的線程時,你取消對獨立式/加入代碼。 – Duck

0
I created thread in main and set attributes using  
pthread_attr_setechedparam(...) .Once the child thread is up and running  i want to check this thread priority and policy set before using 
pthread_getschedparam(, ,). I executed code sudo still it is printing  zero for both pri and policy. Please find the code below. 
#include "stdio.h" 

#include "stdlib.h" 

#include "pthread.h" 

void *Func(void *arg); 
int main() 
{ 

pthread_t tid; 

pthread_attr_t *tattr; 

struct sched_param param; 

int pr,error,i; 

int pol; 
do 

    { 

if((tattr=(pthread_attr_t *)malloc(sizeof(pthread_attr_t)))==NULL) 

{ 

    printf("Couldn't allocate memory for attribute object\n"); 
    } 


}while(tattr==NULL); 

    if(error=pthread_attr_init(tattr)) 
{ 
    fprintf(stderr,"Attribute initialization failed with error %s\n",strerror(error)); 
    } 

//for(i=0;i<5;i++) 
    //{ 
    error = pthread_attr_getschedparam(tattr,&param); 

    if(error!=0) 
    { 
     printf("failed to get priority\n"); 
    } 

    int policy=1; 
    error=pthread_attr_setschedpolicy(tattr,policy); 
    if(error!=0) 
    { 
     printf("failed to set priority\n"); 
    } 

    param.sched_priority=10; 
    error=pthread_attr_setschedparam(tattr,&param); 

    if(error!=0) 
    { 
     printf("failed to set priority\n"); 
    } 

    pthread_create(&tid,tattr,Func,NULL); 

    pthread_getschedparam(tid,&pol,&param); 
    printf("policy:: %d pri :: %d\n",pol,param.sched_priority); 

    printf("waiting for thread %d\n",i); 

    pthread_join(tid,NULL); 
    free(tattr);// release dynamically allocated memory 

    printf("All threads terminated\n"); 
    return 0; 
    } 

    void *Func(void *arg) 
    { 
    printf("inside\n"); 
    int policy,err=0; 
    struct sched_param p; 
    err=pthread_getschedparam(pthread_self(),&policy,&p); 
    if(err!=0) 
    { 
    printf("error\n"); 
    } 
    printf("the priorty= %d policy =%d\n",p.sched_priority,policy); 

    } 
0

此實現爲我工作(這個職位的幫助),這是簡單的:

pthread_t myThread; 
pthread_attr_t *tattr; 
struct sched_param param; 
int error,policy, prio; 

prio = 10; 

tattr = (pthread_attr_t *)malloc(sizeof(pthread_attr_t)); 
error = pthread_attr_init(tattr); 
cout << "init: " << error << endl; 

error = pthread_attr_getschedparam(tattr,&param); 
cout << "getschedparam: " << error << endl; 

error = pthread_attr_setinheritsched(tattr, PTHREAD_EXPLICIT_SCHED); 
cout << "setinheritsched: " << error << endl; 

policy = SCHED_RR; 
error = pthread_attr_setschedpolicy(tattr, policy); 
cout << "setschedpolicy = " << policy << ": " << error << endl; 

param.sched_priority = prio; 
error = pthread_attr_setschedparam(tattr ,&param); 
cout << "setschedparam: " << error << endl; 

error = pthread_create(&myThread,tattr,threadFunc,&numOfExecutions); 
cout << "create: " << error << endl; 

pthread_getschedparam(myThread,&policy,&param); 
printf("policy:: %d pri :: %d\n",policy,param.sched_priority); 

你可以找到我的工作代碼here