title: Communicate with child process stdout/stdin
body: <p>I am trying to communicate with a process (that itself writes to stdin and stdout to interact in a terminal with a user) and read it's stdin and write to it's stdout in C. </p>
  
  <p>Hence I try to substitute a shell user programmatically. A methapohrical example: Imagine I want to use VIM in C for some reason. Then I also need to write commands (stdout) and read stuff from the editor (stdin).</p>
  
  <p>Initially I thought this might be a trivial task, but it seems like there's no standard approach. <code>int system(const char *command);</code> just executes a command and sets the commands stdin/stdout to the one of the calling process. </p>
  
  <p>Because this leads nowhere, I looked at <code>FILE *popen(const char *command, const char *type);</code> but the manual pages state that:</p>
  
  <blockquote>
    <p>Since a pipe is by definition unidirectional, the type argument may specify only reading or writing, not both; the resulting stream is correspondingly read-only or write-only.</p>
  </blockquote>
  
  <p>and its implication:</p>
  
  <blockquote>
    <p>The  return value from popen() is a normal standard I/O stream in all respects save that it must be closed with pclose() rather than fclose(3).  <strong>Writing to such a stream writes to the standard input
           of the command;</strong> the <strong>command's standard output</strong> is the <strong>same as that of the process that called popen()</strong>, unless this is altered by the command itself.  Conversely, <strong>reading from a "popened" stream reads
           the command's standard output</strong>, and the <strong>command's standard input is the same as that of the process that called popen()</strong>.</p>
  </blockquote>
  
  <p>Hence it wouldn't be completely impossible to use popen(), but it appears to me very inelegant, because I would have to parse the stdout of the calling process (the code that called popen()) in order to parse data sent from the popened command (when using popen type 'w'). </p>
  
  <p>Conversely, when popen is called with type 'r', I would need to write to the calling's process stdin, in order to write data to the popened command. It's not even clear to me whether both these processes receive the same data in the stdin in this case...</p>
  
  <p><strong>I just need to control stdin and stdout of a program.</strong> I mean can't there be a function like:</p>
  
  <pre><code>stdin_of_process, stdout_of_process = real_popen("/path/to/bin", "rw")
  // write some data to the process stdin
  write("hello", stdin_of_process)
  // read the response of the process
  read(stdout_of_process)
  </code></pre>
  
  <p>So my <strong>first question</strong>: What is the best way to implement the upper functionality?</p>
  
  <p>Currently I am trying the following approach to communicate with another process:</p>
  
  <ol>
  <li>Set up two pipes with <code>int pipe(int fildes[2]);</code>. One pipe to read the stdout of the process, the other pipe to write to the stdin of the process.</li>
  <li>Fork.</li>
  <li>Execute the process that I want to communicate with in the forked child process using <code>int execvp(const char *file, char *const argv[]);</code>.</li>
  <li>Communicate with the child using the two pipes in the original process.</li>
  </ol>
  
  <p>That's easy said bot not so trivially implemented (At least for me). I oddly managed to do so in one case, but when I tried to understand what I am doing with a simpler example, I fail. Here is my current problem:</p>
  
  <p>I have two programs. The first just writes a incremented number every 100 ms to it's stdout:</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;time.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  void sleepMs(uint32_t ms) {
      struct timespec ts;
      ts.tv_sec = 0 + (ms / 1000);
      ts.tv_nsec = 1000 * 1000 * (ms % 1000);
      nanosleep(&amp;ts, NULL);
  }
  
  int main(int argc, char *argv[]) {
      long int cnt = 0;
      char buf[0x10] = {0};
  
      while (1) {
          sleepMs(100);
          sprintf(buf, "%ld\n", ++cnt);
          if (write(STDOUT_FILENO, buf, strlen(buf)) == -1)
              perror("write");
      }
  }
  </code></pre>
  
  <p>Now the second program is supposed to read the stdout of the first program (Please keep in my mind that I eventually want to read AND write with a process, so a technical correct solution to use popen() for the upper use case might be right in this specific case, because I simplified my experiments to just capture the stdout of the bottom program). I expect from the bottom program that it reads whatever data the upper program writes to stdout. But it does not read anything. Where could be the reason? (second question).</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;time.h&gt;
  
  void sleepMs(uint32_t ms) {
      struct timespec ts;
      ts.tv_sec = 0 + (ms / 1000);
      ts.tv_nsec = 1000 * 1000 * (ms % 1000);
      nanosleep(&amp;ts, NULL);
  }
  
  int main() {
      int pipe_fds[2];
      int n;
      char buf[0x100] = {0};
      pid_t pid;
  
      pipe(pipe_fds);
  
      char *cmd[] = {"/path/to/program/above", NULL};
  
      if ((pid = fork()) == 0) { /* child */
          dup2(pipe_fds[1], 1); // set stdout of the process to the write end of the pipe
          execvp(cmd[0], cmd); // execute the program.
          fflush(stdout);
          perror(cmd[0]); // only reached in case of error
          exit(0);
      } else if (pid == -1) { /* failed */
          perror("fork");
          exit(1);
      } else { /* parent */
  
          while (1) {
              sleepMs(500); // Wait a bit to let the child program run a little
              printf("Trying to read\n");
              if ((n = read(pipe_fds[0], buf, 0x100)) &gt;= 0) { // Try to read stdout of the child process from the read end of the pipe
                  buf[n] = 0; /* terminate the string */
                  fprintf(stderr, "Got: %s", buf); // this should print "1 2 3 4 5 6 7 8 9 10 ..."
              } else {
                  fprintf(stderr, "read failed\n");
                  perror("read");
              }
          }
      }
  }
  </code></pre>