-
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]#
-
April 18th, 2008, 06:36 AM
#4
Re: How to use timer in Unix ?
This informmation was very useful thanks for this post
-
September 8th, 2005, 09:23 PM
#5
Re: How to use timer in Unix ?
-
September 8th, 2005, 10:11 PM
#6
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
#7
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
#8
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
#9
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
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
|