无法设置优先级的Pthread [英] Unable to set Pthread Priority

查看:331
本文介绍了无法设置优先级的Pthread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法用 pthread_attr_setschedparam设置Pthread的优先级()。我试图解决这个问题,但不能这样做。我也咨询也使用同样的功能我的课本。我复制从书这种code。你能告诉我如何设置线程的优先级?

下面是code:

 无效* Func键(无效* ARG);
诠释的main()
{
的pthread_t TID [5];pthread_attr_t *对tattr;
结构sched_pa​​ram参数;
INT PR,错误,我;做
{
如果((对tattr =(pthread_attr_t *)malloc的(的sizeof(pthread_attr_t)))== NULL)
{
    的printf(无法分配的属性对象\\ n内存);
}
}而(对tattr == NULL);如果(误差= pthread_attr_init(对tattr))
{
    fprintf中(标准错误,字符串错误(错误)的属性初始化错误%S \\ n失败);
}对于(I = 0; I&小于5;我+ +)
{
    // scanf函数(%d个,&安培; PR);
        错误= pthread_attr_getschedparam(对tattr,&安培;参数);        如果(错误!= 0)
        {
            的printf(未能获得优先\\ n);
        }        param.sched_priority = 10;
        错误= pthread_attr_setschedparam(对tattr,&安培;参数);        如果(错误!= 0)
        {
            的printf(无法设置优先级\\ n);
        }
/ *
    如果(我%2 == 0)
    {
        如果(误差= pthread_attr_setdetachstate(对tattr,PTHREAD_CREATE_DETACHED))        {
            fprintf中(标准错误,字符串错误(错误)无法设置线程错误%S \\ n属性);
        }
    }
    其他
        如果(误差= pthread_attr_setdetachstate(对tattr,PTHREAD_CREATE_JOINABLE))
        {
            fprintf中(标准错误,字符串错误(错误)无法设置线程错误%S \\ n属性);
        }
* /
        在pthread_create(安培; TID [I],对tattr,Func键,NULL);
        的printf(等待线程%d个\\ N,I);
}免费的(对tattr); //释放动态分配的内存的printf(所有线程终止\\ n);
返回0;
} void *的函数功能(无效* ARG)
{
的printf(\\ n的内部);pthread_attr_t *对tattr =(pthread_attr_t *)ARG;
INT状态下,误差;结构sched_pa​​ram参数;错误= pthread_attr_getdetachstate(对tattr,&安培状态);如果(错误== 0安培;&放大器;状态== PTHREAD_CREATE_DETACHED)
{
    的printf(我的状态是分离\\ n);
}
其他
    如果(错误== 0安培;&放大器;状态== PTHREAD_CREATE_JOINABLE)
    {
        的printf(我的状态是可连接\\ n);
    }错误= pthread_attr_getschedpolicy(对tattr,&安培;参数);如果(错误== 0)
{
    输出(我的首要任务为%d \\ n,param.sched_priority);
}返回NULL;
}


解决方案

pthread_attr_setschedparam 电话与无效参数失败。你的程序将与 SCHED_OTHER 的默认的Linux调度策略开始。你不能改变的优先级 SCHED_OTHER

这是男人(2) sched_setscheduler


  

SCHED_OTHER只能在静态优先使用0 SCHED_OTHER是
         即意为不需要特殊的静态优先实时机制的所有过程标准Linux分时调度。


如果您更改的pthread属性的政策,以另一种时间表试图更改优先级程序将工作之前。类似

 为(i = 0;我小于5;我++)
{
    政策= SCHED_RR;    错误= pthread_attr_setschedpolicy(对tattr,政策);    //插入错误处理    错误= pthread_attr_getschedparam(对tattr,&安培;参数);    //插入错误处理    //亚达内容十分重要...
}

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

解决方案

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

这篇关于无法设置优先级的Pthread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