- /* run like this:
- gcc filename.c
- sudo ./a.out
- */
- #include <stdlib.h> /* exit */
- #include <stdio.h> /* printf */
- #include <sched.h> /* sched_**** */
- int main(int argc, char *argv[])
- {
- printf ("Policy %d\n", sched_getscheduler(0));
- printf("max priority: %d\n", sched_get_priority_max(0)); /* 99 for real-time process 0 non-real-time process */
- struct sched_param param;
- int maxFIFO;
- int maxRR;
- maxFIFO = sched_get_priority_max(SCHED_FIFO);
- maxRR = sched_get_priority_max(SCHED_RR);
- if(maxFIFO == -1 || maxRR == -1){
- perror("sched_get_priority_max() error!\n");
- exit(1);
- }
- param.sched_priority = maxFIFO;
- if(sched_setscheduler(getpid(), SCHED_FIFO, ¶m) == -1){
- perror("sched_setscheduler() error!\n");
- exit(1);
- }
- printf ("Policy %d\n", sched_getscheduler(0));
- param.sched_priority = maxRR;
- if(sched_setscheduler(getpid(), SCHED_RR, ¶m) == -1){
- perror("sched_setscheduler() error!\n");
- exit(1);
- }
- printf ("Policy %d\n", sched_getscheduler(0));
- return 0;
- }
最后执行a.out时必须具有su权限。
sched_normal 或者 sched_other是Linux默认的调度策略,其值为0
sched_fifo 为1
sched_rr 为2
其中后两个为实时进程的调度策略,第一个是非实时进程的调度策略。
- #include <pthread.h>
- #include <stdio.h>
- int main(){
- fprintf(stdout, "SCHED_FIFO\tmin:%d\tmax:%d\n", sched_get_priority_min(SCHED_FIFO), sched_get_priority_max(SCHED_FIFO));
- fprintf(stdout, "SCHED_RR\tmin:%d\tmax:%d\n", sched_get_priority_min(SCHED_RR), sched_get_priority_max(SCHED_RR));
- fprintf(stdout, "SCHED_OTHER\tmin:%d\tmax:%d\n", sched_get_priority_min(SCHED_OTHER), sched_get_priority_max(SCHED_OTHER));
- }
运行上面的程序,输出如下:
SCHED_FIFO min:1 max:99
SCHED_RR min:1 max:99
SCHED_OTHER min:0 max:0
扫描二维码关注公众号,回复:
1588700 查看本文章