-
September 8th, 2005, 03:47 AM
#1
How to use timer in Unix ?
I want to start a timer to count a period of about 10 second, how should I implement the method in time.h ?
-
September 8th, 2005, 05:38 AM
#2
Re: How to use timer in Unix ?
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
Code:
#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;
}
Program output
[root@olinux1 tools]# gcc -o sample sample.c
[root@olinux1 tools]# ./sample
10 sec up partner, Wakeup!!!
[root@olinux1 tools]#
-
September 8th, 2005, 05:55 AM
#3
Re: How to use timer in Unix ?
another example with setitimer() to implement a repetitive timer. Signal handler for SIGALRM signal, resets the timer.
Code:
#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;
}
Program output
[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]#
-
September 8th, 2005, 09:23 PM
#4
Re: How to use timer in Unix ?
-
September 8th, 2005, 10:11 PM
#5
Re: How to use timer in Unix ?
I want to know how to pass parameter to the function "alarm_wakup" from main?
-
September 8th, 2005, 10:20 PM
#6
Re: How to use timer in Unix ?
Well, unfortunately you can't. Work with global variables. They are bad programming choice. But you have a very little choice here.
-
September 9th, 2005, 12:13 AM
#7
Re: How to use timer in Unix ?
then what does the (int i) mean ?
void alarm_wakeup (int i)
-
September 9th, 2005, 12:57 AM
#8
Re: How to use timer in Unix ?
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
Code:
#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
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.
I have also seen signal handler prototyped as
-
September 8th, 2007, 07:24 PM
#9
Re: How to use timer in Unix ?
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 ?
-
April 18th, 2008, 06:36 AM
#10
Re: How to use timer in Unix ?
This informmation was very useful thanks for this post
-
April 18th, 2008, 05:10 PM
#11
Re: How to use timer in Unix ?
Originally Posted by Corne
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 ?
1) Put "sleep 300" in the perl file and run it infinitely
2) Use shell script and use that
These two are easier than C program. C++ - Never reinvent the wheel
-
July 12th, 2008, 06:40 AM
#12
Re: How to use timer in Unix ?
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
Last edited by raedbenz; July 12th, 2008 at 07:40 AM.
Reason: new results
-
July 21st, 2009, 03:32 PM
#13
Re: How to use timer in Unix ?
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
-
April 9th, 2013, 09:27 PM
#14
Re: How to use timer in Unix ?
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>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|