Signals are a mechanism for basic inter-process notification and event handling. Signals do not convey any data in themselves, but they do allow cooperating processes to invoke remote functions in other processes. In addition, signals are the mechanism central to handling certain basic functions you expect from an operating system, such as the ability to interrupt, suspend, or otherwise modify a currently running program.
In this studio, you will:
Please complete the required exercises below, as well as any optional enrichment exercises that you wish to complete.
As you work through these exercises, please record your answers in a text file. When finished, submit your work by sending your text file and source code to firstname.lastname@example.org with the phrase Signal Handling in the subject line.
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.
Copy and paste some program output as the answer to this exercise.
Open the manual page at
man 7 signal. Scroll down to the
list of standard signals. What numeric value does SIGINT have? What is the
sleep.cprogram in the first terminal and make a note of its PID. In the second terminal use the
killcommand documented at
man 1 killto send the SIGINT signal to the sleeping process. (Note- despite the name, the
killcommand can send any signal to a process, not just the kill signal!. The syntax for doing this is below:
kill -s <signal number> <pid>
What happens to the sleeper process?
man 7 signaland look through the list of standard signals. Pick another signal to send to a sleeping process. What signal did you pick? What happened? Copy and paste the results.
exec()s a sleeper program, and then waits for it to finish. After the child returns it will print out a success message.
call_sleeper.c and then terminating the
sleeper with CTRL-C. Which processes terminate: the sleeper, the caller, or
both? Did the calling program print its success message?
signal()system call that is documented at
man 2 signal.
Note: There is an newer function called
sigaction() that is
the newer, better way to do this. However, the interface is much more
complicated (but flexible!) so we will use this function instead.
First you will need to write the signal handler function. This function should have the signature:
void <function_name> ( int signum )
In the body of that function, print a message stating that your program is
ignoring SIGINT. Then, in the
main() function, make a call to
signal(). The first parameter should be the signal number for
SIGINT and the second should be the name of your signal handler function.
Copy and paste your signal handling code.
Copy and paste your program output.NOTE: Once you handle SIGINT in this way, you will no longer be able to kill your process with CTRL-C. If you run into trouble you can still terminate your program with CTRL-\, which sends the SIGQUIT signal.
man 2 killto implement asynchronous program communication. For example, have one process wait in a loop until it recieves SIGUSR1, and then perform some useful work.