Linux setitimer函数测试代码2
2014-03-11 08:45
246 查看
/*首先提示用户输入上限值,然后thread1和thread2循环检测定时器定时时间,直至60秒后,thread3醒来,输出定时器超过设定上限值的次数,并结束程序。*/
#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<pthread.h>
#include<time.h>
#include<signal.h>
#include<unistd.h>
#include<semaphore.h>
int upper_limit;
int times=0;
void change_semaphore();
struct sem_t
{
long int sem1;
};
struct sem_t sem; // define a struct of semaphore
struct itimerval timer1; //define a struct which will be used by function setitimer
void thread1()
{
int i;
long int time;
struct timeval time_start;
struct timeval time_end;
while(1)
{
gettimeofday((struct timeval*)&time_start,NULL); //get start time and save it to struct
sem_wait((struct sem_t*)&sem); //block the present thread till semaphore is bigger than zero
gettimeofday((struct timeval*)&time_end,NULL); //get end time and save it to struct
time=time_end.tv_usec-time_start.tv_usec; //calculate the running time of timer
if(time>upper_limit)
{
times++;
printf("the running time of timer is %d us\n",time);
}
}
}
void thread2()
{
setitimer(ITIMER_REAL,&timer1,NULL); //turn on timer
signal(SIGALRM,change_semaphore); //register signal SIGALRM to function change_semaphore
while(1)
{
pause();//waiting for signal SIGALRM
}
}
void thread3()
{
sleep(60);
printf("Setitimer test is over !\n");
printf("The timer ran over upper limit for %d times\n",times);
}
void change_semaphore()
{
sem_post((struct sem_t*)&sem); //semaphore adds one
}
int main()
{
int i;
pthread_t id1,id2,id3;
int ret1,ret2,ret3;
printf("Please input upper limit(us)\n");
scanf("%d",&upper_limit); //input upper limit
timer1.it_value.tv_usec=10000; //initialize timer
timer1.it_interval.tv_usec=10000; //initialize timer
sem_init((struct sem_t*)&sem,0,0); //initialize semaphore
ret2=pthread_create(&id2,NULL,(void*)thread2,NULL); //create thread 2
if(ret2==0)
printf("Created thread 2 successfully !\n");
else
printf("Failed to create thread 2 !\n");
ret1=pthread_create(&id1,NULL,(void*)thread1,NULL); //create thread 1
if(ret1==0)
printf("Created thread 1 successfully !\n");
else
printf("Failed to create thread 1 !\n");
ret3=pthread_create(&id3,NULL,(void*)thread3,NULL); //create thread 3
if(ret3==0)
printf("Created thread 3 successfully !\n");
else
printf("Failed to create thread 3 !\n");
pthread_join(id3,NULL); //wait for thread 3 to finish
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<pthread.h>
#include<time.h>
#include<signal.h>
#include<unistd.h>
#include<semaphore.h>
int upper_limit;
int times=0;
void change_semaphore();
struct sem_t
{
long int sem1;
};
struct sem_t sem; // define a struct of semaphore
struct itimerval timer1; //define a struct which will be used by function setitimer
void thread1()
{
int i;
long int time;
struct timeval time_start;
struct timeval time_end;
while(1)
{
gettimeofday((struct timeval*)&time_start,NULL); //get start time and save it to struct
sem_wait((struct sem_t*)&sem); //block the present thread till semaphore is bigger than zero
gettimeofday((struct timeval*)&time_end,NULL); //get end time and save it to struct
time=time_end.tv_usec-time_start.tv_usec; //calculate the running time of timer
if(time>upper_limit)
{
times++;
printf("the running time of timer is %d us\n",time);
}
}
}
void thread2()
{
setitimer(ITIMER_REAL,&timer1,NULL); //turn on timer
signal(SIGALRM,change_semaphore); //register signal SIGALRM to function change_semaphore
while(1)
{
pause();//waiting for signal SIGALRM
}
}
void thread3()
{
sleep(60);
printf("Setitimer test is over !\n");
printf("The timer ran over upper limit for %d times\n",times);
}
void change_semaphore()
{
sem_post((struct sem_t*)&sem); //semaphore adds one
}
int main()
{
int i;
pthread_t id1,id2,id3;
int ret1,ret2,ret3;
printf("Please input upper limit(us)\n");
scanf("%d",&upper_limit); //input upper limit
timer1.it_value.tv_usec=10000; //initialize timer
timer1.it_interval.tv_usec=10000; //initialize timer
sem_init((struct sem_t*)&sem,0,0); //initialize semaphore
ret2=pthread_create(&id2,NULL,(void*)thread2,NULL); //create thread 2
if(ret2==0)
printf("Created thread 2 successfully !\n");
else
printf("Failed to create thread 2 !\n");
ret1=pthread_create(&id1,NULL,(void*)thread1,NULL); //create thread 1
if(ret1==0)
printf("Created thread 1 successfully !\n");
else
printf("Failed to create thread 1 !\n");
ret3=pthread_create(&id3,NULL,(void*)thread3,NULL); //create thread 3
if(ret3==0)
printf("Created thread 3 successfully !\n");
else
printf("Failed to create thread 3 !\n");
pthread_join(id3,NULL); //wait for thread 3 to finish
return 0;
}
相关文章推荐
- 生成垃圾文件和测试代码执行时间
- MongoDB之Java测试代码(DAO层)
- 采用Purify(商业内存溢出检测器)以及GPL工具Valgrind(http://developer.kde.org/~sewardj/)测试了MySQL代码。
- 高效编程——C++测试代码运行时间方法
- Tensorflow MNIST 数据集测试代码入门
- [Android 测试] 代码覆盖率之 手工、自动、结合appium、合并ec
- XSS跨站测试代码大全
- Twisted网络编程必备(1)注:测试版本Twisted-10.1.0.winxp32-py2.6,对于原代码略有修改
- 【测试随笔】代码着色测试
- 【代码片段】jQuery测试更多元素集合
- 我的编程学习日志(4)-- 一种简单的测试代码的方法(freopen)
- Webpack 2 视频教程 010 - 配置 ESLint 实现代码规范自动测试 (下)
- c++代码测试-std::string元素
- 一段测试try...catch运行时间的代码
- js防刷新的倒计时代码(未测试)
- 你只喜欢写代码,不喜欢测试代码?大兄弟,你的想法很危险啊!
- Python常用辅助安全测试6个代码例子
- MSN病毒原理及测试代码
- 我的Exchange测试代码
- skia windows 测试代码和注意项