码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-07-01 17:47:55      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:att   sign   elf   while   ++   TE   exp   clu   create   

源码如下:

#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

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

标签:att   sign   elf   while   ++   TE   exp   clu   create   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!