ast_comparator/test/student_data/handout/shlab-handout/cases/51_tsh.c
/*
 * sigchld_handler - The kernel sends a SIGCHLD to the shell whenever
 *     a child job terminates (becomes a zombie), or stops because it
 *     received a SIGSTOP or SIGTSTP signal. The handler reaps all
 *     available zombie children, but doesn't wait for any other
 *     currently running children to terminate.
 */
The kernel sends a SIGCHLD to the shell whenever
a child job terminates (becomes a zombie), or stops because it
received a SIGSTOP or SIGTSTP signal. The handler reaps all
available zombie children, but doesn't wait for any other
currently running children to terminate.
The kernel sends a SIGCHLD to the shell whenever a child job terminates ( becomes a zombie ) or stops because it received a SIGSTOP or SIGTSTP signal . The handler reaps all available zombie children but doesn ' t wait for any other currently running children to terminate .
void sigchld_handler(int sig)
{
  pid_t tmp;
  int status;
  while((tmp = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) { 
    if(WIFSIGNALED(status)) 
      printf("Job [%d] (%d) terminated by signal %d\n", pid2jid(tmp), tmp, WTERMSIG(status));
    else if(WIFSTOPPED(status)) { 
      printf("Job [%d] (%d) stopped by signal %d\n", pid2jid(tmp), tmp, WSTOPSIG(status));
      getjobpid(jobs, tmp)->state = ST; 
      continue;
    }
    else if(WIFCONTINUED(status)) 
      continue;
    deletejob(jobs, tmp); 
  }
  if(tmp < 0) 
    unix_error("SIGCHLD_HANDLER ERROR");
  return;
}
void sigchld_handler ( int sig ) { pid_t tmp ; int status ; while ( ( tmp = waitpid ( -1 , & status , WNOHANG | WUNTRACED ) ) > 0 ) { if ( WIFSIGNALED ( status ) ) printf ( " \n " , pid2jid ( tmp ) , tmp , WTERMSIG ( status ) ) ; else if ( WIFSTOPPED ( status ) ) { printf ( " \n " , pid2jid ( tmp ) , tmp , WSTOPSIG ( status ) ) ; getjobpid ( jobs , tmp ) -> state = ST ; continue ; } else if ( WIFCONTINUED ( status ) ) continue ; deletejob ( jobs , tmp ) ; } if ( tmp < 0 ) unix_error ( " " ) ; return ; }
sigchld_handler - The kernel sends a SIGCHLD to the shell whenever
     a child job terminates (becomes a zombie), or stops because it
     received a SIGSTOP or SIGTSTP signal.
sigchld_handler - The kernel sends a SIGCHLD to the shell whenever a child job terminates ( becomes a zombie ) or stops because it received a SIGSTOP or SIGTSTP signal .
// check if there is a stopped or terminated child process // (1) check if the child process was terminated by other signals (ex. SIGSEGV, SIGUSR1, etc) // (2) check if the child process was stopped // change the state of the job // (3) check if the child process is currently running // (4) remove job from job list // (5) check waitpid error