title: "Bad file descriptor" error when reading from pipe as stdin
body: <p>I fork and set up a command like this:</p>
  
  <pre><code>pid_t pid;
  pid = fork();
  if (pid == 0)
  {   // I am the child
      freopen("/input_pipe","r",stdin);
      freopen("/output_pip","w",stdout);
      execl("/bin/sh", "sh", "-c", command, (char *)NULL); // using execv is probably faster
      // Should never get here
      perror("execl");
      exit(1);
  }
  </code></pre>
  
  <p>The /input_pipe has been created and filled with data before the processs is forked.</p>
  
  <p>This works in almost all cases absolutely fine. The command reads (with <code>read()</code>) from its stdin and gets the data that was written to the pipe by another process.</p>
  
  <p>But sometimes the command cannot read from its stdin stream and gets the "Bad file descriptor" error when trying to do so.</p>
  
  <p>What could cause this error?</p>
  
  <p>Edit: I have changed the freopen parts to this:</p>
  
  <pre><code>    pipe_in = open(pipename_in, O_RDONLY);
      pipe_out = open(pipename_out, O_WRONLY);
  
      dup2(pipe_in, 0);
      dup2(pipe_out, 1);
  </code></pre>
  
  <p>I will test this for a couple of days though as the error was only appearing very seldom.</p>