title: How do I get the status of a child process from a parent, i.e. has it stopped, continued, or exited? For Linux, C lang
body: <p>This is a C language program in Linux (ubuntu). I've been trying to figure out how to get the status of a child process from a parent.</p>
  <p>I've written a simple child process that counts to 25 over 25 seconds and outputs a tick count to standard output.   In the parent process I <em>1&gt; stop the child process</em> for a couple of seconds.  <em>2&gt; continue</em> it for a couple of seconds and then <em>3&gt; kill the child process.</em>    I want to get the status of the child process, for which I've been using the <strong>waitpid()</strong> function.  However, I find that if I used the flags:</p>
  <p><strong>waitCondition = WUNTRACED | WCONTINUED</strong></p>
  <p>it does return the &quot;stopped&quot; status, but it hangs when it's in the continue state.</p>
  <p>Conversely, if I set the flags to:</p>
  <p><strong>waitCondition= WUNTRACED | WCONTINUED | WNOHANG</strong></p>
  <p>The stop status is not registered, but the continue status is registered by waitpid().</p>
  <p><strong>I'm trying to get the parent to recognize the state when it has stopped, continued or exited.</strong></p>
  <p>I have the code below. Does anyone have any ideas for this?  Thanks!</p>
  <pre><code>int waiting4pid()(pid_t processID)
  {   
      int waitCondition = WUNTRACED | WCONTINUED;
      int currentState;
  
      while (waitpid(processID,&amp;currentState,waitCondition) &gt; 0){
  
          if(WIFCONTINUED(currentState)){
              printf(&quot;\n currentState = continued!\n&quot;);
          }
          if(WIFSIGNALED(currentState)){
              printf(&quot;\n currentState = signaled!\n&quot;);            
          }
          if(WIFSTOPPED(currentState)){
              printf(&quot;\n currentState = stopped!\n&quot;);
          }
          
      }
  }
  
  void sigTest()
  { 
      pid_t processID;
  
      processID = fork();
      if(processID ==0) { // child
         // tmp/loop is an application that counts to 25 in 25 seconds and then exits.
         execlp(&quot;tmp/loop&quot;, &quot;tmp/loop&quot;, NULL);
      }else{ 
          sleep(2);
          printf(&quot;\n Stop!&quot;);
          kill(processID, SIGSTOP);
          waiting4pid()(processID);
          
          sleep(2);
          printf(&quot;\n Continue!&quot;);      
          kill(processID,SIGCONT);
          waiting4pid()(processID);
          
          sleep(2);
          printf(&quot;\n Kill!&quot;); 
          kill(processID, SIGKILL);
          waiting4pid()(processID);
      }
  }
  
  void main() 
  {    
      sigTest();
  }
  </code></pre>