Skip to main content
Logo image

Dive Into Systems: Exercises

Section 13.4 Signals

Checkpoint 13.4.1. Interprocess Communication Types.

Checkpoint 13.4.2. Signals.

    True or False: Signals allow one process to send a number and a message string to another process.
  • True
  • Incorrect. A signal allows only a single number to be sent. Each valid number is associated with a signal name, like `SIGINT` or `SIGSEGV`.
  • False
  • Correct!

Checkpoint 13.4.3. Signal Handling.

    What must be added to a program to change the default action of a signal? Select all that apply.
  • A function that defines the actions to perform when the signal is received.
  • Correct!
  • A call to the signal or sigaction function to register a signal handling function on a particular signal.
  • Correct!
  • A call to the alarm function.
  • Incorrect.
  • A call to the fork function.
  • Incorrect.
  • A call to the exit function.
  • Incorrect.
  • A function that defines actions for a child process to perform.
  • Incorrect.

Checkpoint 13.4.4. Signal Handling.

    If a program registers a function named sigchld_handler on the SIGCHLD signal, which event(s) trigger sigchld_handler to run? Select all that apply.
  • When this process calls fork.
  • Incorrect.
  • When this process is blocked on a call to wait.
  • Incorrect.
  • When a child of this process calls execv.
  • Incorrect.
  • When a child of this process exits.
  • Correct!
  • When a child of this process calls fork.
  • Incorrect.

Checkpoint 13.4.5. Signal Handling Actions.

    Which of the following actions of the OS may occur when a process is sent a signal? Select all that apply.
  • The OS waits until the process’s time slice is up to run the signal handler code.
  • Incorrect.
  • The OS interrupts the process to run the signal handler code.
  • Correct!
  • The OS waits until the process calls fork to run the signal handler code.
  • Incorrect.
  • The OS terminates the process after running the signal handler code.
  • Correct!
  • The OS restarts the process where it left off after it runs the signal handler code.
  • Correct!
  • The OS restarts the process at its first instruction after it runs the signal handler code.
  • Incorrect.

Checkpoint 13.4.6. Signal Handlers.

    Processes can override the OS’s default signal handler behavior for any signal.
  • True
  • Incorrect. For some signals, the OS-defined default action cannot be overridden. For example, the SIGKILL (the process is forced to exit) and SIGSTOP (the process is blocked) default actions cannot be overwritten.
  • False
  • Correct!

Checkpoint 13.4.7. Signal Handling.

    A signal handler is a function that the program defines but never explicitly calls.
  • True
  • Correct!
  • False
  • Incorrect. A signal handler function is registered on a signal by calling the signal function. It is executed when the OS delivers the signal to the process.

Checkpoint 13.4.8. SIGINT Handling.

    Consider the following C code snippet:
    #include <stdio.h>
    #include <signal.h>
    #include <unistd.h>
    
    void sigint_handler(int sig) {
        printf("Caught signal number %d...exiting\n.", sig);
        fflush(stdout);
        exit(0);
    }
    
    int main(void) {
        if (signal(SIGINT, sigint_handler) == SIG_ERR) {
            printf("Error call to signal, SIGINT\n");
            exit(1);
        }
        
        while (1) {
            printf("Running...\n");
            sleep(1);
        }
        return 0;
    }
    
    What happens when the user presses CTRL-C while this program is running (note: CTRL-C triggers a SIGINT signal to be sent to the process)?
  • The process terminates immediately.
  • Incorrect.
  • The process pauses and waits for user input.
  • Incorrect.
  • The sigint_handler function is called, printing "Caught signal number 2...exiting" and then the process terminates.
  • Correct!
  • The process restarts automatically.
  • Incorrect.

Checkpoint 13.4.9. SIGCHLD Handling.

    Consider the following code:
    void sigchld_handler(int signum) {
        int status;
        pid_t pid;
    
        while( (pid = waitpid(-1, &status, WNOHANG)) > 0) {
            //….
        }
    }
    
    
    int main(void) {
    pid_t fret;
    
    signal(SIGCHLD, sigchild_handler);
    if (signal(SIGCHLD, sigchld_handler) == SIG_ERR) {
            printf("Error call to signal, SIGCHLD\n");
            exit(1);
    }
    
    fret = fork();
    if (fret < 0) {
            printf("Error fork failed!\n");
            exit(1);
    }
    // Parent and child run concurrently
    …
    }
    
    Which of the following are true? Select all that apply.
  • The handler sigchld_handler is run when a child process gets created
  • Incorrect.
  • Code in sigchld_handler cannot run until fork is called at least once
  • Correct!
  • The option WNOHANG is used so that the child process doesn’t get stuck waiting
  • Incorrect.
  • The loop is needed because multiple child processes may exit before the parent has a chance to respond.
  • Correct!

Checkpoint 13.4.10. Shared Memory Advantages.

    What is one advantage of using shared memory instead of message passing for inter-process communication?
  • Shared memory allows processes on different computers to communicate with each other, while message passing cannot.
  • Incorrect.
  • Shared memory allows two processes running on the same machine to communicate more efficiently than message passing.
  • Correct!
  • Shared memory provides better protection between communicating processes than message passing.
  • Incorrect.
  • Shared memory limits communication to only one direction, thereby granting greater efficiency than message passing.
  • Incorrect.

Checkpoint 13.4.11. Message Passing Advantages.

    What is one advantage of using message passing instead of shared memory for inter-process communication?
  • Message passing allows processes on two different machines to communicate with each other.
  • Correct!
  • Message passing allows multiple processes to directly access the same memory space.
  • Incorrect.
  • Message passing is more efficient than shared memory when two processes are running on the same machine.
  • Incorrect.
  • Message passing prevents two processes running on the same machine from communicating.
  • Incorrect.

Checkpoint 13.4.12. Types of message passed by different forms of IPC.