首页 > 编程语言 > 详细

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

时间:2018-07-01 17:34:35      阅读:411      评论:0      收藏:0      [点我收藏+]

源码如下:

#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

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

原文:https://www.cnblogs.com/S-ong/p/9250485.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!