ast_comparator/test/student_data/handout/shlab-handout/cases/prev_2_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)
{
   struct job_t *this_job;
   int status = -1;
   pid_t pid;
   while((pid=waitpid(-1,&status,WNOHANG|WUNTRACED))>0) 
   {
      this_job = getjobpid(jobs,pid);
      if(this_job == NULL){
         app_error("pid doesn't exist");
         return ;
      }
      if(WIFEXITED(status))
      {
         deletejob(jobs,pid);
      }
      if(WIFSIGNALED(status))
      {
         printf("Job [%d] (%d) terminated by signal 2\n",this_job->jid,this_job->pid);
         deletejob(jobs,pid); 
      }
      if(WIFSTOPPED(status))
      {
         printf("Job [%d] (%d) stopped by signal 20\n",this_job->jid,this_job->pid);
         this_job->state = ST; 
      }
   }
   return;
}
void sigchld_handler ( int sig ) { struct job_t * this_job ; int status = -1 ; pid_t pid ; while ( ( pid = waitpid ( -1 , & status , WNOHANG | WUNTRACED ) ) > 0 ) { this_job = getjobpid ( jobs , pid ) ; if ( this_job == NULL ) { app_error ( " " ) ; return ; } if ( WIFEXITED ( status ) ) { deletejob ( jobs , pid ) ; } if ( WIFSIGNALED ( status ) ) { printf ( " \n " , this_job -> jid , this_job -> pid ) ; deletejob ( jobs , pid ) ; } if ( WIFSTOPPED ( status ) ) { printf ( " \n " , this_job -> jid , this_job -> pid ) ; this_job -> state = ST ; } } 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 .
// return PID of one of the stopped or terminated children. // if none of children stopped or terminated, it return 0; // if returned child normally terminated //deleting jobs if exited normally // if returned child is terminated because of signal that was not caught(in this case, SIGINT) //deleting jobs terminated with signals // if returned child is currently stopped //changing the state of stopped jobs to ST i.e. 3