无法设置Pthread优先级

我无法使用pthread_attr_setschedparam()设置Pthread优先级。 我试图解决这个问题,但无法解决。 我还咨询了我的教科书,该教科书也使用相同的function。 我从书中复制了这段代码。 你能告诉我如何设置线程优先级吗?

这是代码:

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

您的pthread_attr_setschedparam调用失败并显示“invalid parameter”。 您的程序将从SCHED_OTHER的默认Linux调度策略开始。 您无法更改SCHED_OTHER的优先级。

来自man(2) sched_setscheduler

SCHED_OTHER只能用于静态优先级0. 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 ... } 
  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); } 

这个实现对我有用(有了这个post的帮助),它很简单:

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

你可以在这里找到我的工作代码