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

源码如下:

#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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值