linux内核工程师 2.09节 linux内核调度操作接口(调度策略)


[cpp]  view plain  copy
  1. /* run like this: 
  2.    gcc filename.c 
  3.    sudo ./a.out 
  4.  */  
  5. #include <stdlib.h>             /* exit */  
  6. #include <stdio.h>              /* printf */  
  7. #include <sched.h>              /* sched_**** */  
  8.   
  9.   
  10. int main(int argc, char *argv[])  
  11. {  
  12.     printf ("Policy %d\n", sched_getscheduler(0));  
  13.     printf("max priority: %d\n", sched_get_priority_max(0)); /* 99 for real-time process 0 non-real-time process */  
  14.     struct sched_param param;  
  15.     int maxFIFO;  
  16.     int maxRR;  
  17.     maxFIFO = sched_get_priority_max(SCHED_FIFO);  
  18.     maxRR = sched_get_priority_max(SCHED_RR);  
  19.     if(maxFIFO == -1 || maxRR == -1){  
  20.         perror("sched_get_priority_max() error!\n");  
  21.         exit(1);  
  22.     }  
  23.     param.sched_priority = maxFIFO;  
  24.     if(sched_setscheduler(getpid(), SCHED_FIFO, &param) == -1){  
  25.         perror("sched_setscheduler() error!\n");  
  26.         exit(1);  
  27.     }  
  28.     printf ("Policy %d\n", sched_getscheduler(0));  
  29.     param.sched_priority = maxRR;  
  30.     if(sched_setscheduler(getpid(), SCHED_RR, &param) == -1){  
  31.         perror("sched_setscheduler() error!\n");  
  32.         exit(1);  
  33.     }  
  34.     printf ("Policy %d\n", sched_getscheduler(0));  
  35.   
  36.   
  37.     return 0;  
  38. }  


最后执行a.out时必须具有su权限。


sched_normal 或者 sched_other是Linux默认的调度策略,其值为0

sched_fifo 为1

sched_rr 为2

其中后两个为实时进程的调度策略,第一个是非实时进程的调度策略。


  1. #include <pthread.h>  
  2. #include <stdio.h>  
  3. int main(){  
  4.     fprintf(stdout, "SCHED_FIFO\tmin:%d\tmax:%d\n", sched_get_priority_min(SCHED_FIFO), sched_get_priority_max(SCHED_FIFO));  
  5.     fprintf(stdout, "SCHED_RR\tmin:%d\tmax:%d\n", sched_get_priority_min(SCHED_RR), sched_get_priority_max(SCHED_RR));  
  6.     fprintf(stdout, "SCHED_OTHER\tmin:%d\tmax:%d\n", sched_get_priority_min(SCHED_OTHER), sched_get_priority_max(SCHED_OTHER));  
  7. }     

 运行上面的程序,输出如下:

SCHED_FIFO    min:1    max:99
SCHED_RR    min:1    max:99
SCHED_OTHER    min:0    max:0

扫描二维码关注公众号,回复: 1588700 查看本文章

猜你喜欢

转载自blog.csdn.net/zjy900507/article/details/80666233