I am unable to set Pthread priority using pthread_attr_setschedparam()
. I have tried to resolve this issue but couldn't do it. I also consulted my text book which also uses the same function. I copied this code from book. Can you tell me how to set thread priority?
Here is the code:
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,¶m);
if(error!=0)
{
printf("failed to get priority\n");
}
param.sched_priority=10;
error=pthread_attr_setschedparam(tattr,¶m);
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,¶m);
if(error==0)
{
printf(" My Priority is %d\n",param.sched_priority);
}
return NULL;
}
Your pthread_attr_setschedparam
call is failing with "invalid parameter". Your program will start with the default linux scheduling policy of SCHED_OTHER
. You can't change the priority of SCHED_OTHER
.
From man (2) sched_setscheduler
:
SCHED_OTHER can only be used at static priority 0. SCHED_OTHER is the standard Linux time-sharing scheduler that is intended for all processes that do not require special static priority real-time mechanisms.
If you change the policy in the pthread attribute to another kind of schedule prior to attempting to change the priority your program will work. Something like
for (i = 0;i < 5;i++)
{
policy = SCHED_RR;
error = pthread_attr_setschedpolicy(tattr, policy);
// insert error handling
error = pthread_attr_getschedparam(tattr, ¶m);
// insert error handling
// yada yada yada ...
}
This implementation worked for me (with this post help), it's straightforward:
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,¶m);
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 ,¶m);
cout << "setschedparam: " << error << endl;
error = pthread_create(&myThread,tattr,threadFunc,&numOfExecutions);
cout << "create: " << error << endl;
pthread_getschedparam(myThread,&policy,¶m);
printf("policy:: %d pri :: %d\n",policy,param.sched_priority);
You can find my working code here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With