title: fork() and exec() Two Child Processes
body: <p>I am calling fork() twice to create two child processes. I want child process A to do an exec() call and child process B to also do an exec() call. The problem I am having with the given code is that after the first exec() from child process A, the next fork() does not seem to occur and the program exits. I think that it has to do with how exec() overlays the parent process. What I want to accomplish is to call exec() from each of the child processes created by fork().</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;sys/msg.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;iostream&gt;
  
  int main() {
  
  pid_t cpid_a, cpid_b;
  
  cpid_a = fork();
  if(cpid_a &lt; 0) {
      std::cout &lt;&lt; "Fork failed." &lt;&lt; '\n';
      return 1;
  }
  else if(cpid_a == 0) { // code for child process A
      execlp("/bin/ls", "ls", NULL);
  
      cpid_b = fork();
      if(cpid_b &lt; 0) {
          std::cout &lt;&lt; "Fork failed." &lt;&lt; '\n';
          return 1;
      }
      else if(cpid_b == 0) { // code for child process B
          execlp("/bin/ls", "ls", NULL);
      }
  }
  else { // code for parent process
      while(wait(NULL) != -1);
  }
  return 0;
  }
  </code></pre>