wait() 会破坏主进程吗?

Does wait() disrupt the main process?

以下是我的代码的主要部分(取自"advanced linux programming",清单3.7):

void clean_up_child_process ( int signal_number ) {
  /* Clean up the child process. */
  int status ;
  wait ( &status ) ;

  printf ( " wait finished \n" ) ;

  /* Store its exit status in a global variable. */
  child_exit_status = status ;
}

int main() {
  /* Handle SIGCHLD by calling clean_up_child_process. */
  pid_t child_pid ;
  struct sigaction sigchld_action ;
  memset ( &sigchld_action, 0, sizeof(sigchld_action) ) ;
  sigchld_action.sa_handler = &clean_up_child_process ;

  sigaction ( SIGCHLD, &sigchld_action, NULL ) ;

  /* Now do things, including forking a child process */
  child_pid = fork () ;

  if ( child_pid > 0 ) {
    sleep ( 60 ) ; // it ends after only 15 seconds
  } else {
    sleep ( 15 ) ;
  exit (0) ;
  }

  printf ( "%d\n", child_exit_status ) ;
  return 0 ;
}

我的推测是该程序大约需要 60 秒才能完成。然而实际发生的情况是:一旦启动,它在子进程终止后立即只运行大约 15 秒。我想知道为什么sleep ( 60 )不会导致主进程持续一段时间,或者被wait()功能打乱。

如果你阅读a sleep manual page你会看到函数

sleep either until the number of real-time seconds specified in seconds have elapsed or until a signal arrives which is not ignored.

[强调我的]

由于没有忽略SIGCHLD信号,所以当子进程退出,父进程得到信号时,sleep函数会被中断