ECE 329 Handout1/3
Using setitimer()
The setitimerfunction is prototyped in <sys/time.h> as
int setitimer(int whichtimer, struct itimerval *new, struct itimerval *old);
and is used to set a timer specified by whichtimer according to new. The whichtimer argument can have a value of ITIMER_REAL,ITIMER_VIRTUAL, or ITIMER_PROF. The signals ITIMER_REAL and ITIMER_PROF which raise the signals SIGALRM and SIGPROF, respectively.
If old is not a null pointer, setitimer returns information about any previous unexpired timer of the same kind in the structure it points to.
The return value is 0 on success and -1 on failure. The following errno error conditions are defined for this function:
EINVAL- The timer interval was too large.
The structure struct itimerval is used to specify when a timer should expire. It contains the following members:
struct timeval it_interval- This is the interval between successive timer interrupts. If zero, the alarm will only be sent once.
struct timeval it_value- This is the interval to the first timer interrupt. If zero, the alarm is disabled.
Thestruct timevalstructure represents a calendar time. It has the following members:
long int tv_sec- This represents the number of seconds since the epoch. It is equivalent to a normal time_t value.
long int tv_usec - This is the fractional second value, represented as the number of microseconds.
Some times struct timeval values are user for time intervals. Then the tv_sec member is the number of seconds in the interval, and tv_usec is the number of additional microseconds.
Example1:
#include <stdio.h>
#include <sys/time.h>
#include <signal.h>
void main(void)
{
struct itimerval TimerVal;
/* Find out what is the system clock granularity. */
TimerVal.it_interval.tv_sec = 0;
TimerVal.it_interval.tv_usec = 1;
TimerVal.it_value.tv_sec = 0;
TimerVal.it_value.tv_usec = 0;
setitimer (ITIMER_REAL, &TimerVal, 0);
printf("Interval Seconds: %d\n",
TimerVal.it_interval.tv_sec);
printf("Interval uSeconds: %d\n\n",
TimerVal.it_interval.tv_usec);
setitimer (ITIMER_REAL, 0, &TimerVal);
printf ("System clock granularity\n");
printf("Interval Seconds: %d\n",
TimerVal.it_interval.tv_sec);
printf("Interval uSeconds: %d\n",
TimerVal.it_interval.tv_usec);
}
Output
Interval Seconds: 0;
Interval uSeconds: 1;
System clock granularity
Interval Seconds: 0
Interval uSeconds: 54925 (Intel) 10000 (Sun)
Example2:
#include <stdio.h>
#include <sys/time.h>
#include <signal.h>
typedef void (*fptr)();
void Catcher(int i)
{
printf("Timer Expired!\n");
getchar();
}
void main(void)
{ int i, j;
struct itimerval Timer;
signal(SIGALRM, (fptr)Catcher);
/* Set the timer up to be repeating, so that once
* it expires, itstarts another cycle.
*/
Timer.it_interval.tv_sec = 2;
Timer.it_interval.tv_usec = 0;
/* Set the time to expiration to interval seconds.
* The timer resolution is milliseconds.
*/
Timer.it_value.tv_sec = 1;
Timer.it_value.tv_usec = 0;
setitimer (ITIMER_REAL, &Timer, NULL);
for (i=0; i<10000; i++)
for (j=0; j<10000; j++)
printf("%d-%d\n", i, j);
}