All pastes #445501 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-04-17 22:14 UTC
rendered paste body
/*
	Example of using standard UNIX interval timers. These act like
	alarm clocks that signal a process when a specifed interval
	is complete. They can be one-shot, or set to go off periodically

	Interval Timers require the use of itimerval structure objects.

	struct  itimerval {
      struct  timeval it_interval;
      struct  timeval it_value;
    };

	We have already seen the timeval struct used in gettimeofday().
	It has a second and a microsecond component. 

	"it_value" contains the time until the next timer is set to expire
	
	"it_interval" contains the values which will be used to reset the
	timer once it expires. If set to 0, the timer will set for
	one-off execution, otherwise you set the values to the period
	you wish the timer to expire
*/

#include <signal.h>
#include <stdio.h>
#include <string.h> /* for memset */
#include <sys/time.h>
#include <unistd.h>

/* 1. Implement a signal handler function */
void handler(int signum)
{
	printf("Testing.");
}

/* main program */

int main(void)
{
	struct sigaction sa;	/* for signal handler settings */
	struct itimerval timer;	/* for timer setting */
	

	
	/* 2. set up the signal handler here */

	memset(&sa, NULL, 0);
	sa.sa_handler = &handler;
	sigaction(SIGALRM, &sa, NULL);

	/* 3. Set up the periodic timer here*/

	timer.it_interval.tv_usec = 0;
	timer.it_interval.tv_sec = 10;
	timer.it_value.tv_usec = 0;
	timer.it_value.tv_sec = 10;

	/* set up the timer to run */
	setitimer(ITIMER_REAL, &timer, NULL); /* NULL can be replaced with another timer object that
										    will store the old timer settings */

	/* Do some other processing. Nothing extra for exercise */
	while(1)
	{

	}

	return 0;
}