When I compile and run the following code with the command g++ -std=c++11 <filename> and run with ./a.out and enter in some text to see what the select call will return (it should return 1 because when I enter text, text is then available to be read in). Somehow the text I enter escapes as a bash command itself. Could someone explain why this happens?
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
int input_timeout (int filedes, unsigned int seconds) {
  fd_set set;
  struct timeval timeout;
  // Initialize the file descriptor set.
  FD_ZERO (&set);
  FD_SET (filedes, &set);
  // Initialize the timeout data structure.
  timeout.tv_sec = seconds;
  timeout.tv_usec = 0;
  // select returns 0 if timeout, 1 if input available, -1 if error. 
  return select (FD_SETSIZE,
                 &set, NULL, NULL,
                 &timeout);
}
int main () {
  fprintf (stderr, "select returned %d.\n", input_timeout (STDIN_FILENO, 1));
  return 0;
}
So an example of what I said above is in the output below
bash-3.2 $ ./a.out 
what
select returned 1.
bash-3.2$ what
and then waits indefinitely for the input to the what command
Aucun commentaire:
Enregistrer un commentaire