📄 test_itimer.c
字号:
#include <errno.h>#include <signal.h>#include <stdio.h>#include <time.h>#include <unistd.h>#include <fcntl.h>#define BILLION 1000000000L#define TIMER_MSG "Received Timer Interrupt\n"int fd_key;int keynew =0,keyold=0;/* ARGSUSED */static void interrupt(int signo, siginfo_t *info, void *context) { unsigned char i; #if 0 int errsave; errsave = errno; write(STDOUT_FILENO, TIMER_MSG, sizeof(TIMER_MSG) - 1); errno = errsave; #endif read(fd_key, &keynew, sizeof(keynew)); if(keynew!=0) { { if(keynew&0x01) printf("new key number: 1\n"); else if(keynew&0x02) printf("new key number: 2\n"); else if(keynew&0x04) printf("new key number: 3\n"); else if(keynew&0x08) printf("new key number: 4\n"); } keynew =0; } }static int setinterrupt() { struct sigaction act; act.sa_flags = SA_SIGINFO; act.sa_sigaction = interrupt; if ((sigemptyset(&act.sa_mask) == -1) || (sigaction(SIGALRM, &act, NULL) == -1)) return -1; return 0;}static int setperiodic(double sec) { timer_t timerid; struct itimerspec value; if (timer_create(CLOCK_REALTIME, NULL, &timerid) == -1) return -1; value.it_interval.tv_sec = (long)sec; value.it_interval.tv_nsec = (sec - value.it_interval.tv_sec)*BILLION; if (value.it_interval.tv_nsec >= BILLION) { value.it_interval.tv_sec++; value.it_interval.tv_nsec -= BILLION; } value.it_value = value.it_interval; return timer_settime(timerid, 0, &value, NULL);}int main(void) { int i; fd_key = open("/dev/key",O_RDONLY); if (fd_key < 0) { printf("open /dev/key error\n"); } if (setinterrupt() == -1) { perror("Failed to setup SIGALRM handler"); return 1; } if (setperiodic(0.1) == -1) { perror("Failed to setup periodic interrupt"); return 1; } for ( ; ; ) pause();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -