title: Pipe, Fork, and Exec - Two Way Communication Between Parent and Child Process
body: <p>An assignment in my Operating Systems class requires me to build a binary process tree by recursively calling exec on the same program.  The goal is to split some arbitrary task into separate processes.  The parent should communicate with the children, and the children with the parent only via unnamed pipes.  The idea is that the parent sends each child half of the work and this continues recursively until a base case is met where the length of the string being passed to each child is &lt;= 2.  The child then processes this data and sends the results back to the parent via pipes.</p>
  
  <p>To get a better understanding of how two way communication works with pipes in c I created the following simple program before moving on to the actual assignment.  The parent never reads the data from the child process though.  I'm expecting the output...</p>
  
  <blockquote>
    <p>in parent | message received: test</p>
  </blockquote>
  
  <p>Instead, when I print I get...</p>
  
  <blockquote>
    <p>in parent | message received:</p>
  </blockquote>
  
  <p>It seems that buff is empty and not reading from the child process.  Can someone please explain what I'm doing wrong and/or the standard way of</p>
  
  <ol>
  <li>writing to exec'd child from parent</li>
  <li>reading from parent in exec'd child</li>
  <li>writing back to parent from exec'd child</li>
  <li>reading from exec'd child in parent</li>
  </ol>
  
  <p>I am required to use exec(), pipe(), fork().  Thank you.</p>
  
  <pre><code>/**
   * *********************************
   * two_way_pipes.c
   * *********************************
   */
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;math.h&gt;
  #include &lt;sys/time.h&gt; 
  #include &lt;sys/types.h&gt; 
  #include &lt;unistd.h&gt;
  
  #define PARENT_READ read_pipe[0]
  #define PARENT_WRITE write_pipe[1]
  #define CHILD_WRITE read_pipe[1]
  #define CHILD_READ  write_pipe[0]
  
  #define DEBUGGING 1
  
  int main(int argc, char **argv) {
      char buff[5];
  
      // in the child process that was exec'd on the orginal call to two_way_pipes
      if(argc == 2) {
          read(STDIN_FILENO, buff, 4); // this should read "test" from stdin
          buff[4] = '\0';
          fprintf(stdout, "%s\n", buff); // this should right "test" to stdout and be read by the parent process
      // int the root process, the original call to two_way_pipes with no args
      } else {
          int pid;
          int read_pipe[2];
          int write_pipe[2];
  
          pipe(read_pipe);
          pipe(write_pipe);
  
          pid = fork();
  
          // parent process
          if(pid &gt; 0) {
              close(CHILD_READ);
              close(CHILD_WRITE);
  
              write(PARENT_WRITE, "test", 4); // attempting to write this to the child
  
              struct timeval tv;
              fd_set readfds;
              tv.tv_sec = 10;
              tv.tv_usec = 0;
              FD_ZERO(&amp;readfds);
              FD_SET(PARENT_READ, &amp;readfds);
              select(PARENT_READ + 1, &amp;readfds, NULL, NULL, &amp;tv);
  
              if(FD_ISSET(PARENT_READ, &amp;readfds)) {
                  read(PARENT_READ, buff, 4); // should read "test" which was written by the child to stdout
                  buff[4] = '\0';
                  close(PARENT_READ);
                  close(PARENT_WRITE);
                  fprintf(stderr, "in parent | message received: %s\n", buff);  // "test" is not in buff
              }
  
          // child process
          } else if(pid == 0) {
              close(PARENT_READ);
              close(PARENT_WRITE);
  
              dup2(CHILD_READ, STDIN_FILENO);
              dup2(CHILD_WRITE, STDOUT_FILENO);
              close(CHILD_READ);
              close(CHILD_WRITE);
  
              char *argv2[] = {"some random arg to make sure that argc == 2 in the child", NULL};
              execvp("two_way_pipes", argv2);
              _exit(0);
          // error forking child process
          } else {
              fprintf(stderr, "error forking the child\n");
          }
      }
  }
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>Based on Jonathon's answer I modified the arg2 array being passed into execvp to...</p>
  
  <pre><code>char *argv2[] = {"two_way_pipes", "1", NULL};
  execvp("two_way_pipes", argv2);
  </code></pre>
  
  <p>This didn't fix the issue.  The parent still wasn't able to read "test" back from the client.  However, in response to Jonathon's answer and William's comment I started tweaking my exec call and for some reason changing it to the line show below worked.</p>
  
  <pre><code>execl("two_way_pipes", "two_way_pipes", "1", NULL);
  </code></pre>
  
  <p>I'll gladly accept any answers explaining why the execvp call wouldn't work but the execl call did.</p>