织梦CMS - 轻松建站从此开始!

罗索

Linux 线程调度与优先级

jackyhwei 发布于 2020-06-19 15:12 点击:次 
SCHED_OTHER是不支持优先级使用的,而SCHED_FIFO和SCHED_RR支持优先级的使用,他们分别为1和99,数值越大优先级越高。
TAG: 优先级  线程调度  

【转】
Linux内核的三种调度策略:

1,SCHED_OTHER 分时调度策略,
2,SCHED_FIFO实时调度策略,先到先服务。一旦占用cpu则一直运行。一直运行直到有更高优先级任务到达或自己放弃
  3,SCHED_RR实时调度策略,时间片轮转。当进程的时间片用完,系统将重新分配时间片,并置于就绪队列尾。放在队列尾保证了所有具有相同优先级的RR任务的调度公平
 

Linux线程优先级设置
   首先,可以通过以下两个函数来获得线程可以设置的最高和最低优先级,函数中的策略即上述三种策略的宏定义:

 int sched_get_priority_max(int policy);

  int sched_get_priority_min(int policy);

SCHED_OTHER是不支持优先级使用的,而SCHED_FIFO和SCHED_RR支持优先级的使用,他们分别为1和99,数值越大优先级越高。
设置和获取优先级通过以下两个函数

  1. int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param); 
  2.   int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param); 
  3.  param.sched_priority = 51; //设置优先级 

系统创建线程时,默认的线程是SCHED_OTHER。所以如果我们要改变线程的调度策略的话,可以通过下面的这个函数实现。

  1. int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy); 

上面的param使用了下面的这个数据结构:

  1. struct sched_param 
  2.     int __sched_priority; //所要设定的线程优先级 
  3. }; 

我们可以通过下面的测试程序来说明,我们自己使用的系统的支持的优先级:

  1. #include <stdio.h> 
  2. #include <pthread.h> 
  3. #include <sched.h> 
  4. #include <assert.h> 
  5.  
  6. static int get_thread_policy(pthread_attr_t *attr) 
  7.   int policy; 
  8.   int rs = pthread_attr_getschedpolicy(attr,&policy); 
  9.   assert(rs==0); 
  10.   switch(policy) 
  11.   { 
  12.   case SCHED_FIFO: 
  13.     printf("policySCHED_FIFO\n"); 
  14.     break; 
  15.   case SCHED_RR: 
  16.     printf("policySCHED_RR"); 
  17.     break; 
  18.   case SCHED_OTHER: 
  19.     printf("policy=SCHED_OTHER\n"); 
  20.     break; 
  21.   default: 
  22.     printf("policy=UNKNOWN\n"); 
  23.     break; 
  24.   } 
  25.   return policy; 
  26.  
  27. static void show_thread_priority(pthread_attr_t *attr,int policy) 
  28.   int priority = sched_get_priority_max(policy); 
  29.   assert(priority!=-1); 
  30.   printf("max_priority=%d\n",priority); 
  31.   prioritysched_get_priority_min(policy); 
  32.   assert(priority!=-1); 
  33.   printf("min_priority=%d\n",priority); 
  34.  
  35. static int get_thread_priority(pthread_attr_t *attr) 
  36.   struct sched_param param; 
  37.   int rs = pthread_attr_getschedparam(attr,&param); 
  38.   assert(rs==0); 
  39.   printf("priority=%d",param.__sched_priority); 
  40.   return param.__sched_priority; 
  41.  
  42. static void set_thread_policy(pthread_attr_t *attr,int policy) 
  43.   int rs = pthread_attr_setschedpolicy(attr,policy); 
  44.   assert(rs==0); 
  45.   get_thread_policy(attr); 
  46.  
  47. int main(void) 
  48.   pthread_attr_t attr; 
  49.   struct sched_param sched; 
  50.   int rs; 
  51.   rs = pthread_attr_init(&attr); 
  52.   assert(rs==0); 
  53.  
  54.   int policy = get_thread_policy(&attr); 
  55.   printf("Show current configuration of priority\n"); 
  56.     show_thread_priority(&attr,policy); 
  57.   printf("show SCHED_FIFO of priority\n"); 
  58.  show_thread_priority(&attr,SCHED_FIFO); 
  59.   printf("show SCHED_RR of priority\n"); 
  60.   show_thread_priority(&attr,SCHED_RR); 
  61.   printf("show priority of current thread\n"); 
  62.   int priority = get_thread_priority(&attr); 
  63.  
  64.   printf("Set thread policy\n"); 
  65.   printf("set SCHED_FIFO policy\n"); 
  66.   set_thread_policy(&attr,SCHED_FIFO); 
  67.   printf("set SCHED_RR policy\n"); 
  68.   set_thread_policy(&attr,SCHED_RR); 
  69.   printf("Restore current policy\n"); 
  70.   set_thread_policy(&attr,policy); 
  71.  
  72.   rs = pthread_attr_destroy(&attr); 
  73.   assert(rs==0); 
  74.   return 0; 

