1. Goals for this week

  • Practice with signals and signal handler functions.

2. Starting Point Code

Start by creating a week11 directory in your weeklylab subdirectory and copying over some files:

$ cd ~/cs31/weeklylab
$ mkdir week11
$ cd week11
$ pwd
/home/you/cs31/weeklylab/week11
$ cp ~richardw/public/cs31/week11/* ./
$ ls
Makefile  signals.c

3. Signals and Signal Handlers

Let’s look at the program signals.c. This program has some examples of registering a signal handler function on a signal, and of some examples of ways in which you can send signals to processes. For example, the alarm function` can be used by a process to send itself a SIGALRM signal after a specified amount of time.

Let’s try running the program and see what it is doing. Open a new bash shell (a new terminal) and run the following:

$ make
$ ./signals
signals: pid 436474

The program reports that it is process id 436474. You will get a different value for your process id which you should substitute in the examples below.

We will try to use the kill command to send the process signals. Open another bash shell (another terminal) and try the following:

$ ps -u               # you should be able to find signal's pid in the list
                      # of procsses that are running

$ kill -CONT 436474   # sends a SIGCONT signal to process 436474
$ kill -18   436474   # SIGCONT is signal #18 (no need to memorize this!)

$ kill -INT 436474    # sends a SIGINT signal to process 436474
$ kill -2   436474    # SIGINT is signal #2 (no need to memorize this!)

The man page for signal lists the signals on this system and describes the signal system call in more detail.

You can also try changing some of the handler code to see what happens. Try changing the SIGINT handler to not call exit, and to print out some other message. Then see what happens when you type (assume 436474 is the pid):

$ kill -INT 436474

To kill the process now, you need to send it a SIGKILL signal:

$ kill -9 436474

4. Implementing a shell

Proceed to Lab 8. This is the second part of the two-week lab. In this part of the lab, you will be writing a shell, which will require you to implement a signal handler.

5. Handy References