sched_setscheduler

参考:SCHED_SETSCHEDULER

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <sched.h>  
  4. #include <errno.h>  
  5.   
  6. void get_prio(void)  
  7. {  
  8.     int ret;  
  9.     int policy;  
  10.     struct sched_param param;  
  11.   
  12.     policy = sched_getscheduler(0);  
  13.     printf("policy = %d\n", policy);  
  14.   
  15.     ret = sched_getparam(0, &param);  
  16.     if (ret == 0) {  
  17.         printf("sched_priority = %d\n", param.sched_priority);  
  18.     }  
  19.     else {  
  20.         perror("sched_getparam");  
  21.     }  
  22. }  
  23.   
  24. void set_prio(int prio)  
  25. {  
  26.     int ret;  
  27.     struct sched_param param;  
  28.   
  29.     param.sched_priority = prio;  
  30.     ret = sched_setscheduler(0, SCHED_RR, &param);  
  31.     if (ret != 0) {  
  32.         perror("sched_setscheduler");  
  33.     }  
  34. }  
  35.   
  36. int main(int argc, char **argv)  
  37. {  
  38.     set_prio(1);  
  39.     get_prio();  
  40.   
  41.     /* キー入力待ち */  
  42.     getchar();  
  43.   
  44.     set_prio(99);  
  45.     get_prio();  
  46.   
  47.     /* キー入力待ち */  
  48.     getchar();  
  49.   
  50.     return 0;  
  51. }  

ソースコード

実行結果

$ sudo ./sched_set_prio
policy = 2
sched_priority = 1

policy = 2
sched_priority = 99

別のターミナルからプロパティを確認

※ 起動時のプライオリティは 1

$ cat /proc/`pidof sched_set_prio`/sched | grep -e policy -e prio
sched_set_prio (110053, #threads: 1)
policy                                       :                    2
prio                                         :                   98
$ cat /proc/`pidof sched_set_prio`/stat | awk -F' ' '{print $18,$19}'
-2 0

※ プライオリティを 99 に変更する

$ cat /proc/`pidof sched_set_prio`/sched | grep -e policy -e prio
sched_set_prio (110053, #threads: 1)
policy                                       :                    2
prio                                         :                    0
$ cat /proc/`pidof sched_set_prio`/stat | awk -F' ' '{print $18,$19}'
-100 0
$

設定値(RTタスク)一覧

優先度設定値/proc/<pid>/sched/proc/<pid>stat
prioprioritynice
高い990-1000
低い198-20

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS