日期:2014-05-16 浏览次数:20844 次
#include<signal.h> #include<stdio.h> #include<stdlib.h> #include<time.h> #include<unistd.h> int count1=0,count2=0; void handler1(int sig,siginfo_t*,void*){ if(sig==SIGRTMIN){printf("Timer 1s:%d\n",++count1);} } void handler2(int sig,siginfo_t*,void*){ if(sig==SIGRTMIN+1){printf(" Timer 2s:%d\n",++count2);} for(int i=0;i<2000000000;++i){ int j=i+2; int k=j*j; } } 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=handler1; sigemptyset(&sa.sa_mask); if(sigaction(SIGRTMIN,&sa,NULL)==-1)return 1; sa.sa_sigaction=handler2; if(sigaction(SIGRTMIN+1,&sa,NULL)==-1)return 2; createTimer(SIGRTMIN, 1); createTimer(SIGRTMIN+1,2); int left=10; do{left=sleep(left);}while(left>0); return 0; }