测试程序如下,创建了3个SCHED__FIFO的实时线程,线程优先级不同,按理说应该是高优先级的先运行,也就是thread3运行完之后thread2运行,thread2运行完之后thread1才运行,但是在嵌入式设备上运行的打印信息显示却不是这样的。
#include
#include
void FunThread1()
{
int i,j;
int policy;
int priority;
struct sched_param param;
sleep(1);
pthread_getschedparam(pthread_self(),&policy,¶m);
priority = param.sched_priority;
printf("priority %d, ",priority);
if(policy==SCHED_OTHER)
printf("SCHED_OTHER\n");
if(policy==SCHED_RR)
printf("SCHED_RR\n");
if(policy==SCHED_FIFO)
printf("SCHED_FIFO\n");
for(i=1;i <10;i++)
{
for(j=1;j <50000;j++)
{
}
printf("thread 1\n");
}
printf("Thread1 exit\n");
}
void FunThread2()
{
int i,j;
int policy;
int priority;
struct sched_param param;
sleep(1);
pthread_getschedparam(pthread_self(),&policy,¶m);
priority = param.sched_priority;
printf("priority %d, ",priority);
if(policy==SCHED_OTHER)
printf("SCHED_OTHER 2\n");
if(policy==SCHED_RR)
printf("SCHED_RR 2\n");
if(policy==SCHED_FIFO)
printf("SCHED_FIFO 2\n");
for(i=1;i <10;i++)
{
for(j=1;j <50000;j++)
{
}
printf("thread 2\n");
}
//sched_yield();
//sched_yield();
printf("Thread2 exit\n");
}
void FunThread3()
{
int i,j;
int policy;
int priority;
struct sched_param param;
sleep(1);
pthread_getschedparam(pthread_self(),&policy,¶m);
priority = param.sched_priority;
printf("priority %d, ",priority);
if(policy==SCHED_OTHER)
printf("SCHED_OTHER 3\n");
if(policy==SCHED_RR)
printf("SCHED_RR 3\n");
if(policy==SCHED_FIFO)
printf("SCHED_FIFO 3\n");
for(i=1;i <10;i++)
{
for(j=1;j <50000;j++)
{
}
printf("thread 3\n");
}
//sched_yield();
//sched_yield();
printf("Thread3 exit\n");
}
int main()
{
int i;
pthread_t ppid1,ppid2,ppid3;
struct sched_param param1,param2,param3;
pthread_attr_t attr1,attr2,attr3;
i=getuid();
if(i==0)
printf("The current user is root\n");
else
printf("The current user is not root\n");
pthread_attr_init(&attr1);
pthread_attr_init(&attr2);
pthread_attr_init(&attr3);
param1.sched_priority=1;
pthread_attr_setschedpolicy(&attr1,SCHED_FIFO);
pthread_attr_setschedparam(&attr1,¶m1);
pthread_attr_setinheritsched(&attr1,PTHREAD_EXPLICIT_SCHED); //新加,指定不继承父线程调度策略
param2.sched_priority=2;
pthread_attr_setschedpolicy(&attr2,SCHED_FIFO);
pthread_attr_setschedparam(&attr2,¶m2);
pthread_attr_setinheritsched(&attr2,PTHREAD_EXPLICIT_SCHED); //新加,指定不继承父线程调度策略
param3.sched_priority=3;
pthread_attr_setschedpolicy(&attr3,SCHED_FIFO);
pthread_attr_setschedparam(&attr3,¶m3);
pthread_attr_setinheritsched(&attr3,PTHREAD_EXPLICIT_SCHED); //新加,指定不继承父线程调度策略
pthread_create(&ppid1,&attr1,(void *)FunThread1,NULL);
pthread_create(&ppid2,&attr2,(void *)FunThread2,NULL);
pthread_create(&ppid3,&attr3,(void *)FunThread3,NULL);
pthread_join(ppid1,NULL);
pthread_join(ppid2,NULL);
pthread_join(ppid3,NULL);
pthread_attr_destroy(&attr1);
pthread_attr_destroy(&attr2);
pthread_attr_destroy(&attr3);
return 0;
}
嵌入式设备上的打印信息如下:
# ./sched
The current user is root
priority 2, SCHED_FIFO 2
priority 3, SCHED_FIFO 3
thread 2
thread 3
thread 2
thread 3
thread 2
thread 3
thread 2
thread 3
thread 2
thread 3
thread 2
thread 3
thread 2
thread 3
thread 2
thread 3
thread 2
Thread2 exit
thread 3
Thread3 exit
priority 1, SCHED_FIFO
thread 1
thread 1
thread 1
thread 1
thread 1
thread 1
thread 1
thread 1
thread 1
Thread1 exit
根据这个运行的打印信息,线程2和线程3怎么感觉是没有按照实时线程的调度方式执行,有点像公平调度呢?但是获取其调度方式也是SCHED-FIFO,想不通,求解?