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

  • SEARCH
  • Home
  • 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'm trying to understand this: http://tcs.uj.edu.pl/~walczak/fibonacci.pdf I have a word created from a concatenation
I am trying to understand some example code on this web page: ( http://www.csharp-station.com/HowTo/HttpWebFetch.aspx
I'm trying to understand why the below is occuring: alt text http://img185.imageshack.us/img185/8301/capturexj.png It appears
I'm trying to understand the math on this raphael.js demo: http://raphaeljs.com/pie.js Checkout the sector
I am trying to understand UML diagram describing Decorator Pattern at link below http://www.dofactory.com/Patterns/PatternDecorator.aspx
Im trying to understand what a functor is, i found this tutorial/example: http://en.wikibooks.org/wiki/Haskell/Solutions/Applicative_Functors data
I am trying to understand how do I modify the example in this http://www.w3schools.com/php/php_ajax_database.asp
I am trying to understand the below use of a lambda expression. This code
I am trying to understand as why on this blog http://mysiddhashram.blogspot.com/ each time I
i am using this site as a resource http://www.perlmonks.org/?node_id=573138 I am trying to understand

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.