title: Wait for popen script to terminate in C
body: <p>Is that possible with C to wait until the popen subprocess is finished?</p>
  
  <p>I have a script launched by a C daemon which will connect to a wifi network when an external signal is triggered.</p>
  
  <p>Here is my code for now :</p>
  
  <pre><code>********************* Wait for external event here ***********************
  
  if((fp = popen(CONNECTION_SCRIPT, "r")) == NULL)
  {
      SYSLOG_ERR("[Error] Impossible to open pipe to execute  %s\n", CONNECTION_SCRIPT);
      exit(EXIT_FAILURE);
  }
  
  while(fgets(scriptBuffer, SCRIPT_BUF_SIZE, fp) != NULL)
  {
      SYSLOG("[%s] %s", CONNECTION_SCRIPT, scriptBuffer);
  }
  
  if(pclose(fp))
  {
      SYSLOG_ERR("[Error] Error with %s execution \n", CONNECTION_SCRIPT);
      exit(EXIT_FAILURE);
  }
  </code></pre>
  
  <p>However, I'm not waiting for the subprocess to finish so when I'm closing the pipe, the ressource is busy because the script is stil writting in.
  Is there a way to wait for the end of the connection script before reading in the pipe and closing it?</p>
  
  <p>I know I could do it by forking the process and waiting for the child to terminate but it's quite a lot of modifications just to synchronize threads</p>