ast_comparator/test/student_data/handout/shlab-handout/cases/50_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 pid;
	int status;
	while((pid = waitpid(-1, &status, WUNTRACED | WNOHANG)) > 0)	
	{
		if(WIFSTOPPED(status))
		{
			if(WSTOPSIG(status))
				printf("Job [%d] (%d) stopped by signal %d\n", pid2jid(pid), pid, SIGTSTP);
			getjobpid(jobs, pid)->state = ST;
		}
		else
		{
			if(WTERMSIG(status))
				printf("Job [%d] (%d) terminated by signal %d\n", pid2jid(pid), pid, SIGINT);
			struct job_t* job = getjobpid(jobs, pid);
			if(job->in_fd > 0)
				if(close(job->in_fd) < 0)
					unix_error("close error");
			if(job->out_fd > 0)
				if(close(job->out_fd) < 0)
					unix_error("close error");
			deletejob(jobs, pid);
		}
	}
	return;
}
void sigchld_handler ( int sig ) { pid_t pid ; int status ; while ( ( pid = waitpid ( -1 , & status , WUNTRACED | WNOHANG ) ) > 0 ) { if ( WIFSTOPPED ( status ) ) { if ( WSTOPSIG ( status ) ) printf ( " \n " , pid2jid ( pid ) , pid , SIGTSTP ) ; getjobpid ( jobs , pid ) -> state = ST ; } else { if ( WTERMSIG ( status ) ) printf ( " \n " , pid2jid ( pid ) , pid , SIGINT ) ; struct job_t * job = getjobpid ( jobs , pid ) ; if ( job -> in_fd > 0 ) if ( close ( job -> in_fd ) < 0 ) unix_error ( " " ) ; if ( job -> out_fd > 0 ) if ( close ( job -> out_fd ) < 0 ) unix_error ( " " ) ; deletejob ( jobs , pid ) ; } } 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 .
// Determine the status of child