Lab 10: Signals, and using sigaction

Due Thursday, April 9, by 9:00 AM


Unlike earlier labs, but like lab 9, the solution will be a C program rather than a series of commands.

Each process has a unique number called a Process ID (PID), and using this number, it is possible to send signals to the process. For this lab, first set up your program to send signal 2 to itself, which will terminate the lab. To show this, set up your program like this:
int main(){
	// Retrieve this program's PID, using getpid
	// Send signal 2 to this process, using kill

	puts("Reached the end safe and sound!  Try again!\n");
	return 0;
}
If your program works right, it will NOT print out the message at the end. Then, modify it by installing a signal handler for signal 2, so it looks like this:
void signal_handler(int){
	puts("We're in the signal handler!\n");
}

int main(){
	// Install a handler for signal 2, so that the function above will run
	// Use the sigaction function for this
	// You'll have to make an sa_handler struct for it to use

	// Retrieve this program's PID, using getpid
	// Send signal 2 to this process, using kill

	puts("Reached the end safe and sound!  But that's what it should do now!\n");
	return 0;
}
There is a demo called handle_sigint.c in the class examples that will give an example of how to use sigaction. Turn in your lab by creating a file named "lab10.c" with no extension in your home directory, with permissions set so that only you can view the file. Substitute a c++ file extension if your lab will only compile with c++. This assignment is worth 35 points, 5 from correct naming and permissions, and 30 from working correctly. As a reminder, you can set the permissions correctly with this line:
chmod 600 lab10.c