• Linux线程调度策略与优先级


    下面的这个测试程序,创建了三个线程,默认创建的线程的调度策略是SCHED_OTHER,其余的两个线程的调度策略设置成SCHED_RR。我的Linux的内核版本是2.6.31。SCHED_RR是根据时间片来确定线程的调度。时间片用完了,不管这个线程的优先级有多高都不会在运行,而是进入就绪队列中,等待下一个时间片的到了,那这个时间片到底要持续多长时间?在《深入理解Linux内核》中的第七章进程调度中,是这样描诉的,Linux采取单凭经验的方法,即选择尽可能长、同时能保持良好相应时间的一个时间片。这里也没有给出一个具体的时间来,可能会根据不同的CPU 来定,还有就是多CPU 的情况。

    #include 
    #include 
    #include 
    #include 
    
    void Thread1()
    {
      sleep(1);
      int i,j;
      int policy;
      struct sched_param param;
      pthread_getschedparam(pthread_self(),&policy,¶m);
      if(policy == SCHED_OTHER)
        printf("SCHED_OTHER\n");
      if(policy == SCHED_RR);
      printf("SCHED_RR 1 \n");
      if(policy==SCHED_FIFO)
        printf("SCHED_FIFO\n");
    
      for(i=1;i<10;i++)
      {
        for(j=1;j<5000000;j++)
        {
        }
        printf("thread 1\n");
      }
      printf("Pthread 1 exit\n");
    }
    
    void Thread2()
    {
      sleep(1);
      int i,j,m;
      int policy;
      struct sched_param param;
    pthread_getschedparam(pthread_self(),&policy,¶m);
     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<5000000;j++)
        {
         
        }
        printf("thread 2\n");
      }
      printf("Pthread 2 exit\n");
    }
    
    void Thread3()
    {
      sleep(1);
      int i,j;
      int policy;
      struct sched_param param;
    pthread_getschedparam(pthread_self(),&policy,¶m);
     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<5000000;j++)
        {
        }
        printf("thread 3\n");
      }
      printf("Pthread 3 exit\n");
    }
    
    int main()
    {
      int i;
      i = getuid();
      if(i==0)
        printf("The current user is root\n");
      else
        printf("The current user is not root\n");
    
      pthread_t ppid1,ppid2,ppid3;
      struct sched_param param;
    
      pthread_attr_t attr,attr1,attr2;
      
      pthread_attr_init(&attr1);
    pthread_attr_init(&attr);
    pthread_attr_init(&attr2);
      param.sched_priority = 51;
     pthread_attr_setschedpolicy(&attr2,SCHED_RR);
     pthread_attr_setschedparam(&attr2,¶m);
     pthread_attr_setinheritsched(&attr2,PTHREAD_EXPLICIT_SCHED);//要使优先级其作用必须要有这句话
    
     param.sched_priority = 21;
     pthread_attr_setschedpolicy(&attr1,SCHED_RR);
     pthread_attr_setschedparam(&attr1,¶m);
     pthread_attr_setinheritsched(&attr1,PTHREAD_EXPLICIT_SCHED);
     
     pthread_create(&ppid3,&attr,(void *)Thread3,NULL);
     pthread_create(&ppid2,&attr1,(void *)Thread2,NULL);
     pthread_create(&ppid1,&attr2,(void *)Thread1,NULL);
     
     pthread_join(ppid3,NULL);
     pthread_join(ppid2,NULL);
     pthread_join(ppid1,NULL);
     pthread_attr_destroy(&attr2);
     pthread_attr_destroy(&attr1);
     return 0;
    }


    下面是该程序的其中之一的运行结果:

    sudo
    ./prio_test
    The current user is root
    SCHED_OTHER
    SCHED_RR
    SCHED_RR 1
    thread 1
    thread 1
    thread 1
    thread 1
    thread 1
    thread 1
    thread 1
    thread 1
    thread 1
    Pthread 1 exit
    thread 2
    thread 2
    thread 2
    thread 2
    thread 2
    thread 2
    thread 2
    thread 2
    thread 2
    Pthread 2 exit
    thread 3
    thread 3
    thread 3
    thread 3
    thread 3
    thread 3
    thread 3
    thread 3
    thread 3
    Pthread 3 exit

       这里我们可以看到,由于线程3的调度策略是SCHED_OTHER,而线程2的调度策略是SCHED_RR,所以,在Thread3中,线程3被线程1,线程2给抢占了。由于线程1的优先级大于线程2的优先级,所以,在线程1以先于线程2运行,不过,这里线程2有一部分代码还是先于线程1运行了。
        我原以为,只要线程的优先级高,就会一定先运行,其实,这样的理解是片面的,特别是在SMP的PC机上更会增加其不确定性。
        其实,普通进程的调度,是CPU根据进程优先级算出时间片,这样并不能一定保证高优先级的进程一定先运行,只不过和优先级低的进程相比,通常优先级较高的进程获得的CPU时间片会更长而已。其实,如果要想保证一个线程运行完在运行另一个线程的话,就要使用多线程的同步技术,信号量,条件变量等方法。 而不是 绝对依靠优先级的高低,来保证。
        不过,从运行的结果上,我们可以看到, 调度策略为SCHED_RR的线程1,线程2确实抢占了调度策略为SCHED_OTHER的线程3。这个是可以理解的,由于SCHER_RR是实时调度策略。
       只有在下述事件之一发生时,实时进程才会被另外一个进程取代。
      (1) 进程被另外一个具有更高实时优先级的实时进程抢占。
      (2) 进程执行了阻塞操作并进入睡眠
      (3)进程停止(处于TASK_STOPPED 或TASK_TRACED状态)或被杀死。
      (4)进程通过调用系统调用sched_yield(),自愿放弃CPU 。
      (5)进程基于时间片轮转的实时进程(SCHED_RR),而且用完了它的时间片。
       基于时间片轮转的实时进程是,不是真正的改变进程的优先级,而是改变进程的基本时间片的长度。所以基于时间片轮转的进程调度,并不能保证高优先级的进程先运行。
       下面是另一种运行结果:
     

    sudo
    ./prio_test
    The current user is root
    SCHED_OTHER
    SCHED_RR 1
    thread 1
    thread 1
    thread 1
    thread 1
    thread 1
    thread 1
    thread 1
    thread 1
    thread 1
    Pthread 1 exit
    SCHED_RR
    thread 2
    thread 2
    thread 2
    thread 2
    thread 2
    thread 2
    thread 2
    thread 2
    thread 2
    Pthread 2 exit
    thread 3
    thread 3
    thread 3
    thread 3
    thread 3
    thread 3
    thread 3
    thread 3
    thread 3
    Pthread 3 exit

      可以看出并没有每一次都保证高优先级的线程先运行。

  • 相关阅读:
    Linux命令(93)之su
    sass 封装媒体查询工具
    ORACLE-统计信息收集&&分析表和索引
    节省50%带宽,这款媒体处理产品了解下!
    菜刀webshell特征分析
    Spring 面向切面编程 第1关:使用前后置通知统计所有方法的执行时间
    盘点企业数据管理的五种高效工具
    上周热点回顾(3.28-4.3)
    apache httpd 换行解析漏洞
    【软考 系统架构设计师】嵌入式系统④ 嵌入式操作系统
  • 原文地址:https://blog.csdn.net/weixin_72426331/article/details/127351578