/* *
#include <sys/time.h>
int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue);
struct itimerval
{
struct timerval it_interval;
struct timerval it_value;
}
struct timeval
{
long tv_sec;
long tv_usec;
}
*/
#include <stdio.h> // for printf()
#include <unistd.h> // for pause()
#include <signal.h> // for signal()
#include <string.h> // for memset()
#include <sys/time.h> // struct itimeral,setitimer()
#include <time.h> // for localtime(),asctime(),time()
void printMsg(int);
int main()
{
// Get system call result to determine successful or failed
int res = 0;
// Register printMsg to SIGALRM
signal(SIGALRM, printMsg);
struct itimerval tick;
// Initialize struct
memset(&tick, 0, sizeof(tick));
// Timeout to run function first time
tick.it_value.tv_sec = 1; // sec
tick.it_value.tv_usec = 0; // micro sec.
// Interval time to run function
tick.it_interval.tv_sec = 1;
tick.it_interval.tv_usec = 0;
// Set timer, ITIMER_REAL : real-time to decrease timer,
// send SIGALRM when timeout
res = setitimer(ITIMER_REAL, &tick, NULL);
if (res)
{
printf("Set timer failed!!/n");
}
// Always sleep to catch SIGALRM signal
while(1)
{
pause();
}
return 0;
}
void printMsg(int num)
{
time_t now;
time(&now);
printf("Hello World!! %s ", asctime(localtime(&now)));
}
****************************************************************
Hello World!! Tue Jan 31 11:00:49 2012
Hello World!! Tue Jan 31 11:00:50 2012
Hello World!! Tue Jan 31 11:00:51 2012
Hello World!! Tue Jan 31 11:00:52 2012
Hello World!! Tue Jan 31 11:00:53 2012
Hello World!! Tue Jan 31 11:00:54 2012
Hello World!! Tue Jan 31 11:00:55 2012
Hello World!! Tue Jan 31 11:00:56 2012
Hello World!! Tue Jan 31 11:00:57 2012
Hello World!! Tue Jan 31 11:00:58 2012
Hello World!! Tue Jan 31 11:00:59 2012
Hello World!! Tue Jan 31 11:01:00 2012
Hello World!! Tue Jan 31 11:01:01 2012
Hello World!! Tue Jan 31 11:01:02 2012
Hello World!! Tue Jan 31 11:01:03 2012
Hello World!! Tue Jan 31 11:01:04 2012
Hello World!! Tue Jan 31 11:01:05 2012
Hello World!! Tue Jan 31 11:01:06 2012
Hello World!! Tue Jan 31 11:01:07 2012
Hello World!! Tue Jan 31 11:01:08 2012
Hello World!! Tue Jan 31 11:01:09 2012
Hello World!! Tue Jan 31 11:01:10 2012
Hello World!! Tue Jan 31 11:01:11 2012
Hello World!! Tue Jan 31 11:01:12 2012
Hello World!! Tue Jan 31 11:01:13 2012
Hello World!! Tue Jan 31 11:01:14 2012
Hello World!! Tue Jan 31 11:01:15 2012
Hello World!! Tue Jan 31 11:01:16 2012
Hello World!! Tue Jan 31 11:01:17 2012
Hello World!! Tue Jan 31 11:01:18 2012
Hello World!! Tue Jan 31 11:01:19 2012
Hello World!! Tue Jan 31 11:01:20 2012
Hello World!! Tue Jan 31 11:01:21 2012
Hello World!! Tue Jan 31 11:01:22 2012
Hello World!! Tue Jan 31 11:01:23 2012
Hello World!! Tue Jan 31 11:01:24 2012
Hello World!! Tue Jan 31 11:01:25 2012
Hello World!! Tue Jan 31 11:01:26 2012
Hello World!! Tue Jan 31 11:01:27 2012
Hello World!! Tue Jan 31 11:01:28 2012
Hello World!! Tue Jan 31 11:01:29 2012
Hello World!! Tue Jan 31 11:01:30 2012
Hello World!! Tue Jan 31 11:01:31 2012
Hello World!! Tue Jan 31 11:01:32 2012
Hello World!! Tue Jan 31 11:01:33 2012
Hello World!! Tue Jan 31 11:01:34 2012
Hello World!! Tue Jan 31 11:01:35 2012
Hello World!! Tue Jan 31 11:01:36 2012
Hello World!! Tue Jan 31 11:01:37 2012
Hello World!! Tue Jan 31 11:01:38 2012
Hello World!! Tue Jan 31 11:01:39 2012
Hello World!! Tue Jan 31 11:01:40 2012
Hello World!! Tue Jan 31 11:01:41 2012
Hello World!! Tue Jan 31 11:01:42 2012
Hello World!! Tue Jan 31 11:01:43 2012
Hello World!! Tue Jan 31 11:01:44 2012
Hello World!! Tue Jan 31 11:01:45 2012
Hello World!! Tue Jan 31 11:01:46 2012
Hello World!! Tue Jan 31 11:01:47 2012
Hello World!! Tue Jan 31 11:01:48 2012
Hello World!! Tue Jan 31 11:01:49 2012
Hello World!! Tue Jan 31 11:01:50 2012
Hello World!! Tue Jan 31 11:01:51 2012
Hello World!! Tue Jan 31 11:01:52 2012
Hello World!! Tue Jan 31 11:01:53 2012
Hello World!! Tue Jan 31 11:01:54 2012
Hello World!! Tue Jan 31 11:01:55 2012
Hello World!! Tue Jan 31 11:01:56 2012
Hello World!! Tue Jan 31 11:01:57 2012
Hello World!! Tue Jan 31 11:01:58 2012
Hello World!! Tue Jan 31 11:01:59 2012
********************************************************