#include
// for printf()
#include // for pause()
#include // for signal()
#include // for memset()
#include // struct itimeral. setitimer()
#include
void printMsg(int);
int main() {
// Get system call result to determine successful or failed
int res = 0;
pthread_t a;
// Register printMsg to SIGALRM
signal(SIGALRM, pthread_create(&a,NULL,(void*)printMsg,NULL));
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) {
printf("%s","Hello World!!\n");
}
阅读(1750) | 评论(1) | 转发(1) |