linux内核的三种调度策略:
- SCHED_OTHER分时调度策略
- SCHED_FIFO实时调度策略,先到先服务
- SHCED_RR实时调度策略(时间片轮转)
RR和FIFO属于实时任务,创建优先级大于0(1-99),按照可抢占优先级调度算法进行,就绪态的实时任务立即抢占非实时任务
主要是由下列2个函数可以获取线程设置的最高级和最低优先级
int sched_get_priority_max(int policy); //获取实时优先级的最大值
int sched_get_priority_min(int policy); //获取实时优先级的最大值
SCHED_OTHER它不支持优先级使用,SCHED_FIFO/SCHED_RR支持优先级使用,它们分别是1和99,数值越大优先级越高
实时调度策略(SCHED_FIFO/SCHED_RR) 优先级最大值为99
普通调度策略(SCHED_NORMAL/SCHED_BATCH/SCHED_IDLE),始终返回0,即普通任务调度的函数,没有效果
设备与获取优先级的两个主要函数:
int pthread_attr_setschedparam(pthread_attr_t *attr,const struct sched_param *param);//创建线程优先级
int pthread_attr_getschedparam(pthread_attr_t *attr,const struct sched_param *param);//获取线程优先级
param.sched_priority=51;//设置优先级
当系统创建线程时,默认线程是SCHED_OTHER,改变调度策略,可以通过以下的函数:
int pthread_attr_setschedpolicy(pthread_attr_t *attr,int policy);//设置线程调度策略
struct sched_param
{int __sched_priority;//所有设定的线程优先级}
实战
#include <stdio.h>
#include <pthread.h>
#include <sched.h>
#include <assert.h>
static int get_thread_policy(pthread_attr_t *attr)
{
int plicy;
int rs=pthread_attr_getschedpolicy(attr,&plicy);
assert(rs==0);
switch(plicy)
{
case SCHED_FIFO:
printf("policy=SCHED_FIFO.\n");
break;
case SCHED_RR:
printf("policy=SCHED_RR.\n");
break;
case SCHED_OTHER:
printf("policy=SCHED_OTHER.\n");
break;
default:
printf("policy=UNKNOWN.\n");
break;
}
return plicy;
}
static void show_thread_priority(pthread_attr_t *attr,int policy)
{
int priority=sched_get_priority_max(policy);
assert(priority!=-1);
printf("max_priority=%d\n",priority);
priority=sched_get_priority_min(policy);
assert(priority!=-1);
printf("min_priority=%d\n",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=%d",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(0==rs);
get_thread_policy(attr);
}
int main()
{
pthread_attr_t attr;
struct sched_param sched;
int rs=pthread_attr_init(&attr);
assert(0==rs);
int plicy=get_thread_policy(&attr);
printf("output current configuration of priority.\n");
show_thread_priority(&attr,plicy);
printf("output SCHED_FIFO of priority.\n");
show_thread_priority(&attr,SCHED_FIFO);
printf("output SCHED_RR of priority.\n");
show_thread_priority(&attr,SCHED_RR);
printf("output priority of current thread.\n");
int priority=get_thread_priority(&attr);
printf("set thrad policy.\n");
printf("set SCHED_FIFO polity.\n");
set_thread_policy(&attr,SCHED_FIFO);
printf("set SCHED_RR policy.\n");
set_thread_policy(&attr,SCHED_RR);
printf("restore current policy.\n");
set_thread_policy(&attr,plicy);
rs=pthread_attr_destroy(&attr);c
assert(0==rs);
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void threadfunc1()
{
sleep(1);
int policy;
struct sched_param praram;
pthread_getschedparam(pthread_self(),&policy,&praram);
if(policy==SCHED_OTHER)
printf("SCHED_OTHER.\n");
if(policy==SCHED_RR)
;
printf("SCHED_RR 1.\n");
if(policy==SCHED_FIFO)
printf("SCHED_FIFO.\n");
for(int i=1;i<=10;i++)
{
for(int j=1;j<4000000;j++){
}
printf("Threadfunc1.\n");
}
printf("pthreadfunc1 EXIT.\n");
}
void threadfunc2()
{
sleep(1);
int policy;
struct sched_param praram;
pthread_getschedparam(pthread_self(),&policy,&praram);
if(policy==SCHED_OTHER)
printf("SCHED_OTHER.\n");
if(policy==SCHED_RR)
;
printf("SCHED_RR 1.\n");
if(policy==SCHED_FIFO)
printf("SCHED_FIFO.\n");
for(int i=1;i<=10;i++)
{
for(int j=1;j<4000000;j++){
}
printf("Threadfunc2.\n");
}
printf("pthreadfunc2 EXIT.\n");
}
void threadfunc3()
{
sleep(1);
int policy;
struct sched_param praram;
pthread_getschedparam(pthread_self(),&policy,&praram);
if(policy==SCHED_OTHER)
printf("SCHED_OTHER.\n");
if(policy==SCHED_RR)
;
printf("SCHED_RR 1.\n");
if(policy==SCHED_FIFO)
printf("SCHED_FIFO.\n");
for(int i=1;i<=10;i++)
{
for(int j=1;j<4000000;j++){
}
printf("Threadfunc3.\n");
}
printf("pthreadfunc3 EXIT.\n");
}
int main()
{
int i;
i=getuid();
if(i==0)
printf("the current user is root.\n");
else
printf("the current user is not root.\n");
pthread_t ppid1,ppid2,ppid3;
struct sched_param param;
pthread_attr_t attr1,attr2,attr3;
pthread_attr_init(&attr2);
pthread_attr_init(&attr1);
pthread_attr_init(&attr3);
param.sched_priority=51;
pthread_attr_setschedpolicy(&attr3,SCHED_RR);
pthread_attr_setschedparam(&attr3,¶m);
pthread_attr_setinheritsched(&attr3,PTHREAD_EXPLICIT_SCHED);
param.sched_priority=22;
pthread_attr_setschedpolicy(&attr2,SCHED_RR);
pthread_attr_setschedparam(&attr2,¶m);
pthread_attr_setinheritsched(&attr2,PTHREAD_EXPLICIT_SCHED);
pthread_create(&ppid3,&attr1,(void*)threadfunc3,NULL);
pthread_create(&ppid2,&attr2,(void*)threadfunc2,NULL);
pthread_create(&ppid1,&attr3,(void*)threadfunc1,NULL);
pthread_join(ppid3,NULL);
pthread_join(ppid2,NULL);
pthread_join(ppid1,NULL);
pthread_attr_destroy(&attr3);
pthread_attr_destroy(&attr2);
pthread_attr_destroy(&attr1);
return 0;
}