日期:2014-05-16 浏览次数:20695 次
#include<signal.h> #include<stdio.h> #include<stdlib.h> #include<time.h> #include<unistd.h> void handler(int sig,siginfo_t*,void*){ if(sig==SIGRTMIN){printf("Caught %d\n",sig);} else if(sig==SIGRTMIN+1){ } } timer_t createTimer(int sig, double secs){ sigevent sev; timer_t timerid; sev.sigev_notify=SIGEV_SIGNAL; sev.sigev_signo =sig; sev.sigev_value.sival_ptr=&timerid; if(timer_create(CLOCK_REALTIME,&sev,&timerid)==-1)return 0; long freq_nanosecs=(long)secs*1000000000; itimerspec its; its.it_value.tv_sec =freq_nanosecs/1000000000; its.it_value.tv_nsec =freq_nanosecs%1000000000; its.it_interval.tv_sec =its.it_value.tv_sec; its.it_interval.tv_nsec=its.it_value.tv_nsec; if(timer_settime(timerid,0,&its,NULL)==-1)return 0; return timerid; } int main(void){ struct sigaction sa; sa.sa_flags=SA_SIGINFO; sa.sa_sigaction=handler; sigemptyset(&sa.sa_mask); if(sigaction(SIGRTMIN,&sa,NULL)==-1)return 1; createTimer(SIGRTMIN,2); createTimer(SIGRTMIN+1,5); int left=10; do{left=sleep(left);}while(left>0); return 0; }