开发者

Linux C select: piping echo to input works, but reading from keyboard doesn't?

开发者 https://www.devze.com 2022-12-29 15:11 出处:网络
I am trying to understand http://beej.us/guide/bgnet/examples/select.c (included below for reference). I am doing this:

I am trying to understand http://beej.us/guide/bgnet/examples/select.c (included below for reference). I am doing this:

:~$ cat /etc/issue

Ubuntu 10.04 LTS \n \l
:~$ gcc --version
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3

:~$ wget http://beej.us/guide/bgnet/examples/select.c
:~$ gcc select.c -o select

:~$ echo "ff" | ./select 
A key was pressed!

:~$ ./select 
TYPINGTYTimed out.

So, the select program apparently recognizes an echo piping into it as input; but it will not recognize keypresses on the terminal. Why is this? Can some sort of redirection be used (I guess, similar to how screen could 'redirect' keyboard input to a serial session) so that actual key presses in terminal are recognized?

Thanks, Cheers!

select.c:

开发者_StackOverflow /* ** select.c -- a select() demo */

#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#define STDIN 0  // file descriptor for standard input

int main(void)
{
 struct timeval tv;
 fd_set readfds;

 tv.tv_sec = 2;
 tv.tv_usec = 500000;

 FD_ZERO(&readfds);
 FD_SET(STDIN, &readfds);

 // don't care about writefds and exceptfds:
 select(STDIN+1, &readfds, NULL, NULL, &tv);

 if (FD_ISSET(STDIN, &readfds))
  printf("A key was pressed!\n");
 else
  printf("Timed out.\n");

 return 0;
}

Edit: see answer; thus all we need is to press enter:

:~$ ./select 

A key was pressed!

or we can turn off buffered input with stty raw (and turn it back on with stty cooked):

:~ stty raw
:~ ./select 
                                            dA key was pressed!
                                                               :~ stty cooked 


Standard input is a buffered stream. The select() call will not be able to detect that there is input available until the newline is hit at the end of the input. You can't use select() like this to read individual keystrokes.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号