#include #include #include #include #include #include #define TRUE 1 #define FALSE 0 #define PRINT_CTIME 0 void ptimer_handler(int signal) { time_t now; if(signal == SIGPROF) { #if PRINT_CTIME now = time(NULL); printf("get sig prof alarm:%s\n",ctime(&now)); #else printf("get sig prof alarm\n"); #endif } } void vtimer_handler(int signal) { time_t now; if(signal == SIGVTALRM) { #if PRINT_CTIME now = time(NULL); printf("get sig vt alarm:%s\n",ctime(&now)); #else printf("get sig vt alarm\n"); #endif } } void timer_handler(int signal) { time_t now; if(signal == SIGALRM) { #if PRINT_CTIME now = time(NULL); printf("get sig alarm:%s\n",ctime(&now)); #else printf("get sig alarm\n"); #endif } } int risoo_prof_alarm(struct timeval *data) { struct itimerval timer,old; memcpy(&timer.it_value,data,sizeof(struct timeval)); timer.it_interval.tv_sec = 0; timer.it_interval.tv_usec = 0; if(setitimer(ITIMER_PROF,&timer,&old) < 0) { perror("setitimer() "); return FALSE; } return TRUE; } int risoo_vt_alarm(struct timeval *data) { struct itimerval timer,old; memcpy(&timer.it_value,data,sizeof(struct timeval)); timer.it_interval.tv_sec = 0; timer.it_interval.tv_usec = 0; if(setitimer(ITIMER_VIRTUAL,&timer,&old) < 0) { perror("setitimer() "); return FALSE; } return TRUE; } int risoo_real_alarm(struct timeval *data) { struct itimerval timer,old; memcpy(&timer.it_value,data,sizeof(struct timeval)); timer.it_interval.tv_sec = 0; timer.it_interval.tv_usec = 0; if(setitimer(ITIMER_REAL,&timer,&old) < 0) { perror("setitimer() "); return FALSE; } return TRUE; } int main(void) { struct timeval timer; signal(SIGVTALRM,vtimer_handler); signal(SIGPROF,ptimer_handler); signal(SIGALRM,timer_handler); timer.tv_sec = 0; timer.tv_usec = 500000; if(!risoo_vt_alarm(&timer)) { printf("set vt alarm fialed\n"); return 0; } printf("set vt alarm ok\n"); if(!risoo_prof_alarm(&timer)) { printf("set prof alarm fialed\n"); return 0; } printf("set prof alarm ok\n"); if(!risoo_real_alarm(&timer)) { printf("set real alarm fialed\n"); return 0; } printf("set real alarm ok\n"); // alarm(1); while(1); return 0; }