Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 769619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:21:58+00:00 2026-05-14T18:21:58+00:00

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

  • 0

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:


/*
** 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 

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:21:59+00:00Added an answer on May 14, 2026 at 6:21 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand UML diagram describing Decorator Pattern at link below http://www.dofactory.com/Patterns/PatternDecorator.aspx
I was trying to understand something with pointers, so I wrote this code: #include
trying to understand http and headers i was playing around with telnet to send
I am trying to understand Http/Https a little better and possibly what headers I'm
I am trying to understand how software like App-V and sandboxie ( http://www.sandboxie.com/ )
I'm trying to understand 2 different lines of code below. My javascript is weak,
Trying to understand an fft (Fast Fourier Transform) routine I'm using (stealing)(recycling) Input is
After trying to understand why client code is not rendered in a page (injected
I've been trying to understand how Ruby blocks work, and to do that I've
I've been trying to understand Process.MainWindowHandle . According to MSDN; The main window is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.