|
Starting a Timer To Count a Period
What command for starting a timer to count a period of about 10 second,
how should I implement the method in time.h ?
Solution:
Setitimer Function To Set Timer Interval
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:
#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:
Quote:
[root@ounix1 tools]# gcc -o sample sample.c
[root@ounix1 tools]# ./sample
10 sec up partner, Wakeup!!!
[root@ounix1 tools]#
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;
}
Have a Unix Problem
Do you have
a UNIX Question?
Unix Books :-
UNIX
Programming, Certification, System Administration, Performance Tuning Reference
Books
Return to : - Unix
System Administration Hints and Tips
(c) www.gotothings.com All material on this site is
Copyright.
Every effort is made to ensure the content integrity.
Information used on this site is at your own risk.
All product names are trademarks of their respective
companies.
The site www.gotothings.com is in no way affiliated
with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.
|