Unit II Signals
Unit II Signals
Signals
Instructors:
Dr. Santhosh
Signals
A signal is a small message that notifies a process that an
event of some type has occurred in the system
Akin to exceptions and interrupts
Sent from the kernel (sometimes at the request of another process) to a
process
Signal type is identified by small integer ID’s (1-30)
Only information in a signal is its ID and the fact that it arrived
pid=10
pgid=10
Shell
Background Background
process group 32 process group 40
Child Child
linux> ps
Examples PID TTY TIME CMD
/bin/kill –9 24818 24788 pts/2 00:00:00 tcsh
Send SIGKILL to process 24818 24818 pts/2 00:00:02 forks
24819 pts/2 00:00:02 forks
24820 pts/2 00:00:00 ps
/bin/kill –9 –24817 linux> /bin/kill -9 -24817
linux> ps
Send SIGKILL to every process
PID TTY TIME CMD
in process group 24817 24788 pts/2 00:00:00 tcsh
24823 pts/2 00:00:00 ps
linux>
Background Background
process group 32 process group 40
Child Child
pid=21 pid=22
pgid=20 pgid=20
Foreground
process group 20 9
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Carnegie Mellon
Receiving Signals
Suppose kernel is returning from an exception handler
and is ready to pass control to process p
Process A Process B
user code
user code
Receiving Signals
Suppose kernel is returning from an exception handler
and is ready to pass control to process p
If (pnb == 0)
Pass control to next instruction in the logical flow for p
Else
Choose least nonzero bit k in pnb and force process p to receive
signal k
The receipt of the signal triggers some action by p
Repeat for all nonzero k in pnb
Pass control to next instruction in logical flow for p
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 13
Carnegie Mellon
Default Actions
Each signal type has a predefined default action, which is
one of:
The process terminates
The process stops until restarted by a SIGCONT signal
The process ignores the signal
int main()
{
/* Install the SIGINT handler */
if (signal(SIGINT, sigint_handler) == SIG_ERR)
unix_error("signal error");
return 0;
} sigint.c
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 16
Carnegie Mellon
Time
kernel code
Inext
user code (main)
Supporting functions
sigemptyset – Create empty set
sigfillset – Add every signal number to set
sigaddset – Add signal number to set
sigdelset – Delete signal number from set
Sigemptyset(&mask);
Sigaddset(&mask, SIGINT);
Async-Signal-Safety
Function is async-signal-safe if either reentrant (e.g., all
variables stored on stack frame, CS:APP3e 12.7.2) or non-
interruptible by signals.
Posix guarantees 117 functions to be async-signal-safe
Source: “man 7 signal”
Popular functions on the list:
_exit, write, wait, waitpid, sleep, kill
Popular functions that are not on the list:
printf, sprintf, malloc, exit
Unfortunate fact: write is the only async-signal-safe output function
csapp.c
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 26
Carnegie Mellon