23,121
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
int
main ()
{
int keyboard;
int ret, i, cnt;
char c;
fd_set readfd;
struct timeval timeout;
keyboard = open("/dev/tty", O_RDONLY | O_NONBLOCK);
assert(keyboard>0);
/*
while(1)
{
i = read(keyboard, &c, 1);
printf("%c \n", c);
}
*/
cnt = 0;
while(1) {
timeout.tv_sec = 1;
timeout.tv_usec = 0;
FD_ZERO(&readfd);
FD_SET(keyboard, &readfd);
printf("waiting for io ...\n");
ret = select(keyboard+1, &readfd, NULL, NULL, /*&timeout*/ NULL); //blocked without timeout
if(FD_ISSET(keyboard, &readfd)) {
printf("%d\n", ++cnt);
i = read(keyboard, &c, 1);
printf("read out:%d\n", i);
if('\n' == c)
continue;
//printf("the input is %c\n",c);
if ('q'==c)
break;
}
}
}