Comp111: Operating Systems
Classroom Exercise 4
Signals and handlers
Fall 2018

group member 1: ____________________________ login: ______________
group member 2: ____________________________ login: ______________
group member 3: ____________________________ login: ______________
group member 4: ____________________________ login: ______________
group member 5: ____________________________ login: ______________
group member 6: ____________________________ login: ______________
group member 7: ____________________________ login: ______________
group member 8: ____________________________ login: ______________

In class we have discussed the concept of a signal, including sending and handling signals. Let's make sure we understand the important issues.

  1. Why can't you block a segmentation fault signal (SIGSEGV)?

  2. I claimed in class that one can actually stack signals when using obsolete signal() syntax, but if you try that with the keyboard you will not be able to do it. How can one reliably generate stacked signals?

  3. Why is there a separate signal stack for receipt of signals? What would go wrong if the execution stack were used for this purpose?
  4. Consider the code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <unistd.h> 
    #include <signal.h> 
    #include <sys/time.h> 
    #include <sys/resource.h> 
    void reaper(int sig, siginfo_t *info, void *buf) {  
        int status; 
        int pid = waitpid(-1, &status, 0); // wait for any child 
        printf("pid %d with status %d reaped by parent\n", pid, WEXITSTATUS(status)) ; 
    main() { 
        struct sigaction sa, old_sa;  
        sigset_t mask; 
        sa.sa_sigaction = reaper; 
        sa.sa_mask = mask;  
        sa.sa_flags = SA_SIGINFO;  // enable siginfo_t argument
        if (fork()) { 
            printf("I'm the parent with PID %d\n", getpid());
            sigaction(SIGCHLD, &sa, &old_sa);  
        } else { 
            printf("I'm the child with PID %d\n", getpid());
    1. What happens if you leave off the WEXITSTATUS and just print status? Why?

    2. You observe that some times, the printout in reaper does not occur. Why does this happen?

  5. (Advanced) Some really sneaky signal handlers actually correct for errors. For example, it is possible with extreme cleverness to catch and correct a SIGFPE (floating point exception). But this is tricky. Explain why it's difficult and what you would have to do to correct a divide-by-zero error.