The select function tests for read/write/error conditions on file descriptors. Here's an example that waits 10 seconds for you to enter some data via STDIN, before giving up:
(its from the faq)
Code:
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
int main(void)
{
fd_set f;
int rc;
char buf[128];
struct timeval t;
t.tv_sec = 10;
t.tv_usec = 0;
/* set descriptor */
FD_ZERO(&f);
FD_SET(STDIN_FILENO, &f);
rc = select(STDIN_FILENO + 1, &f, NULL, NULL, &t);
if (rc == -1)
{
/* select( ) error */
}
else if (rc == 0)
{
/* no input available */
}
else if (FD_ISSET(STDIN_FILENO, &f))
{
rc = read(STDIN_FILENO, buf, 127);
if (rc == -1)
{
/* read( ) error */
}
else
{
buf[rc] = 0;
printf("%s", buf);
}
}
}