linux 间隔时间中断测试
该程序可以设置等待某段时间后发生中断,然后再按照要求在固定的时间又触发中断
/**
* alarm2.c 带间隔时间中断测试
*/
#include<stdio.h>
#include<sys/time.h>
#include<signal.h>
#define TIME 500
#define VTIME 10 //star time
#define ITIME 1000 //interval time
#define MSG "----ring----"
#define COUNTS 5
void ring();
void set_ring_time(int,int);
int main(int ac,char *av[])
{
signal(SIGALRM,ring);
set_ring_time(VTIME,ITIME);
while(1)
pause(); //等待中断到达
return 0;
}
void ring()
{
static int counts=COUNTS;
printf(" %s\n",MSG);
if(--counts<=0)
exit(1);
}
void set_ring_time(int vtime,int itime)
{
struct itimerval it_time;
long v_sec=vtime/1000;
long v_usec=vtime%1000;
long i_sec=itime/1000;
long i_usec=itime%1000;
it_time.it_interval.tv_sec=i_sec;
it_time.it_interval.tv_usec=i_usec;
it_time.it_value.tv_sec=v_sec;
it_time.it_value.tv_usec=v_usec;
setitimer(ITIMER_REAL,&it_time,NULL);
}
* alarm2.c 带间隔时间中断测试
*/
#include<stdio.h>
#include<sys/time.h>
#include<signal.h>
#define TIME 500
#define VTIME 10 //star time
#define ITIME 1000 //interval time
#define MSG "----ring----"
#define COUNTS 5
void ring();
void set_ring_time(int,int);
int main(int ac,char *av[])
{
signal(SIGALRM,ring);
set_ring_time(VTIME,ITIME);
while(1)
pause(); //等待中断到达
return 0;
}
void ring()
{
static int counts=COUNTS;
printf(" %s\n",MSG);
if(--counts<=0)
exit(1);
}
void set_ring_time(int vtime,int itime)
{
struct itimerval it_time;
long v_sec=vtime/1000;
long v_usec=vtime%1000;
long i_sec=itime/1000;
long i_usec=itime%1000;
it_time.it_interval.tv_sec=i_sec;
it_time.it_interval.tv_usec=i_usec;
it_time.it_value.tv_sec=v_sec;
it_time.it_value.tv_usec=v_usec;
setitimer(ITIMER_REAL,&it_time,NULL);
}