总结:对三种线程调度方式进行测试。
1.SCHED_OTHER:分时调度策略,为默认方式,凡是采用本模式的线程,线程优先级会强制为0,通俗点将,工作在本模式下的线程,都在同一优先级下。所谓的“分时调度策略”,可以理解为线程执行一次后主动放弃CPU,线程调度一次,执行下一个线程。测试代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sched.h>
#include <assert.h>
void *Thread1(void* pp)
{
sleep(1);
int i,j;
int policy;
struct sched_param param;
pthread_getschedparam(pthread_self(),&policy,¶m);
if(policy == SCHED_OTHER) printf("SCHED_OTHER 1\n");
if(policy == SCHED_RR) printf("SCHED_RR 1 \n");
if(policy==SCHED_FIFO) printf("SCHED_FIFO 1\n");
//for(i=1;i<1000;i++)
while(1)
{
i++;
for(j=1;j<5000000;j++)
{
}
//printf("t1-%d", i);
printf("1");
if(i%100 == 0) printf("\n");
}
printf("Pthread 1 exit\n");
}
void *Thread2(void* pp)
{
sleep(1);
int i,j,m;
int policy;
struct sched_param param;
pthread_getschedparam(pthread_self(),&policy,¶m);
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<1000;i++)
while(1)
{
i++;
for(j=1;j<5000000;j++)
{
}
//printf("t2-%d", i);
printf("2");
if(i%100 == 0) printf("\n");
}
printf("Pthread 2 exit\n");
}
void *Thread3(void* pp)
{
sleep(1);
int i,j;
int policy;
struct sched_param param;
pthread_getschedparam(pthread_self(),&policy,¶m);
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<1000;i++)
while(1)
{
i++;
for(j=1;j<5000000;j++)
{
}
//printf("t3-%d", i);
printf("3");
if(i%100 == 0) printf("\n");
}
printf("Pthread 3 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(&attr1);
pthread_attr_init(&attr2);
pthread_attr_init(&attr3);
#if 0
param.sched_priority = 10;
pthread_attr_setschedpolicy(&attr1,SCHED_RR);
pthread_attr_setschedparam(&attr1,¶m);
pthread_attr_setinheritsched(&attr1,PTHREAD_EXPLICIT_SCHED);
param.sched_priority = 10;
pthread_attr_setschedpolicy(&attr2,SCHED_RR);
pthread_attr_setschedparam(&attr2,¶m);
pthread_attr_setinheritsched(&attr2,PTHREAD_EXPLICIT_SCHED);//要使优先级其作用必须要有这句话
param.sched_priority = 10;
pthread_attr_setschedpolicy(&attr3,SCHED_RR);
pthread_attr_setschedparam(&attr3,¶m);
pthread_attr_setinheritsched(&attr3,PTHREAD_EXPLICIT_SCHED);//要使优先级其作用必须要有这句话
#endif
pthread_create(&ppid1,&attr1,Thread1,NULL);
pthread_create(&ppid2,&attr2,Thread2,NULL);
pthread_create(&ppid3,&attr3,Thread3,NULL);
pthread_join(ppid1,NULL);
pthread_join(ppid2,NULL);
pthread_join(ppid3,NULL);
while(1) {
sleep(100);
printf("main loop\n");
}
pthread_attr_destroy(&attr1);
pthread_attr_destroy(&attr2);
pthread_attr_destroy(&attr3);
return 0;
}
测试结果如下,每个线程执行一次就进行一次线程调度。
2.SCHED_FIFO:实时调度策略,先到先服务(优先级和启动顺序,优先级高的先到,同一优先级下,先创建的线程先到)。
线程优先级不同:优先级高的线程永远占据CPU,不进行调度。除非主动放弃CPU(线程挂起、休眠等);
线程优先级相同:最先启动的线程永远占据CPU,不进行调度。除非主动放弃CPU(线程挂起、休眠等);
#if 1
param.sched_priority = 10;
pthread_attr_setschedpolicy(&attr1,SCHED_FIFO);
pthread_attr_setschedparam(&attr1,¶m);
pthread_attr_setinheritsched(&attr1,PTHREAD_EXPLICIT_SCHED);
param.sched_priority = 10;
pthread_attr_setschedpolicy(&attr2,SCHED_FIFO);
pthread_attr_setschedparam(&attr2,¶m);
pthread_attr_setinheritsched(&attr2,PTHREAD_EXPLICIT_SCHED);//要使优先级其作用必须要有这句话
param.sched_priority = 10;
pthread_attr_setschedpolicy(&attr3,SCHED_FIFO);
pthread_attr_setschedparam(&attr3,¶m);
pthread_attr_setinheritsched(&attr3,PTHREAD_EXPLICIT_SCHED);//要使优先级其作用必须要有这句话
#endif
3.SCHED_RR:实实时调度策略,时间片轮转。当线程的时间片用完,系统将重新分配时间片,并置于就绪队列尾。放在队列尾保证了所有具有相同优先级的RR任务的调度公平;
线程优先级不同:优先级高的线程永远占据CPU,不进行调度。除非主动放弃CPU(线程挂起、休眠等);
线程优先级相同:每个线程执行一段时间(时间片),时间到了之后。线程调度,执行一下个排队线程;
#if 1
param.sched_priority = 10;
pthread_attr_setschedpolicy(&attr1,SCHED_RR);
pthread_attr_setschedparam(&attr1,¶m);
pthread_attr_setinheritsched(&attr1,PTHREAD_EXPLICIT_SCHED);
param.sched_priority = 10;
pthread_attr_setschedpolicy(&attr2,SCHED_RR);
pthread_attr_setschedparam(&attr2,¶m);
pthread_attr_setinheritsched(&attr2,PTHREAD_EXPLICIT_SCHED);//要使优先级其作用必须要有这句话
param.sched_priority = 10;
pthread_attr_setschedpolicy(&attr3,SCHED_RR);
pthread_attr_setschedparam(&attr3,¶m);
pthread_attr_setinheritsched(&attr3,PTHREAD_EXPLICIT_SCHED);//要使优先级其作用必须要有这句话
#endif