title: wait command wont wait for child process to finish c cpp c++
body: <p>I am trying to write a c++ program that creates a child process, runs a command and pipes the output back to the input of a command the parent is running.</p>
  
  <p>I have the parent execute the wait(NULL) or wait((void*)pid) command but it does not wait.</p>
  
  <p>here is the code:</p>
  
  <pre><code>#include &lt;string.h&gt;
  #include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;sys/wait.h&gt;
  using namespace std;
  
  int main(int argc, char * argv[])
  {    
          char* commands[strlen(argv[1])];
          char *command = NULL;
          command = strtok(argv[1],"|");
          int i = 0;
          while(command != NULL)
          {
                  commands[i] = command;
                  i++;
                  command = strtok(NULL,"|");
          }
  
          int numberOfCommands = i;
  
          pid_t pid;
          int pfd[2];
          char* prgname = NULL;
          if(pipe(pfd) == -1)
          {
                  perror("error on pipe call");
                  return(1);
          }
  
          for(int j = 0;j&lt;numberOfCommands;j++)
          {
                  cout&lt;&lt;commands[j]&lt;&lt;endl;
          }
  
          pid = fork();
          if(pid == 0){//child process
                  printf("Child: My PID = %d\n", getpid());
                  printf("Child: Running...\n");
                  close(pfd[0]); //close read end of pipe
                  dup2(pfd[1],1);//connect the pipes
                  close(pfd[1]);//close extra file descriptors
                  prgname = commands[0];//first command
                  cout&lt;&lt;"child starting command: "&lt;&lt;prgname&lt;&lt;endl;
                  execlp(prgname, prgname, 0);//Load the program
                  **printf("Child: Done sleeping, returning.\n");**
          }
          else
          {
                  printf("Parent: My PID = %d\n", getpid());
                  **wait((void*)pid); //also tried wait(NULL); same effect
                  printf("Parent: Running...\n");**
                  close(pfd[1]); //close the write end of the pipe
                  dup2(pfd[0],0);//connect the pipes
                  close(pfd[0]); //close extra file descriptor
                  prgname = commands[1];//now run the second command
                  cout&lt;&lt;"parent starting command: "&lt;&lt;prgname&lt;&lt;endl;
                  execlp(prgname, prgname, 0);//Load the programm
          }
          cout&lt;&lt;"all done"&lt;&lt;endl;
          return 0;
  }
  </code></pre>
  
  <p>Take not of the bolded lines. I would expect the parent process to wait at the wait() command and the child would print out "Child done sleeping..." and then finish and then the parent would print out "Parent: running..."</p>
  
  <p>What am I doing wrong!</p>
  
  <p>Thanks!</p>
  
  <p>Update: full output to program is:</p>
  
  <pre><code>dmegs
  more
  Child: My PID = 30070
  Child: Running...
  Parent: My PID = 30066
  Parent: Running...
  parent starting command: more
  child starting command: dmegs
  Child: Done sleeping, returning.
  all done
  </code></pre>