execvp fork : wait for stdout
<p>I'm coding a simple linux shell in C.</p>

<p>Sometimes when using fork and then executing a NON-BLOCKING command - my next <code>printf</code> disappear. I'm guessing it is because the child process is writing to stdout.</p>

<p>If I use <code>waitpid</code> there is no problem - because my next printf will only print AFTER the child process got terminated. Sometimes the user will want to execute a non-blocking command - and then I wont use <code>waitpid</code> - and then my next <code>printf</code> will disappear.</p>

<p>If I use <code>sleep(1)</code> it also solves the problem. But I was wondering if there is a more elegant way of achieving that.</p>

<pre><code>int main( int argc, char *argv[], char *env[] )
{
   pid_t child_pid;
   int status;

   if((child_pid = fork()) &lt; 0 )
   {
      perror("fork failure");
      exit(1);
   }
   if(child_pid == 0)
   {  
        printf("\nChild: I am a new-born process!\n\n");
        char *sd[] = {"ls", "-l", NULL};
        execvp(sd[0], sd);
   }
   else
   {
        printf("THIS LINE SOMETIMES DISAPPEAR");
   }
   return 0;
}
</code></pre>