Linux 线程调度策略与线程优先级
Linux内核的三种调度策略SCHED_OTHER分时调度策略。 SCHED_FIFO实时调度策略, SCHED_RR实时调度策略,时间片轮转。 系统创建线程时,默认的线程是SCHED_OTHER。所以如果我们要改变线程的调度策略的话,可以通过下面的这个函数实现: int pthread_attr_setschedpolicy(pthread_attr_t *attr,int policy); Linux线程优先级设置首先,可以通过以下两个函数来获得线程可以设置的最高和最低优先级,函数中的策略即上述三种策略的宏定义: int sched_get_priority_max(int policy); int sched_get_priority_min(int policy); SCHED_OTHER是不支持优先级使用的,而SCHED_FIFO和SCHED_RR支持优先级的使用,他们分别为1和99,数值越大优先级越高。 int pthread_attr_setschedparam(pthread_attr_t *attr,const struct sched_param *param); int pthread_attr_getschedparam(const pthread_attr_t *attr,struct sched_param *param); 上面的param使用了下面的这个数据结构: struct sched_param { int __sched_priority; //所要设定的线程优先级 }; 例如: param.sched_priority = 51; //设置优先级 测试程序#include <stdio.h> #include <pthread.h> #include <sched.h> #include <assert.h> //打印当前的线程调度策略 static int get_thread_policy(pthread_attr_t *attr) { int policy; int rs = pthread_attr_getschedpolicy(attr,&policy); assert(rs==0); switch(policy) { case SCHED_FIFO: printf("policy= SCHED_FIFOn"); break; case SCHED_RR: printf("policy= SCHED_RRn"); break; case SCHED_OTHER: printf("policy=SCHED_OTHERn"); break; default: printf("policy=UNKNOWNn"); break; } return policy; } //打印当前调度策略线程的最高和最低优先级 static void show_thread_priority(pthread_attr_t *attr,int policy) { int priority = sched_get_priority_max(policy); assert(priority!=-1); printf("max_priority=%dn",priority); priority= sched_get_priority_min(policy); assert(priority!=-1); printf("min_priority=%dn",priority); } //打印当前线程的优先级 static int get_thread_priority(pthread_attr_t *attr) { struct sched_param param; int rs = pthread_attr_getschedparam(attr,¶m); assert(rs==0); printf("priority=%dn",param.__sched_priority); return param.__sched_priority; } //设置线程线程的调度策略 static void set_thread_policy(pthread_attr_t *attr,int policy) { int rs = pthread_attr_setschedpolicy(attr,policy); assert(rs==0); get_thread_policy(attr); } int main(void) { pthread_attr_t attr; struct sched_param sched; int rs; rs = pthread_attr_init(&attr); assert(rs==0); //获取默认的线程调度策略 printf("== Show current configuration of priority ==n"); int policy = get_thread_policy(&attr); show_thread_priority(&attr,policy); //打印当前线程的优先级 printf("show priority of current threadn"); int priority = get_thread_priority(&attr); printf("== Show different Scheduling method priority ==n"); //获取SCHED_FIFO调度策略的最高和最低优先级 printf("show SCHED_FIFO of priority:n"); show_thread_priority(&attr,SCHED_FIFO); //获取SCHED_RR调度策略的最高和最低优先级 printf("show SCHED_RR of priority:n"); show_thread_priority(&attr,SCHED_RR); printf("== Set thread policy == n"); //设置线程的调度属性为SCHED_FIFO printf("set SCHED_FIFO policy:n"); set_thread_policy(&attr,SCHED_FIFO); priority = get_thread_priority(&attr); sched.__sched_priority = 99; pthread_attr_setschedparam(&attr,&sched); priority = get_thread_priority(&attr); //设置线程的调度属性为SCHED_FIFO printf("set SCHED_RR policy:n"); set_thread_policy(&attr,SCHED_RR); priority = get_thread_priority(&attr); sched.__sched_priority = 55; pthread_attr_setschedparam(&attr,&sched); priority = get_thread_priority(&attr); //恢复线程的调度策略 printf("Restore current policy:n"); set_thread_policy(&attr,policy); rs = pthread_attr_destroy(&attr); assert(rs==0); return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |