Fall 2000

Lab 8: Signal

CS61C (Patterson)

Purpose:

In this lab you will learn the basic signal handling concepts in C, as well as an important UNIX skill: reading (and understanding) man pages.

Overview:

Most Unix operating systems provide a concept of signal handlers. A signal handler is a C function which that called whenever a signal is sent. A signal is basically a higher level, C version of a trap or interrupt.

Prelab:

The man page for the C library function signal says the function prototype for the signal function is the following: void (*signal (int sig, void (*disp)(int)))(int); Translate that prototype into English. For example, in English the function prototype void vacuum(int air); is "vacuum is a function that takes an integer, air, and returns void."

Task1:

Write a simple C program, which, every 10 seconds, prints out the message "Contemplating meaning of life...\n". You may wish to read the man pages for "sleep", by typing man sleep on the command prompt.

After the program starts running, send an interrupt signal (Control-C) to stop execution.

HINT: Do you remember how to create an infinite loop?

Task2:

Add in the signal handler to catch the interrupt signal, and simply print out "Stop interrupting!\n" every time an interrupt signal is sent, before resuming execution. Read the man pages for signal (man signal) to understand the signal handling in C; and read the man pages for signal(5) (man -s 5 signal) to find the signal value for the interrupt signal.

Task 3:

Right now you will not be able to exit out of the loop simply by pressing Control-C. You can, however, suspend the program by pressing Control-Z. Read the man pages for kill (man kill), and kill the process. You may wish to read the man pages for ps to acquire the process id number that kill needs.

Additional Requirements:

This lab will be e-submitted by the beginning of your section this week. You should submit one file, called signal.c. It should contain the code for your solution to Task 2, and a comment indicating how you accopmlished Task 3.

Last updated: 10/07/2000