Example #1
0
/* 
 * 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.  
 */
void sigchld_handler(int sig) 
{

    int info; 
    pid_t pid;


    while ((pid = waitpid(fgpid(jobs), &info, WNOHANG|WUNTRACED)) > 0) {

        if (WIFSTOPPED(info))  // Job exited by external process
        { 
            sigtstp_handler(20);
        } 
        else if (WIFSIGNALED(info)) // job terminated due to faulty signal
        {
            sigint_handler(-2);
        } 
        else if (WIFEXITED(info)) // Job exits normalls

        { 
            deletejob(jobs, pid);
        }
    }

   
    if ((errno != ECHILD && pid == -1) || pid > 0) // After the loop pid is -1, or pid is 0
    { 
        unix_error("WAITPID ERROR 404");
    }
    return;
}
Example #2
0
/* 
 * 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.  
 */
void sigchld_handler(int sig) 
{
	int status;
	pid_t pid;
	if (!flag)
	{
		pid = waitpid(-1, NULL, WNOHANG);
		deletejob(jobs, pid);
		return;
	}
	while ((pid = waitpid(-1, &status, WUNTRACED|WNOHANG)) > 0)
	{
		if (WIFSTOPPED(status))
			sigtstp_handler(SIGTSTP);
		else if (WIFSIGNALED(status))
		{
			if (WTERMSIG(status) == SIGINT)
				sigint_handler(SIGINT);
			else
				unix_error("sigchld_handler error");
		}
		else if (WIFEXITED(status))
			deletejob(jobs, pid);
	}
}
Example #3
0
void	signal_handler(int signum)
{
	t_data *data;

	data = NULL;
	data = stock_data(data, 0);
	if(signum == SIGINT)
		sigint_handler(data);
	if(signum == SIGTSTP)
		sigtstp_handler(data);
	if(signum == SIGWINCH)
		sigwinch_handler(data);
	if(signum == SIGCONT)
		sigcont_handler(data);
}
Example #4
0
/* 
 * 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.  
 */
void sigchld_handler(int sig) 
{
/*need to send extra signal*/    
    if (trace16){
       pid_t pid;
       int status;
       while ((pid = waitpid(-1, &status, WNOHANG|WUNTRACED)) > 0){
          if (WIFSTOPPED(status)) sigtstp_handler(SIGTSTP);
          else if (WIFSIGNALED(status)) sigint_handler(SIGINT);
          else if (WIFEXITED(status)) deletejob(jobs,pid);
       }
    }
/*normal ones*/
    else{
       pid_t pid;
       int status;
       pid = waitpid(-1, NULL, WNOHANG);
       deletejob(jobs,pid);
       return;
    }
}
Example #5
0
File: tsh.c Project: vvv214/icsLab
/* 
 * 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.  
 */
void sigchld_handler(int sig)
{
	int status;
	pid_t pid;

	//reap child
	while((pid = waitpid(-1, &status, WNOHANG|WUNTRACED)) > 0){

		//child is stopped
		if(WIFSTOPPED(status))
			sigtstp_handler(SIGTSTP);

		//child is killed
		else if(WIFSIGNALED(status))
			sigint_handler(SIGINT);

		//child exit, just delete the job
		else if(WIFEXITED(status))
			deletejob(jobs, pid);
	}
   	return;
}
Example #6
0
/*
 * 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.
 */
void sigchld_handler(int sig)
{
    pid_t pid;
    int status, child_sig;

    /* reap any zombies and handle status reports */
    while ((pid = waitpid(-1, &status, WUNTRACED | WNOHANG)) > 0) {
        if (WIFSTOPPED(status)) {
            sigtstp_handler(WSTOPSIG(status));
        } else if (WIFSIGNALED(status)) {
            child_sig = WTERMSIG(status);
            if (child_sig == SIGINT)
                sigint_handler(child_sig);
            else
                unix_error("sigchld_handler: uncaught signal\n");
        } else {
            deletejob(jobs, pid); /* remove the job */
        }
    }
    if (pid == -1 && errno != ECHILD)
        unix_error("sigchld_handler: waitpid error");
    return;
}
Example #7
0
/*
 * 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.
 */
void sigchld_handler(int sig)
{
    pid_t pid;
    int status;

    int saved_errno = errno;
    while ((pid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
        if (WIFSTOPPED(status)) { //check if it is a Stop signal (crl-z)
            sigtstp_handler(20); //send signal to signalhandler
            return;
        }

        if (WIFSIGNALED(status)) { //checks for terminate signal (crl-c)
            sigint_handler(-2); //send signal to signalhandler
        }
        deletejob(jobs,pid);
    }
    errno = saved_errno;



    return;
}
Example #8
0
/* 
 * 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'the wait for any other
 *     currently running children to terminate.  
 */
void sigchld_handler(int sig) 
{
    int status;
    pid_t pid;

    while ((pid = waitpid(-fgpid(jobs), &status, WNOHANG|WUNTRACED)) > 0)
    {
        if(WIFEXITED(status))
            deletejob(jobs, pid);                             /* Delete the child from the job list*/

        if(WIFSIGNALED(status))
            sigint_handler(SIGINT);               //jump to sigint handler if ctrl-c is pressed from keyboard

        if(WIFSTOPPED(status))
            sigtstp_handler(SIGTSTP);     //jump to sigstp handler if ctrl-z is pressed from keyboard

    }        /* Reap a zombie child */

    if (errno != ECHILD)
        unix_error("waitpid error");

    return;
}
Example #9
0
File: tsh.c Project: helloylk/shlab
/*
 * 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.
 */
void sigchld_handler(int sig)
{
	int status;  
	pid_t pid;  
    
	// Waiting for/ handling all of the child processes according to their status
	while ((pid = waitpid(fgpid(jobs), &status, WNOHANG|WUNTRACED)) > 0) {  
		if (WIFSTOPPED(status)){  
			sigtstp_handler(20);  
		}  
		else if (WIFSIGNALED(status)){  
			sigint_handler(-2);  
		}  
		else if (WIFEXITED(status)){  
			deletejob(jobs, pid);  
		}  
	}  
	
	if (errno != ECHILD) {  
		unix_error("waitpid error");   
	}  
	
	return; 
}}