I want to start a timer to count a period of about 10 second, how should I implement the method in time.h ?
Printable View
I want to start a timer to count a period of about 10 second, how should I implement the method in time.h ?
You can use setitimer function on Unix to set a timer interval. After the specified timer expires it generates SIGALRM which can be captured using signal() function to call a signal handler function.
setitimer takes argument of type "struct itimerval" found int sys/time.h and has following structure
Code:struct itimerval {
struct timeval it_interval; /* next value */
struct timeval it_value; /* current value */
};
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
A sample code would be
Program outputCode:#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#define INTERVAL 10
void alarm_wakeup (int i)
{
signal(SIGALRM,alarm_wakeup);
printf("%d sec up partner, Wakeup!!!\n",INTERVAL);
exit(0);
}
int main ()
{
struct itimerval tout_val;
tout_val.it_interval.tv_sec = 0;
tout_val.it_interval.tv_usec = 0;
tout_val.it_value.tv_sec = INTERVAL; /* set timer for "INTERVAL (10) seconds */
tout_val.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &tout_val,0);
signal(SIGALRM,alarm_wakeup); /* set the Alarm signal capture */
while (1)
{
;
}
return 0;
}
Quote:
[root@olinux1 tools]# gcc -o sample sample.c
[root@olinux1 tools]# ./sample
10 sec up partner, Wakeup!!!
[root@olinux1 tools]#
another example with setitimer() to implement a repetitive timer. Signal handler for SIGALRM signal, resets the timer.
Program outputCode:#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#define INTERVAL 5
int howmany = 0;
void alarm_wakeup (int i)
{
struct itimerval tout_val;
signal(SIGALRM,alarm_wakeup);
howmany += INTERVAL;
printf("\n%d sec up partner, Wakeup!!!\n",howmany);
tout_val.it_interval.tv_sec = 0;
tout_val.it_interval.tv_usec = 0;
tout_val.it_value.tv_sec = INTERVAL; /* 10 seconds timer */
tout_val.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &tout_val,0);
}
void exit_func (int i)
{
signal(SIGINT,exit_func);
printf("\nBye Bye!!!\n");
exit(0);
}
int main ()
{
struct itimerval tout_val;
tout_val.it_interval.tv_sec = 0;
tout_val.it_interval.tv_usec = 0;
tout_val.it_value.tv_sec = INTERVAL; /* 10 seconds timer */
tout_val.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &tout_val,0);
signal(SIGALRM,alarm_wakeup); /* set the Alarm signal capture */
signal(SIGINT,exit_func);
while (1)
{
//printf("!");
}
return 0;
}
Quote:
[root@olinux1 tools]# ./sample
5 sec up partner, Wakeup!!!
10 sec up partner, Wakeup!!!
15 sec up partner, Wakeup!!!
20 sec up partner, Wakeup!!!
^C
Bye Bye!!!
[root@olinux1 tools]#
Thx a lot!
I want to know how to pass parameter to the function "alarm_wakup" from main?
Well, unfortunately you can't. Work with global variables. They are bad programming choice. But you have a very little choice here.
then what does the (int i) mean ?
void alarm_wakeup (int i)
Frankly, I do not really know for sure. But signal handler prototype varies from system to system. On my linux system it has prototype as
The function argument here to the signal handler function is usually the signal number that invoked the handler. Anyway as far I know, its unused.Code:#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
I have also seen signal handler prototyped as
Code:void (*func)();
How can I use this kind of timer to run a file every few minutes/seconds ?
I tried crontab but seems like is not working right (even though it was supposed to).
I want to run a file.pl using perl every 5 minutes or so. How should I change this timer to what I need ?
This informmation was very useful thanks for this post
1) Put "sleep 300" in the perl file and run it infinitelyQuote:
Originally Posted by Corne
2) Use shell script and use that
These two are easier than C program. C++ - Never reinvent the wheel
Hi nkhambal ,
actually i have tried to run the 1st code u posted, it works fine.
but i get these warnings:
s1.c: In function ‘alarm_wakeup’:
s1.c:35: warning: incompatible implicit declaration of built-in function ‘exit’
how can i get rid of them?
(i am working with Ubuntu8.04)
thanks
hello togehter,
I also looking for a timer for my program. The example from "nkhambal" works fine. But is it possible to recognize a method from a class as signal handler instead a function??
I have a solution like this, but i would prefer a normal method as signal handler!
Code:.h:
class CommandProcessor
{
public:
// implements singleton pattern
static CommandProcessor& getInstance();
void timerExpired();
};
.cpp:
//normal functions:
void sigHandler(int i)
{
CommandProcessor::getInstance().timerExpired();
}
void configureTimer()
{
struct itimerval timer;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 0;
timer.it_value.tv_sec = 0; ;//INTERVAL; /* set timer for "INTERVAL (10) seconds */
timer.it_value.tv_usec = 1;
setitimer(ITIMER_REAL, &timer,0);
signal(SIGALRM, sigHandler);
}
// method from commandProcessor class
void CommandProcessor::timerExpired()
{
//....
}
Thx for help,
Cheers Bisk
Hi Raedbenz,
I realize this question was posed in 2008 and it is 2013 already, but the answer to your question is very simple. You probably already have it figured out..
Add the line:
Code:#include <stdlib.h>