下面是测试程序的运行结果:

  1. policy=SCHED_OTHER 
  2. Show current configuration of priority 
  3. max_priority=0 
  4. min_priority=0 
  5. show SCHED_FIFO of priority 
  6. max_priority=99 
  7. min_priority=1 
  8. show SCHED_RR of priority 
  9. max_priority=99 
  10. min_priority=1 
  11. show priority of current thread 
  12. priority=0Set thread policy 
  13. set SCHED_FIFO policy 
  14. policySCHED_FIFO 
  15. set SCHED_RR policy 
  16. policySCHED_RRRestore current policy 
  17. policy=SCHED_OTHER 

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

  1. #include <stdio.h> 
  2. #include <unistd.h> 
  3. #include <stdlib.h> 
  4. #include <pthread.h> 
  5.  
  6. void Thread1() 
  7.   sleep(1); 
  8.   int i,j; 
  9.   int policy; 
  10.   struct sched_param param; 
  11.   pthread_getschedparam(pthread_self(),&policy,&param); 
  12.   if(policy == SCHED_OTHER) 
  13.     printf("SCHED_OTHER\n"); 
  14.   if(policy == SCHED_RR); 
  15.   printf("SCHED_RR 1 \n"); 
  16.   if(policy==SCHED_FIFO) 
  17.     printf("SCHED_FIFO\n"); 
  18.  
  19.   for(i=1;i<10;i++) 
  20.   { 
  21.     for(j=1;j<5000000;j++) 
  22.     { 
  23.     } 
  24.     printf("thread 1\n"); 
  25.   } 
  26.   printf("Pthread 1 exit\n"); 
  27.  
  28. void Thread2() 
  29.   sleep(1); 
  30.   int i,j,m; 
  31.   int policy; 
  32.   struct sched_param param; 
  33. pthread_getschedparam(pthread_self(),&policy,&param); 
  34.  if(policy == SCHED_OTHER) 
  35.     printf("SCHED_OTHER\n"); 
  36.   if(policy == SCHED_RR); 
  37.   printf("SCHED_RR\n"); 
  38.   if(policy==SCHED_FIFO) 
  39.     printf("SCHED_FIFO\n"); 
  40.  
  41.   for(i=1;i<10;i++) 
  42.   { 
  43.     for(j=1;j<5000000;j++) 
  44.     { 
  45.       
  46.     } 
  47.     printf("thread 2\n"); 
  48.   } 
  49.   printf("Pthread 2 exit\n"); 
  50.  
  51. void Thread3() 
  52.   sleep(1); 
  53.   int i,j; 
  54.   int policy; 
  55.   struct sched_param param; 
  56. pthread_getschedparam(pthread_self(),&policy,&param); 
  57.  if(policy == SCHED_OTHER) 
  58.     printf("SCHED_OTHER\n"); 
  59.   if(policy == SCHED_RR) 
  60.     printf("SCHED_RR \n"); 
  61.   if(policy==SCHED_FIFO) 
  62.     printf("SCHED_FIFO\n"); 
  63.  
  64.   for(i=1;i<10;i++) 
  65.   { 
  66.     for(j=1;j<5000000;j++) 
  67.     { 
  68.     } 
  69.     printf("thread 3\n"); 
  70.   } 
  71.   printf("Pthread 3 exit\n"); 
  72.  
  73. int main() 
  74.   int i; 
  75.   i = getuid(); 
  76.   if(i==0) 
  77.     printf("The current user is root\n"); 
  78.   else 
  79.     printf("The current user is not root\n"); 
  80.  
  81.   pthread_t ppid1,ppid2,ppid3; 
  82.   struct sched_param param; 
  83.  
  84.   pthread_attr_t attr,attr1,attr2; 
  85.    
  86.   pthread_attr_init(&attr1); 
  87. pthread_attr_init(&attr); 
  88. pthread_attr_init(&attr2); 
  89.   param.sched_priority = 51
  90.  pthread_attr_setschedpolicy(&attr2,SCHED_RR); 
  91.  pthread_attr_setschedparam(&attr2,&param); 
  92.  pthread_attr_setinheritsched(&attr2,PTHREAD_EXPLICIT_SCHED);//要使优先级其作用必须要有这句话 
  93.  
  94.  param.sched_priority = 21
  95.  pthread_attr_setschedpolicy(&attr1,SCHED_RR); 
  96.  pthread_attr_setschedparam(&attr1,&param); 
  97.  pthread_attr_setinheritsched(&attr1,PTHREAD_EXPLICIT_SCHED); 
  98.   
  99.  pthread_create(&ppid3,&attr,(void *)Thread3,NULL); 
  100.  pthread_create(&ppid2,&attr1,(void *)Thread2,NULL); 
  101.  pthread_create(&ppid1,&attr2,(void *)Thread1,NULL); 
  102.   
  103.  pthread_join(ppid3,NULL); 
  104.  pthread_join(ppid2,NULL); 
  105.  pthread_join(ppid1,NULL); 
  106.  pthread_attr_destroy(&attr2); 
  107.  pthread_attr_destroy(&attr1); 
  108.  return 0; 

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

  1. sudo ./prio_test 
  2. The current user is root 
  3. SCHED_OTHER 
  4. SCHED_RR 
  5. SCHED_RR 1 
  6. thread 1 
  7. thread 1 
  8. thread 1 
  9. thread 1 
  10. thread 1 
  11. thread 1 
  12. thread 1 
  13. thread 1 
  14. thread 1 
  15. Pthread 1 exit 
  16. thread 2 
  17. thread 2 
  18. thread 2 
  19. thread 2 
  20. thread 2 
  21. thread 2 
  22. thread 2 
  23. thread 2 
  24. thread 2 
  25. Pthread 2 exit 
  26. thread 3 
  27. thread 3 
  28. thread 3 
  29. thread 3 
  30. thread 3 
  31. thread 3 
  32. thread 3 
  33. thread 3 
  34. thread 3 
  35. 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),而且用完了它的时间片。
   基于时间片轮转的实时进程是,不是真正的改变进程的优先级,而是改变进程的基本时间片的长度。所以基于时间片轮转的进程调度,并不能保证高优先级的进程先运行。
   下面是另一种运行结果:

  1. sudo ./prio_test 
  2. The current user is root 
  3. SCHED_OTHER 
  4. SCHED_RR 1 
  5. thread 1 
  6. thread 1 
  7. thread 1 
  8. thread 1 
  9. thread 1 
  10. thread 1 
  11. thread 1 
  12. thread 1 
  13. thread 1 
  14. Pthread 1 exit 
  15. SCHED_RR 
  16. thread 2 
  17. thread 2 
  18. thread 2 
  19. thread 2 
  20. thread 2 
  21. thread 2 
  22. thread 2 
  23. thread 2 
  24. thread 2 
  25. Pthread 2 exit 
  26. thread 3 
  27. thread 3 
  28. thread 3 
  29. thread 3 
  30. thread 3 
  31. thread 3 
  32. thread 3 
  33. thread 3 
  34. thread 3 
  35. Pthread 3 exit 

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

Remember don't at a loss and let the brain to calm down when comes questions, so can solve them faster!
(xiaotlili)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/202006/17700.html]
本文出处:博客园 作者:xiaotlili 原文
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容