title: What does "wait and waitpid are always interrupted when a signal is caught" mean?
body: <p>From <em>APUE</em>:</p>
  
  <blockquote>
    <p><strong>To prevent applications from having to handle interrupted system
    calls, 4.2BSD introduced the automatic restarting of certain
    interrupted system calls.</strong> The system calls that were automatically
    restarted are <code>ioctl</code>, <code>read</code>, <code>readv</code>, <code>write</code>, <code>writev</code>, <code>wait</code>, and <code>waitpid</code>. As
    we’ve mentioned, the first five of these functions are interrupted by
    a signal only if they are operating on a slow device; <strong><code>wait</code> and <code>waitpid</code>
    are always interrupted when a signal is caught.</strong> Since this caused a
    problem for some applications that didn’t want the operation restarted
    if it was interrupted, 4.3BSD allowed the process to disable this
    feature on a per-signal basis.</p>
  </blockquote>
  
  <p>Does it mean that before <em>automatic restarting</em> was introduced, if a process catch a signal, <code>wait</code> and <code>waitpid</code> would immediately stop waiting and execute the subsequent code?</p>
  
  <p>For example:</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;signal.h&gt;
  
  void handler(int sig){}
  void handler2(int sig){}
  
  
  int main(){
      pid_t pid;
      int status;
      signal(SIGUSR1, handler);
      signal(SIGUSR2, handler2);    
  
      if((pid == fork()) &lt; 0){
          printf("fork error\n");
      }else{
          if(pid){
              //child
              //do something, needs several hours.
          }else{
              //parent
              waitpid(pid, &amp;status, 0);
              printf("Hello world\n");
          }
      }
      return 0;
  }
  </code></pre>
  
  <p>If not providing <em>automatic restarting</em>, when I run this program in the background <code>gcc test.c &amp;&amp; ./a.out &amp;</code>, then I send a signal <code>kill -SIGUSR1 pid</code> or <code>kill -SIGUSR2 pid</code>, <code>waitpid</code> would return and the code after <code>waitpid(pid, &amp;status, 0);</code> would execute.</p>
  
  <p>And if providing <em>automatic restarting</em>, <code>waitpid</code> would execute again and the parent will remain waiting.</p>
  
  <p>Is my understanding correct?</p>