📄 asynctest.c
字号:
/* * asynctest.c: use async notification to read stdin * * Tested with 2.0 on the x86, Sparc and Alpha * Tested with 2.2 on the x86 * (but pipes don't generate async signals, tty's do) * * Actually, it should run with any Unix */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <signal.h>#include <fcntl.h>int gotdata=0;void sighandler(int signo){ if (signo==SIGIO) gotdata++; return;}char buffer[4096];int main(int argc, char **argv){ int count; struct sigaction action; memset(&action, 0, sizeof(action)); action.sa_handler = sighandler; action.sa_flags = 0; sigaction(SIGIO, &action, NULL); fcntl(STDIN_FILENO, F_SETOWN, getpid()); fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) | FASYNC); while(1) { /* this only returns if a signal arrives */ sleep(86400); /* one day */ if (!gotdata) continue; count=read(0, buffer, 4096); /* buggy: if avail data is more than 4kbytes... */ write(1,buffer,count); gotdata=0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -