日期:2014-05-16 浏览次数:20675 次
#include <stdio.h> #include <unistd.h> #include <signal.h> void sigalrm_fn(int sig) { printf("alarm!\n"); alarm(2); return; } int main(void) { signal(SIGALRM, sigalrm_fn); alarm(1); while(1) pause(); }
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <time.h> #include <sys/time.h> int sec; void sigroutine(int signo){ switch (signo){ case SIGALRM: printf("Catch a signal -- SIGALRM \n"); signal(SIGALRM, sigroutine); break; case SIGVTALRM: printf("Catch a signal -- SIGVTALRM \n"); signal(SIGVTALRM, sigroutine); break; } return; } int main() { struct itimerval value, ovalue, value2; //(1) sec = 5; printf("process id is %d\n", getpid()); signal(SIGALRM, sigroutine); signal(SIGVTALRM, sigroutine); value.it_value.tv_sec = 1; value.it_value.tv_usec = 0; value.it_interval.tv_sec = 1; value.it_interval.tv_usec = 0; setitimer(ITIMER_REAL, &value, &ovalue); //(2) value2.it_value.tv_sec = 0; value2.it_value.tv_usec = 500000; value2.it_interval.tv_sec = 0; value2.it_interval.tv_usec = 500000; setitimer(ITIMER_VIRTUAL, &value2, &ovalue); for(;;) ; }
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<sys/time.h> #include<errno.h> #include<string.h> #include<unistd.h> #include<sys/types.h> #include<sys/select.h> int main(int argc, char **argv) { unsigned int nTimeTestSec = 0; unsigned int nTimeTest = 0; struct timeval tvBegin; struct timeval tvNow; int ret = 0; unsigned int nDelay = 0; struct timeval tv; int fd = 1; int i = 0; struct timespec req; unsigned int delay[20] = {500000, 100000, 50000, 10000, 1000, 900, 500, 100, 10, 1, 0}; int nReduce = 0; //误差 fprintf(stderr, "%19s%12s%12s%12s\n", "fuction", "time(usec)", "realtime", "reduce"); fprintf(stderr, "----------------------------------------------------\n"); for (i = 0; i < 20; i++) { if (delay[i] <= 0)