CSE 422S: Studio 4

Linux Signal Handling


"[S]ignals are significantly more powerful and expressive when SA_SIGINFO-style handlers are used."

—Robert Love, Linux System Programming, 2nd Edition, Chapter 10, pp. 362

Signals are a simple yet powerful way for Linux processes to communicate in pre-defined ways. They are reasonably well suited for inter-process event handling during system operation, especially for parent-child process interactions. Signals can be sent to any process in the system, and can be recieved by defining special functions called signal handlers, which are executed upon receipt of the signal.

In this studio, you will:

  1. Generate and recieve program signals
  2. Create custom signal handlers

Please complete the required exercises below, as well as any optional enrichment exercises that you wish to complete. We encourage you to please work in groups of 2 or 3 people on each studio (and the groups are allowed to change from studio to studio) though if you would prefer to complete any studio by yourself that is allowed.

As you work through these exercises, please record your answers, and when you finish them each and every person who worked on them should please log into Canvas, select this course in this semester, and then upload a file containing them and submit them for this studio assignment (there should be a separate submission from each person who worked together on them).

Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.


Required Exercises

  1. As the answer to the first exercise, list the names of the people who worked together on this studio.

  2. For the first exercise we will use an existing program to handle signals with a custom signal handler. On your Raspberry Pi 3, or on one of the Linux Lab cluster servers, download a code package that includes five programs here. Unzip the package, and build the programs with the Makefile that is provided in it.

    You can also compile each program individually, as in:

    gcc dense_mm.c -o dense_mm

    Run the dense_mm program, and let it complete. Then, run it again, and this time while this program is executing, send it the keyboard interrupt signal (SIGINT) by pressing CTRL+C (hold down the CTRL button and press C) while its terminal window is selected (has the input focus). As the answer to this exercise, copy and paste terminal window output showing you successfully interrupting your program in this way (i.e., show the run with it completing uninterrupted and show the run when you interrupted it).

  3. There are many signals that can be sent or recieved in Linux. An overview of the signal mechanism can be found at the manual page displayed when you issue the terminal window command man 7 signal. Some commonly used signals are SIGINT (keyboard interrupt), SIGQUIT (telling a program to quit from keyboard), and SIGTERM (terminate process). Every signal in the system is associated with a default action (which typically terminates the recieving process), but each of these can be overridden simply by defining your own signal handling function, which is what we will now do.

    The modern, portable way to define a signal handler is to use the function sigaction(), which is documented by man 2 sigaction. Use this function to define a new handler for SIGINT within the dense_mm.c file. You will need to:

    Declare a global counter variable, intialize it to 0, and increment it each time your signal handler function is called. At the end of your main() function, before it returns, print the value of this counter.

    Build and run your program, and while it is running send it SIGINT a few times by hitting CTRL+C. Copy and paste program output from such a run, as the answer to this exercise.

    NOTE: Overriding the SIGINT handler may make it difficult to terminate your program. You can alternately use CTRL+\ to send the SIGQUIT signal or the kill program to terminate your processes.

  4. One way that signal handlers differ from other functions of a user-space program is that they execute asynchronously with respect to the rest of a program. When called, they may interrupt the normal execution of the program at virtually any point. Consider what might happen, for example, if the same function is called from both the regular program flow as well as a signal handler. This could result in the function being called concurrently from two different contexts, even within a single-threaded program! This could cause data corruption or even deadlock if not handled appropriately.

    Even library functions are not immune to problems with such asynchrony. Download the program print500000.c, build and run it. This is a very simple program that defines a signal handler for SIGINT, and then prints the numbers from 1 to 500000 in order. Try running this program and interrupting it with SIGINT. Inspect both the program code and the program output where it was possibly interrupted by a signal handler.

    As the answer to this exercise, please explain what happens and what evidence you saw (or didn't see) for the program being interrupted, and whether or not that caused any problems.

  5. For the reasons outlined above, it may not be safe to make many common system calls or library function calls from within a signal handler. Many functions, such as printf() are not guaranteed to execute correctly when called from within a handler, since they may be called concurrently from different contexts. Fortunately, there is a list of functions that is guaranteed to be safe when called from a handler. These functions are given in the signal documentation at man 7 signal under "Async-signal-safe functions".

    In your updated version of the dense_mm.c code (the one with the signal handler you added) use the write() function documented in the man 2 write page, to produce output from within the signal handler. In particular, you must:

    Build and run each program, interrupting it, and as the answer to this exercise please show the output of each program that includes the message output from within the signal handler, and explain briefly whether or not there were any problems because of doing that.

  6. The next few exercises will explore the differences between standard signals and real-time signals.

    First, download the program receiver.c and build it.

    Look at the code, and as the answer to this exercise, briefly explain what it does.

  7. Now, write and compile a second program, sender.c, that takes a PID and a number of signals to send as arguments. Your program should send SIGUSR1 to the specified PID a number of times equal to the argument provided. It should then send SIGUSR2 once.

    Now, run your receiver program, and note its PID.

    Run your sender program in a new terminal window, providing the receiver's PID and a number of signals to send.

    As the answer to this exercise, please report how many times the receiver received SIGUSR, and why you think this behavior makes sense.

  8. Now, you will modify both programs to use real-time signals.

    In receiver.c, replace SIGUSR1 and SIGUSR2 with SIGRTMIN and SIGRTMIN+1 respectively.

    In your sender.c program, do the same thing, and replace calls to kill() with appropriate calls to sigqueue().

    The Linux kernel enforces resource limits to prevent userspace programs from hogging computational resources. One such limit is RLIMIT_SIGPENDING, which limits the number of queued real-time signals a process may have pending.

    In your sender.c program, use a call to getrlimit to determine the current limit (also called the soft limit) of this value, and ensure that the provided argument for the number of signals to send does not exceed this value. More information on getrlimit can be found with the following command:

    man 2 getrlimit

    Now, run your receiver and sender again, like in the previous exercise. As the answer to this exercise, please report how many times the receiver received SIGRTMIN, and tell us the current limit on the number of pending real-time signals.

  9. Now, we are going to explore sending signals with data. Instead of using two signals (one to increase a counter, and one to end the program), we will use a single signal type, but with accompanying data to indicate what action should occur.

    First, modify your sender.c program. The sigqueue function includes a union sigval parameter.

    This union contains an integer value, sival_int

    Where the sender would have previously sent SIGRTMIN, have it again send SIGRTMIN but with sival_int = 0.

    Where the sender would have previously sent SIGRTMIN + 1, have it now send SIGRTMIN but with sival_int = 1.

    Now, modify receiver.c so it only handles SIGRTMIN.

    To allow receiver.c to access the integer it has been sent, instead of setting ss.sa_handler, you will need to set ss.sa_sigaction. You will also need to add (using bitwise OR) the SA_SIGINFO flag to ss.sa_flags.

    Modify the signal handler function with a new signature:

    void sig_handler( int signum, siginfo_t * si, void * ucontext )

    You can access the sigval union through si->si_value

    Modify the signal handler appropriately so that it increments the count when sival_int == 0 and sets the done flag when sival_int == 1.

    Now, run your receiver and sender again, like in the previous exercise. As the answer to this exercise, please report how many times the receiver received SIGRTMIN with sival_int == 0.

  10. Things to Turn In:

    For this studio, please turn in the following:


Page updated Monday, August 10, 2020, by Marion Sudvarg and Chris Gill.