加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

c – 在linux中更改线程优先级和调度程序

发布时间:2020-12-13 19:38:03 所属栏目:Linux 来源:网络整理
导读:我有一个单线程应用程序.如果我使用下面的代码,我得到sched_setscheduler():不允许操作 . struct sched_param param;param.sched_priority = 1;if (sched_setscheduler(getpid(),SCHED_RR,param))printf(stderr,"sched_setscheduler(): %sn",strerror(errn

我有一个单线程应用程序.如果我使用下面的代码,我得到sched_setscheduler():不允许操作
.

struct sched_param param;
param.sched_priority = 1;
if (sched_setscheduler(getpid(),SCHED_RR,&param))
printf(stderr,"sched_setscheduler(): %sn",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(),&param))
            printf("error while setting thread priority to %d",tPriority);
}
最佳答案
该错误可能是由对实时优先级设置的限制(ulimit -r检查,ulimit -r 99允许1-99优先级)引起的.从pthread_setschedparam开始成功:如果你在没有-pthread选项的情况下编译,这个函数只是一个存根,就像其他一些pthread函数一样.使用-pthread选项,结果应该相同(strace显示使用相同的系统调用).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读