无法在使用SCHED_RR创建时设置pthread优先级

无法在使用SCHED_RR创建时设置pthread优先级,c,multithreading,pthreads,linux-capabilities,C,Multithreading,Pthreads,Linux Capabilities,如何获得以初始优先级创建的pthread线程?在下面的代码中,我声明了执行此操作所需的caps,事实上,它确实将线程的优先级更改为15,但由于某种原因,线程总是以优先级0开始,即使我指定它需要被SCHED_RR 我还通过使用sudo setcap\u SYS\u NICE+eip[program]确保程序具有正确的功能。我已经试着以普通用户和root用户的身份运行它。这两种情况都是一样的 那么,我遗漏了什么呢?:) 谢谢 /* compiled with -lpthread -lcap */

如何获得以初始优先级创建的pthread线程?在下面的代码中,我声明了执行此操作所需的caps,事实上,它确实将线程的优先级更改为15,但由于某种原因,线程总是以优先级0开始,即使我指定它需要被SCHED_RR

我还通过使用
sudo setcap\u SYS\u NICE+eip[program]
确保程序具有正确的功能。我已经试着以普通用户和root用户的身份运行它。这两种情况都是一样的

那么,我遗漏了什么呢?:)

谢谢

/* compiled with -lpthread -lcap */

#include <stdlib.h>
#include <stdio.h>
#include <sys/capability.h>
#include <pthread.h>
#include <unistd.h>

pthread_t mythread;

void myfunction()
{
    int i ;
    int err_code;
    int policy;
    struct sched_param schedule;

    for (i=0; i < 70; i++)
    {
        pthread_getschedparam(pthread_self(), &policy, &schedule);
        printf("My policy is %d. My priority is %d\n", policy, schedule.sched_priority);
        sleep(1);
    }
}

int main()
{
    cap_t caps;
    char myCaps[] = "cap_sys_nice+eip";
    int err_code;
    int policy = SCHED_RR;
    struct sched_param schedule;
    pthread_attr_t attr;

    caps = cap_from_text(myCaps);

    if (caps == NULL)
    {
        printf("Caps is null :(\n");
        return 1;
    }

    printf("I have capabilities!\n");


    schedule.sched_priority = 80; //SCHED_RR goes from 1 -99
    pthread_attr_init(&attr);
    pthread_attr_setschedpolicy(&attr, policy);
    pthread_attr_setschedparam(&attr, &schedule);
    pthread_create(&mythread, NULL, (void *) &myFunction, NULL);
    sleep(3);

    schedule.sched_priority = 15; //Arbitrary, for testing purposes

    err_code = pthread_setschedparam(mythread, policy, &schedule);
    if (err_code != 0)
    {
        printf("I have failed you, master! Error: %d\n", err_code);
    }

    pthread_join(mythread, NULL);
    cap_fee(caps);

    return 0;
}
/*使用-lpthread-lcap编译*/
#包括
#包括
#包括
#包括
#包括
pthread_t mythread;
void myfunction()
{
int i;
内部错误代码;
int政策;
结构调度参数调度;
对于(i=0;i<70;i++)
{
pthread_getschedparam(pthread_self(),&policy,&schedule);
printf(“我的策略是%d。我的优先级是%d\n”,策略,计划。计划优先级);
睡眠(1);
}
}
int main()
{
帽子;
char myCaps[]=“cap\u sys\u nice+eip”;
内部错误代码;
int policy=SCHED_RR;
结构调度参数调度;
pthread_attr_t attr;
caps=cap_from_text(myCaps);
if(caps==NULL)
{
printf(“Caps为空:(\n”);
返回1;
}
printf(“我有能力!\n”);
schedule.sched_priority=80;//sched_RR从1到99
pthread_attr_init(&attr);
pthread_attr_setschedpolicy(&attr,policy);
pthread_attr_setschedparam(&attr,&schedule);
pthread_create(&mythread,NULL,(void*)&myFunction,NULL);
睡眠(3);
schedule.sched_priority=15;//任意,用于测试目的
err_code=pthread_setschedparam(mythread、policy和schedule);
如果(错误代码!=0)
{
printf(“我让你失望了,主人!错误:%d\n”,错误代码);
}
pthread_join(mythread,NULL);
cap_费用(caps);
返回0;
}

看起来您还必须使用pthread\u EXPLICIT\u SCHED调用pthread\u attr\u setinheritsched,以使其覆盖父(创建)线程的属性


实际上,在pthread\u create调用中初始化的pthread\u attr\t似乎没有实际使用。请注意,您将NULL作为arg 2传入。

mercurytw的答案也适用于问题中的代码。
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);