select 函数用法 :
/*************************************************************************
> File Name: myserver.c
> Author: ahuang1900
> Mail: ahuang1900@qq.com
> Created Time: 2014年10月08日 星期三 20时09分45秒
************************************************************************/
#include<stdio.h>
#include<sys/time.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);
printf("keyboard = %d\n", keyboard);
assert(keyboard);
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);
printf("select的返回值:%d\n", ret);
if (FD_ISSET(keyboard, &readfd))
{
i = read(keyboard, &c, 1);
if ('\n' == c)
continue;
printf("ahuang1900 input is %c\n", c);
if ('q' == c)
break;
}
}
return 0;
}