9

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

这是代码:

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

3 回答 3

7

您的pthread_attr_setschedparam呼叫因“无效参数”而失败。您的程序将从默认的 linux 调度策略开始SCHED_OTHER。您无法更改 的优先级SCHED_OTHER

从人(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 ...
}
于 2012-12-20T06:54:21.420 回答
2

这个实现对我有用(在这篇文章的帮助下),它很简单:

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

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

于 2016-03-22T15:21:50.647 回答
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);

    }
于 2015-07-15T12:13:44.300 回答