IO复用之 select
Posted nanqiang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IO复用之 select相关的知识,希望对你有一定的参考价值。
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <assert.h> int main () { int keyboard; int ret,i; char c; fd_set readfd; struct timeval timeout; keyboard = open("/dev/tty",O_RDONLY | O_NONBLOCK); assert(keyboard>0); while(1) { timeout.tv_sec=5; timeout.tv_usec=0; FD_ZERO(&readfd); FD_SET(keyboard,&readfd); ret=select(keyboard+1,&readfd,NULL,NULL,&timeout); //select error when ret = -1 if (ret == -1) perror("select error"); //data coming when ret>0 else if (ret) { if(FD_ISSET(keyboard,&readfd)) { i=read(keyboard,&c,1); if(‘ ‘==c) continue; printf("the input is %c ",c); if (‘q‘==c) break; } } //time out when ret = 0 else if (ret == 0) printf("time out "); } }
以上是关于IO复用之 select的主要内容,如果未能解决你的问题,请参考以下文章