CS 201 Signals: Gerson Robboy Portland State University
CS 201 Signals: Gerson Robboy Portland State University
Signals
Gerson Robboy Portland State University
Signals
A signal is a message that notifies a process that an event of some type has occurred.
Signals are the operating system abstraction for exceptions and interrupts.
15-213, F02
Signal Concepts
Sending a signal
Kernel sends (delivers) a signal to a destination process. Kernel sends a signal for one of the following reasons:
Kernel has detected a system event such as divide-by-zero
(SIGFPE) or the termination of a child process (SIGCHLD) Another process has invoked the kill system call to request the kernel to send a signal to the destination process.
15-213, F02
15-213, F02
A destination process receives a signal sent by the kernel. By default, most signals cause the process to terminate. But, theres a way a process can handle most signals. Well see how. Three possible ways to react:
Ignore the signal (do nothing)
Terminate the process. Catch the signal by executing a user-level function called a
Signals
Sent by the kernel to a process. Different signals are identified by small integer IDs The only information in a signal is its ID and the fact that it arrived. A few frequently seen signals:
ID Name 2 SIGINT 9 SIGKILL 11 SIGSEGV 14 SIGALRM 17 SIGCHLD
6
Corresponding Event Interrupt from keyboard (ctl-c) Kill program (cannot override or ignore) Segmentation violation Timer signal Child stopped or terminated
15-213, F02
Default Actions
Each signal type has a predefined default action, which is one of:
The process terminates The process terminates and dumps core. The process stops until restarted by a SIGCONT signal. The process ignores the signal.
15-213, F02
Control goes to the kernel, via an interrupt vector. The kernel has a handler for each kind of event. Usually this detour of control is invisible to the user process.
For some events, particularly faults, the kernel handles the event by sending a signal to the process.
Alters the flow of control of a process. Can also have a handler (analogous to an exception handler)
15-213, F02
By default, most signals (but not all) terminate the process. Can send control to a signal handler in the user program.
15-213, F02
SIG_IGN: ignore signals of type signum SIG_DFL: revert to the default action for signals of type signum.
Yes, this is weird, but you can assign these integer values to a
pointer.
10
15-213, F02
Signal Handlers
A signal handler is a function you write, to handle a signal.
In some cases, the next instruction. In some cases, the instruction that was interrupted by an exception.
15-213, F02
12
Exercise
Write a small program that installs a handler for the SIGSEGV signal, and then accesses an illegal memory address in order to execute the handler.
14
15-213, F02
linux> ./forks 16 linux> Child1: pid=24818 pgrp=24817 Child2: pid=24819 pgrp=24817 linux> ps PID TTY TIME CMD 24788 pts/2 00:00:00 tcsh 24818 pts/2 00:00:02 forks 24819 pts/2 00:00:02 forks 24820 pts/2 00:00:00 ps linux> kill -9 -24817 linux> ps PID TTY TIME CMD 24788 pts/2 00:00:00 tcsh 24823 pts/2 00:00:00 ps linux>
kill 9 24818
Send SIGKILL to
process 24818
kill 9 24817
Send SIGKILL to
15-213, F02
The recipient process can catch the signal if not SIGKILL Synchronization between processes
16
15-213, F02
WNOHANG: Return immediately if no child has exited (and if a child has exited, return the pid) WUNTRACED: Also return for children which are stopped but not traced.
15-213, F02
17
18
15-213, F02
{
int i; int j = alarm(6);
while(1);
printf("exiting\n"); exit(0); }
20
15-213, F02
}
21 15-213, F02
22
15-213, F02
The process is handling the first one The second one is pending Other signals may be lost
23
15-213, F02
int ccount = 0; void child_handler(int sig) { int child_status; pid_t pid = wait(&child_status); ccount--; printf("Received signal %d from process %d\n", sig, pid); }
int main() { pid_t pid[N]; int i, child_status; ccount = N; signal(SIGCHLD, child_handler); for (i = 0; i < N; i++) if ((pid[i] = fork()) == 0) { exit(0); } while (ccount > 0) pause();/* Suspend until signal occurs */ }
24
15-213, F02
If a process has many children and wants to reliably know when each one terminates, how do you do it?
25
15-213, F02
Sigaction()
The newer version of signal with a zillion options. Problem: different flavors of Unix have subtle variations in how they handle signals (see sec. 8.5)
The POSIX solution : sigaction can specify in detail how signal handling should behave Use sigaction to write portable signal-handling code. On POSIX-compliant systems, signal is implemented using sigaction.
26
Summary
Signals provide process-level exception handling
Some caveats
27
15-213, F02