将标准输入keyboard作为fd加入到fd_set中去。
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
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);
printf("fd_set size = %d\n", sizeof(fd_set));
FD_ZERO(&readfd);
while(1)
{
timeout.tv_sec=2;
timeout.tv_usec=0;
FD_SET(keyboard,&readfd);
ret=select(keyboard+1,&readfd,NULL,NULL,&timeout);
//ret=select(keyboard+1,&readfd,NULL,NULL,NULL);
printf("FD_ISEET before = %d, tv_sec =%d, tv_usec =%d", FD_ISSET(keyboard, &readfd),
timeout.tv_sec, timeout.tv_usec);
if(FD_ISSET(keyboard,&readfd))
{
read(keyboard,&c,1);
if('\n'== c)
continue;
printf("hehethe input is %c\n",c);
if ('q'==c)
break;
}
}
}
阅读(2093) | 评论(0) | 转发(0) |