gpt4 book ai didi

c++ - 在 Linux 中更改线程优先级和调度程序

转载 作者:IT王子 更新时间:2023-10-29 00:56:45 24 4
gpt4 key购买 nike

我有一个单线程应用程序。如果我使用下面的代码,我会得到 sched_setscheduler(): Operation not permitted

struct sched_param param;
param.sched_priority = 1;
if (sched_setscheduler(getpid(), SCHED_RR, &param))
printf(stderr, "sched_setscheduler(): %s\n", strerror(errno));

但是,如果我使用如下所示的 pthread api,我不会收到错误。对于单线程应用程序,两者之间有什么区别?下面的函数是否真的改变了调度程序和优先级,或者我是否遗漏了一些错误处理?

void assignRRPriority(int tPriority)
{
int policy;
struct sched_param param;

pthread_getschedparam(pthread_self(), &policy, &param);
param.sched_priority = tPriority;
if(pthread_setschedparam(pthread_self(), SCHED_RR, &param))
printf("error while setting thread priority to %d", tPriority);
}

最佳答案

错误可能是由实时优先级的限制设置引起的(ulimit -r 进行检查,ulimit -r 99 允许 1-99 优先级)。从 pthread_setschedparam 开始成功:如果您在编译时没有使用 -pthread 选项,则此函数只是一个 stub ,就像其他一些 pthread 函数一样。使用 -pthread 选项,结果应该是相同的(strace 表明使用了相同的系统调用)。

关于c++ - 在 Linux 中更改线程优先级和调度程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14176292/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com