当前位置: 技术问答>linux和unix
向高手请教,如何实现timer的嵌套?(菜鸟免进)
来源: 互联网 发布时间:2014-11-26
本文导语: #include #include #include #include struct sigaction oldact; struct itimerval olditimer; int timer_2=0; int timer_1=0; void timer1(int sig) { fprintf(stdout,"timer1=%dn",timer_1++); return; } void init_timer1() { struct sigaction act; struct itimerval itime...
#include
#include
#include
#include
struct sigaction oldact;
struct itimerval olditimer;
int timer_2=0;
int timer_1=0;
void timer1(int sig)
{
fprintf(stdout,"timer1=%dn",timer_1++);
return;
}
void init_timer1()
{
struct sigaction act;
struct itimerval itimer;
act.sa_handler = timer1;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
if(sigaction (SIGALRM, &act, &oldact) == -1)
{
fprintf(stdout,"init_timer1() : sigaction failedn");
return;
}
itimer.it_interval.tv_sec = 1;
itimer.it_interval.tv_usec = 0;
itimer.it_value = itimer.it_interval;
if (setitimer(ITIMER_REAL, &itimer, &olditimer) == -1)
{
fprintf(stdout,"init_timer1() : seritimer failedn");
return;
}
fprintf(stdout,"init_timer1() : successn");
}
void stop_timer1()
{
if(sigaction (SIGALRM, &oldact, NULL) == -1)
{
fprintf(stdout,"stop_timer1() : sigaction failedn");
return;
}
if (setitimer(ITIMER_REAL, &olditimer, NULL) == -1)
{
fprintf(stdout,"stop_timer1() : seritimer failedn");
return;
}
fprintf(stdout,"stop_timer1() : successn");
return;
}
void start_timer1()
{
init_timer1();
for(long i=0;i
#include
#include
#include
struct sigaction oldact;
struct itimerval olditimer;
int timer_2=0;
int timer_1=0;
void timer1(int sig)
{
fprintf(stdout,"timer1=%dn",timer_1++);
return;
}
void init_timer1()
{
struct sigaction act;
struct itimerval itimer;
act.sa_handler = timer1;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
if(sigaction (SIGALRM, &act, &oldact) == -1)
{
fprintf(stdout,"init_timer1() : sigaction failedn");
return;
}
itimer.it_interval.tv_sec = 1;
itimer.it_interval.tv_usec = 0;
itimer.it_value = itimer.it_interval;
if (setitimer(ITIMER_REAL, &itimer, &olditimer) == -1)
{
fprintf(stdout,"init_timer1() : seritimer failedn");
return;
}
fprintf(stdout,"init_timer1() : successn");
}
void stop_timer1()
{
if(sigaction (SIGALRM, &oldact, NULL) == -1)
{
fprintf(stdout,"stop_timer1() : sigaction failedn");
return;
}
if (setitimer(ITIMER_REAL, &olditimer, NULL) == -1)
{
fprintf(stdout,"stop_timer1() : seritimer failedn");
return;
}
fprintf(stdout,"stop_timer1() : successn");
return;
}
void start_timer1()
{
init_timer1();
for(long i=0;i