setitimer函数测试代码1
2014-03-11 08:44
316 查看
/*thread1先获取系统时间,然后阻塞,紧接着thread2调用setitimer开启定时器,定时10ms,时间到后收到信号SIGALRM,转到信号处理函数change_semaphore改变信号量,信号量变为1以后thread1停止阻塞,输出两次时间差*/
#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>
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()
{
long int time;
struct timeval time_start;
struct timeval time_end;
gettimeofday((struct timeval*)&time_start,NULL); //get start time and save it to struct
sem_wait(&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>20000)
{
printf("the running time of timer is %d us\n",time);
}
exit(0);
}
void thread2()
{
setitimer(ITIMER_REAL,&timer1,NULL); //turn on timer
signal(SIGALRM,change_semaphore); //register signal SIGALRM to function change_semaphore
pause();//waiting for signal SIGALRM
exit(0);
}
void change_semaphore()
{
sem_post(&sem); //semaphore adds one
}
int main()
{
pthread_t id1,id2;
int ret1,ret2;
timer1.it_value.tv_usec=10000; //initialize timer
sem_init(&sem,0,0); //initialize semaphore
ret1=pthread_create(&id1,NULL,(void*)thread1,NULL); //create thread 1
ret2=pthread_create(&id2,NULL,(void*)thread2,NULL); //create thread 2
pthread_join(id1,NULL); //waiting for thread 1 to finish
pthread_join(id2,NULL); //waiting for thread 2 to finish
return 0;
}
# shell程序
#!/bin/bash
for ((i=0;i<10000;i++));
do
/root/program/setitimer_test2;
done
#this shell program is used for executing setitimer_test2 for 10000 times
#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>
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()
{
long int time;
struct timeval time_start;
struct timeval time_end;
gettimeofday((struct timeval*)&time_start,NULL); //get start time and save it to struct
sem_wait(&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>20000)
{
printf("the running time of timer is %d us\n",time);
}
exit(0);
}
void thread2()
{
setitimer(ITIMER_REAL,&timer1,NULL); //turn on timer
signal(SIGALRM,change_semaphore); //register signal SIGALRM to function change_semaphore
pause();//waiting for signal SIGALRM
exit(0);
}
void change_semaphore()
{
sem_post(&sem); //semaphore adds one
}
int main()
{
pthread_t id1,id2;
int ret1,ret2;
timer1.it_value.tv_usec=10000; //initialize timer
sem_init(&sem,0,0); //initialize semaphore
ret1=pthread_create(&id1,NULL,(void*)thread1,NULL); //create thread 1
ret2=pthread_create(&id2,NULL,(void*)thread2,NULL); //create thread 2
pthread_join(id1,NULL); //waiting for thread 1 to finish
pthread_join(id2,NULL); //waiting for thread 2 to finish
return 0;
}
# shell程序
#!/bin/bash
for ((i=0;i<10000;i++));
do
/root/program/setitimer_test2;
done
#this shell program is used for executing setitimer_test2 for 10000 times
相关文章推荐
- 生成垃圾文件和测试代码执行时间
- 如何改变maven项目的pom文件中默认的主代码目录 以及默认的测试代码目录?
- 本地测试接口代码
- 采用Purify(商业内存溢出检测器)以及GPL工具Valgrind(http://developer.kde.org/~sewardj/)测试了MySQL代码。
- 【代码片段】jQuery测试层次选择器
- Twisted网络编程必备(1)注:测试版本Twisted-10.1.0.winxp32-py2.6,对于原代码略有修改
- 【代码片段】jQuery测试兄弟元素集合
- python学习第十一章测试代码部分课后练习自己尝试的代码
- jacoco统计自动化测试代码覆盖率
- 人脸识别引擎SeetaFaceEngine中Identification模块使用的测试代码
- 实现一个无锁的Stack,并写一段测试代码(多线程访问),证明这个Stack是线程安全的。给出程序以及运行的截图。
- JavaScript验证手机号码代码简单代码实例(未测试)
- sql 之 c++测试代码
- c++代码测试-std::string元素
- 测试代码的运行时间(经典加总结)
- 打印的一段代码测试研究
- TDD:代码可测试设计
- 算法测试平台搭建常用代码备份
- Python常用辅助安全测试6个代码例子