title: Sleep not working in child process after the popen function call
body: <p>I have written a small C program which is creating a child process and then running a shell command using popen. I have created a signal handler to wait for the child process to end while the parent process just runs in an infinite loop. When I am calling 'sleep(5)' after the popen system call the sleep call does not work. </p>
  
  <p>Instead if I put the sleep call before the popen then it is working. Also if I remove the signal handler and just wait for the child process in the parent process then there is no problem(i.e. sleep runs properly anywhere).</p>
  
  <pre><code>//Assume all required header files and function prototypes added
  
  int main()
  {
      int pid,status;
      signal(SIGCHLD,reaper);
      pid = fork();
      if(pid == 0)
         runCommand();
      while(1);
  }
  
  void runCommand()
  {
      FILE *pipen;
      char *buffer;
  
      buffer = (char*) malloc(100);
  /*sleep #1*/    sleep(5);
      if ((pipen = popen("uname -a", "r")) == NULL) {
          perror("\nError during shell command execution: ");
          exit(0);
      }
  /*sleep #2*/    sleep(5);
      fgets(buffer, 100, pipen);
  /*sleep #3*/    sleep(5);
      printf("\n%s\n",buffer);
      exit(0);
  }
  
  void reaper(int signal)
  {
      int status;
      waitpid(-1,&amp;status,WNOHANG);
  }
  
  </code></pre>
  
  <p>When I am running the above program sleep #1 works and the process sleeps for the given time. I am getting to know this by observing the time it takes to print the result of the command (by the printf statement).
  But for sleep #2 and sleep #3 I expect the process to sleep but that does not seem to be happening as the result of the command is printed on the console instantaneously.</p>
  
  <p>The behavior can be observed by keeping only the sleep #2 or sleep #3 call, and removing the other two sleep calls.</p>
  
  <p>Does anyone know why this is happening?</p>