实时线程和非实时线程测试

源码如下:

#include<stdio.h>
#include <unistd.h>
#include<pthread.h>

void* thread1(void* arg)
{
unsigned int i,j;

int policy;
struct sched_param param;
pthread_getschedparam( pthread_self( ) , &policy, &param) ;
if ( policy == SCHED_OTHER)
printf ( "SCHED_OTHER/n" ) ;
if ( policy == SCHED_RR)
printf ( "SCHED_RR /n" ) ;
if ( policy == SCHED_FIFO)
printf ( "SCHED_FIFO/n" ) ;

while(1)
{
for(i=0;i<2;i++)
{
printf("i am thread1,my tid is %lu\n",pthread_self());
for(j=0;j<50000;j++);
}
printf("\n");
sleep(5);
}

}

void* thread2(void* arg)
{
unsigned int i,j;

int policy;
struct sched_param param;

pthread_getschedparam( pthread_self( ) , &policy, &param) ;
if ( policy == SCHED_OTHER)
printf ( "SCHED_OTHER/n" ) ;
if ( policy == SCHED_RR)
printf ( "SCHED_RR /n" ) ;
if ( policy == SCHED_FIFO)
printf ( "SCHED_FIFO/n" ) ;
while(1)
{
for(i=0;i<2;i++)
{
printf("i am thread2,my tid is %lu\n",pthread_self());
for(j=0;j<50000;j++);
}
printf("\n");
sleep(5);
}
}


void* thread3(void* arg)
{
unsigned int i,j;

int policy;
struct sched_param param;
pthread_getschedparam( pthread_self( ) , &policy, &param) ;
if ( policy == SCHED_OTHER)
printf ( "SCHED_OTHER/n" ) ;
if ( policy == SCHED_RR)
printf ( "SCHED_RR /n" ) ;
if ( policy == SCHED_FIFO)
printf ( "SCHED_FIFO/n" ) ;

while(1)
{
for(i=0;i<2;i++)
{
printf("i am thread3,my tid is %lu\n",pthread_self());
for(j=0;j<50000;j++);
}
printf("\n");
sleep(5);
}
}

int main()
{
pthread_t tid1,tid2,tid3;
pthread_attr_t attr1, attr2;
struct sched_param param;


pthread_attr_init ( &attr1) ;
pthread_attr_init ( &attr2) ;



param.sched_priority = 51;
pthread_attr_setschedpolicy ( &attr1, SCHED_RR) ;
pthread_attr_setschedparam ( &attr1, &param) ;
pthread_attr_setinheritsched ( &attr1, PTHREAD_EXPLICIT_SCHED ) ;

param.sched_priority = 21;
pthread_attr_setschedpolicy ( &attr2, SCHED_RR) ;
pthread_attr_setschedparam ( &attr2, &param) ;
pthread_attr_setinheritsched ( &attr2, PTHREAD_EXPLICIT_SCHED ) ;

pthread_create(&tid3,NULL,thread3,NULL);
pthread_create(&tid1,&attr1,thread1,NULL);
pthread_create(&tid2,&attr2,thread2,NULL);

pthread_join(tid3,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);


pthread_attr_destroy ( &attr1) ;
pthread_attr_destroy ( &attr2) ;

return 0;
}

 

结果如下:

# ./show_file
SCHED_RR /ni am thread1,my tid is 32771
i am thread1,my tid is 32771

SCHED_RR /ni am thread2,my tid is 49156
i am thread2,my tid is 49156

SCHED_OTHER/ni am thread3,my tid is 16386
i am thread3,my tid is 16386

i am thread1,my tid is 32771
i am thread1,my tid is 32771

i am thread2,my tid is 49156
i am thread2,my tid is 49156

i am thread3,my tid is 16386
i am thread3,my tid is 16386

i am thread1,my tid is 32771
i am thread1,my tid is 32771

i am thread2,my tid is 49156
i am thread2,my tid is 49156

i am thread3,my tid is 16386
i am thread3,my tid is 16386

i am thread1,my tid is 32771
i am thread1,my tid is 32771

i am thread2,my tid is 49156
i am thread2,my tid is 49156

i am thread3,my tid is 16386
i am thread3,my tid is 16386

i am thread1,my tid is 32771
i am thread1,my tid is 32771

i am thread2,my tid is 49156
i am thread2,my tid is 49156

i am thread3,my tid is 16386
i am thread3,my tid is 16386

i am thread1,my tid is 32771
i am thread1,my tid is 32771

i am thread2,my tid is 49156
i am thread2,my tid is 49156

i am thread3,my tid is 16386
i am thread3,my tid is 16386

i am thread1,my tid is 32771
i am thread1,my tid is 32771

i am thread2,my tid is 49156
i am thread2,my tid is 49156

i am thread3,my tid is 16386
i am thread3,my tid is 16386

posted @ 2018-07-01 16:35  帆随我向  阅读(404)  评论(0编辑  收藏  举报