"[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:
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.
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).
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:
void function_name (int arg)
struct sigaction variable in your main() function
sa_handler field of your struct sigaction variable
your signal handling function
sa_flags field of your struct sigaction variable SA_RESTART
sigaction() function in your main() function, with SIGINT as the signum argument and a pointer to your struct sigaction for the act argument.
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.
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.
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:
char* message = "handler called\n")
write() function with argument fd equal to zero to write to standard output (e.g. write(0, message, num_chars);)
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.
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.
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.
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.
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.
For this studio, please turn in the following:
dense_mm.c that contains a signal handlersender.c and modified receiver.c programs that send and receive real-time signals with dataPage updated Monday, August 10, 2020, by Marion Sudvarg and Chris Gill.