本文主要是介绍Linux实时调度策略(SCHED_RR)和CFS(SCHED_OTHER)之间的区别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本文试图从直观角度,说明SCHED_RR调度策略和SCHED_OTHER调度策略之间的区别。
在Linux上,SCHED_OTHER和SCHED_NORMAL的意思相同,它们都是指的CFS调度策略,只不过,在内核中,CFS定义为SCHED_NORMAL,在用户态,CFS定义为SCHED_OTHER.
可参照内核头文件和musl libc头文件:
测试代码如下:
#include <string.h>
#include <pthread.h>
#include <sched.h>
#include <stdio.h>void *child_thread(void *arg)
{int policy = 0;int max_priority = 0,min_priority = 0;struct sched_param param;pthread_attr_t attr;struct sched_param sp;bzero((void*)&sp, sizeof(sp));pthread_attr_init(&attr);pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);pthread_attr_getinheritsched(&attr,&policy);if(policy == PTHREAD_EXPLICIT_SCHED){printf("Inheritsched:PTHREAD_EXPLICIT_SCHED\n");}if(policy == PTHREAD_INHERIT_SCHED){printf("Inheritsched:PTHREAD_INHERIT_SCHED\n");}//pthread_attr_setschedpolicy(&attr,SCHED_RR);pthread_attr_setschedpolicy(&attr,SCHED_OTHER);pthread_attr_getschedpolicy(&attr,&policy);if(policy == SCHED_FIFO){printf("Schedpolicy:SCHED_FIFO\n");}if(policy == SCHED_RR){printf("Schedpolicy:SCHED_RR\n");}if(policy == SCHED_OTHER){printf("Schedpolicy:SCHED_OTHER\n");}max_priority = sched_get_priority_max(policy);min_priority = sched_get_priority_min(policy);printf("Maxpriority:%u\n",max_priority);printf("Minpriority:%u\n",min_priority);param.sched_priority = max_priority;pthread_attr_setschedparam(&attr,¶m);sp.sched_priority = 1;// Actually set the sched params for the current thread.if (0 == pthread_setschedparam(pthread_self(), policy, &sp)) {printf("IO Thread #%ld using high-priority scheduler!", pthread_self());}printf("sche
这篇关于Linux实时调度策略(SCHED_RR)和CFS(SCHED_OTHER)之间的区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!