Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9027719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:48:46+00:00 2026-06-16T06:48:46+00:00

I am unable to set Pthread priority using pthread_attr_setschedparam() . I have tried to

  • 0

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,&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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T06:48:47+00:00Added an answer on June 16, 2026 at 6:48 am

    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, &param);
    
        // insert error handling
    
        // yada yada yada ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got a simple but strange problem. I am unable to set size
Perhaps I haven't found the right post but I have been unable to set
I am using Apache Hadoop (hadoop-0.20.203.0) and unable to set sticky bit for /tmp
I have created a divider in JSplitPane . I am unable to set the
I have a horribly coded set of controllers which I am unable to refactor
I wrote a custom partitioner but am unable to set it to the JobConf
Using Jetty 7.0.2, I am unable to set a custom session cookie path. I
ASP.NET using C#; .net3.5 I want to set some value for this label lblSkill1
This is the very simple code I am using to set a cookie in
I have a variable stoppoint defined in my makefile. I want to set this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.