xiaobu1990 2014-12-23 06:02 采纳率: 0%
浏览 1370

linux实时线程调度错误问题

测试程序如下,创建了3个SCHED__FIFO的实时线程,线程优先级不同,按理说应该是高优先级的先运行,也就是thread3运行完之后thread2运行,thread2运行完之后thread1才运行,但是在嵌入式设备上运行的打印信息显示却不是这样的。
#include
#include
void FunThread1()
{
int i,j;
int policy;
int priority;
struct sched_param param;

        sleep(1);
        pthread_getschedparam(pthread_self(),&policy,&param);
        priority = param.sched_priority;
        printf("priority %d, ",priority);
        if(policy==SCHED_OTHER)
                printf("SCHED_OTHER\n");
        if(policy==SCHED_RR)
                printf("SCHED_RR\n");
        if(policy==SCHED_FIFO)
                printf("SCHED_FIFO\n");
        for(i=1;i <10;i++)
        {
                for(j=1;j <50000;j++)
                {
                }
                printf("thread 1\n");
        }
        printf("Thread1 exit\n");
}
void FunThread2()
{
        int i,j;
        int policy;
        int priority;
        struct sched_param param;

        sleep(1);
        pthread_getschedparam(pthread_self(),&policy,&param);
        priority = param.sched_priority;
        printf("priority %d, ",priority);
        if(policy==SCHED_OTHER)
                printf("SCHED_OTHER 2\n");
        if(policy==SCHED_RR)
                printf("SCHED_RR 2\n");
        if(policy==SCHED_FIFO)
                printf("SCHED_FIFO 2\n");
        for(i=1;i <10;i++)
        {
                for(j=1;j <50000;j++)
                {
                }
                printf("thread 2\n");
        }
        //sched_yield();
        //sched_yield();
        printf("Thread2 exit\n");
}
void FunThread3()
{
        int i,j;
        int policy;
        int priority;
        struct sched_param param;

        sleep(1);
        pthread_getschedparam(pthread_self(),&policy,&param);
        priority = param.sched_priority;
        printf("priority %d, ",priority);
        if(policy==SCHED_OTHER)
                printf("SCHED_OTHER 3\n");
        if(policy==SCHED_RR)
                printf("SCHED_RR 3\n");
        if(policy==SCHED_FIFO)
                printf("SCHED_FIFO 3\n");
        for(i=1;i <10;i++)
        {
                for(j=1;j <50000;j++)
                {
                }
                printf("thread 3\n");
        }
        //sched_yield();
        //sched_yield();
        printf("Thread3 exit\n");
}
int main()
{
        int i;
        pthread_t ppid1,ppid2,ppid3;
        struct sched_param param1,param2,param3;
        pthread_attr_t attr1,attr2,attr3;

        i=getuid();
        if(i==0)
                printf("The current user is root\n");
        else
                printf("The current user is not root\n");

        pthread_attr_init(&attr1);
        pthread_attr_init(&attr2);
        pthread_attr_init(&attr3);

        param1.sched_priority=1;
        pthread_attr_setschedpolicy(&attr1,SCHED_FIFO);
        pthread_attr_setschedparam(&attr1,&param1);
        pthread_attr_setinheritsched(&attr1,PTHREAD_EXPLICIT_SCHED);   //新加,指定不继承父线程调度策略

        param2.sched_priority=2;
        pthread_attr_setschedpolicy(&attr2,SCHED_FIFO);
        pthread_attr_setschedparam(&attr2,&param2);
        pthread_attr_setinheritsched(&attr2,PTHREAD_EXPLICIT_SCHED);   //新加,指定不继承父线程调度策略

        param3.sched_priority=3;
        pthread_attr_setschedpolicy(&attr3,SCHED_FIFO);
        pthread_attr_setschedparam(&attr3,&param3);
        pthread_attr_setinheritsched(&attr3,PTHREAD_EXPLICIT_SCHED);   //新加,指定不继承父线程调度策略

        pthread_create(&ppid1,&attr1,(void *)FunThread1,NULL);    
        pthread_create(&ppid2,&attr2,(void *)FunThread2,NULL);
        pthread_create(&ppid3,&attr3,(void *)FunThread3,NULL);

        pthread_join(ppid1,NULL);
        pthread_join(ppid2,NULL);
        pthread_join(ppid3,NULL);

        pthread_attr_destroy(&attr1);
        pthread_attr_destroy(&attr2);
        pthread_attr_destroy(&attr3);

        return 0;
} 

嵌入式设备上的打印信息如下:
# ./sched
The current user is root
priority 2, SCHED_FIFO 2
priority 3, SCHED_FIFO 3
thread 2
thread 3
thread 2
thread 3
thread 2
thread 3
thread 2
thread 3
thread 2
thread 3
thread 2
thread 3
thread 2
thread 3
thread 2
thread 3
thread 2
Thread2 exit
thread 3
Thread3 exit
priority 1, SCHED_FIFO
thread 1
thread 1
thread 1
thread 1
thread 1
thread 1
thread 1
thread 1
thread 1
Thread1 exit
根据这个运行的打印信息,线程2和线程3怎么感觉是没有按照实时线程的调度方式执行,有点像公平调度呢?但是获取其调度方式也是SCHED-FIFO,想不通,求解?

  • 写回答

1条回答 默认 最新

  • m0_73083857 2022-09-06 16:57
    关注

    有可能是因为多核cpu,创建线程前绑定当前进程到指定cpu上,试试呢

    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题