ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

sched_setscheduler是用于所有线程还是主线程?

2019-11-18 12:50:36  阅读:847  来源: 互联网

标签:thread-priority scheduled-tasks c-3 linux


我有以下来源,希望具有SCHED_RR优先级90:

int main(int argc, char** argv)
{
    const char *sched_policy[] = {
    "SCHED_OTHER",
    "SCHED_FIFO",
    "SCHED_RR",
    "SCHED_BATCH"
    };
    struct sched_param sp = {
        .sched_priority = 90
    };
    pid_t pid = getpid();
    printf("pid=(%d)\n",pid);
    sched_setscheduler(pid, SCHED_RR, &sp);
    printf("Scheduler Policy is %s.\n", sched_policy[sched_getscheduler(pid)]);

    pthread_t tid ;
    pthread_create(&tid , NULL, Thread1 , (void*)(long)3);
    pthread_create(&tid , NULL, Thread2 , (void*)(long)3);
    pthread_create(&tid , NULL, Thread3 , (void*)(long)3);
    while(1)
        sleep(100);
}

当shell为“ top”时,我可以看到该进程具有-91的PR,看起来它可以正常工作,
据我所知,在Linux中,thread1和thread2和thread3是不同的任务
我喜欢从主线程共享相同的虚拟内存
在此测试中,我是否需要添加

pthread_setschedparam(pthread_self(), SCHED_RR, &sp);

对于所有线程1,线程2和线程3,以便可以安排所有这3个线程
与SCHED_RR?!还是我不需要这样做?以及我如何观察
线程1,线程2和线程3线程是SCHED_RR还是SCHED_OTHER?

编辑:

sudo chrt -v -r 90 ./xxx.exe 

等着瞧 :

pid 7187's new scheduling policy: SCHED_RR
pid 7187's new scheduling priority: 90

我如何确定这仅适用于主线程?或pid 7187中的所有线程
是SCHED_RR政策吗?再三,如何观察呢?

解决方法:

在创建任何新线程之前,应检查(并设置(如果需要))调度程序继承属性.

int pthread_attr_getinheritsched(const pthread_attr_t * attr,int * inheritsched);

int pthread_attr_setinheritsched(pthread_attr_t * attr,int继承)

pthread_attr_getinheritsched()将存储在继承的指向的变量中,这是两个可能值之一:

>

PTHREAD_INHERIT_SCHED – Threads that are created using attr
inherit scheduling attributes from the creating thread; the
scheduling attributes in attr are ignored.

>

PTHREAD_EXPLICIT_SCHED – Threads that are created using attr take
their scheduling attributes from the values specified by the
attributes object.

如果希望每个新创建的线程都继承调用任务的调度程序属性,则应设置PTHREAD_INHERIT_SCHED(如果尚未设置).

另请注意:

The default setting of the inherit-scheduler attribute in a newly
initialized thread attributes object is PTHREAD_INHERIT_SCHED

参考文献

$man pthread_setschedparam
$man pthread_attr_setinheritsched

>(带引号的材料是从Linux手册页项目3.74发行版的一部分中复制的.)

标签:thread-priority,scheduled-tasks,c-3,linux
来源: https://codeday.me/bug/20191118/2028290.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有