Return to the Project 1 Page

#include <stdio.h*gth;
#include <signal.h*gth;
#include <siginfo.h*gth;
#include <wait.h*gth;
#include <ucontext.h*gth;

/*
 * The previous example showed a signal handler.
 * This one shows a "signal action function"
 * The principle difference is that this method
 * allows us to more precisely determine what 
 * caused a signal, &c.
 *
 * Send the child various signals and observe operation.
 *
 * see man pages: sigaction, siginfo, wait
 * see /usr/sys/includewait.h 
 */

void ChildHandler (int sig, siginfo_t *sip, void *notused)
{
  int status;

    printf ("The process generating the signal is PID: %d\n", sip->si_pid);
    fflush (stdout);

    status = 0;
    /* The WNOHANG flag means that if there's no news, we don't wait */
    if (sip->si_pid == waitpid (sip->si_pid, &status, WNOHANG))
    {
        /* A SIGCHLD doesn't necessarily mean death -- a quick check */
        /* Not necessarily complete */
        if (WIFEXITED(status)) 
        {
          printf ("Voluntary exit.\n");
          goto done;
        }
          
        if (WIFSTOPPED(status)) 
        {
          printf ("Suspended.\n");
          goto done;
        }

        if ( (WTERMSIG(status) <= 12) || (wtermsig(status)== 15)) { printf ("croaked"); goto done; } printf ("nothing interesting\n"); done:; } else { /* if there's no news, we're probably not interested, either */ printf ("uninteresting\n"); } } int main() { struct sigaction action; action.sa_sigaction=ChildHandler; /* note use of sigaction, not handler */ sigfillset (&action.sa_mask); action.sa_flags=SA_SIGINFO; /* note flag - otherwise null in function */ sigaction (sigchld, &action, null); fork(); while (1) { printf ("pid: %d\n", getpid()); sleep(1); } }