All about Linux signals
Table of Contents:
- All about Linux signals
- Introduction
- What is signaled, signal handlers
- Handling specific signals: SIGCHLD, SIGBUS, SIGSEGV, SIGABRT
- What happens when a process receives a signal, system call interruption
- Blocking signals
- Waiting for a signal
- Sending signals
- Real-time signals
- Signals and fork()
- Signals and threads
- Other uses of signals
- That's not everything!
What happens when a process receives a signal?
Default actions
With each signal there is an associated default action which is taken when you don't provide a signal handler and you don't block a signal. The actions are:
- Termination of a process. This is the most common action. Not only for
SIGTERM
orSIGQUIT
but also for signals likeSIGPIPE, SIGUSR1, SIGUSR2
and others. - Termination with code dump. This is common for signals that indicate a bug in the program like
SIGSEGV, SIGILL, SIGABRT
and others. - Few signals are ignored by default like
SIGCHLD
. -
SIGSTOP
(and similar stop signals) cause the program to suspend andSIGCOND
to continue. The most common situation is when you use the CTRL-Z command in the shell.
For a complete list of default actions see the signal(7) manual page.
Interrupting system calls
If you set a signal handler in your program you must be prepared that some system calls can be interrupted by signals. Even if you don't set any signal handler there could be signals delivered to your program so it's best to be prepared for that. An example situation is compiling your program with the -pg gcc option (enable profiling), so when running it occasionally gets
SIGPROF
handled without your knowledge, but causing syscalls to be interrupted.
What is interrupted?
Every system or standard library function that uses a system call can be potentially interrupted and you must consult it's manual page to be sure. In general function that return immediately (don't wait for any I/O operation to complete or sleep) are not interruptible like socket(2) which just allocates a socket and doesn't wait for anything. On the other hand functions that wait for something (like for a network transfer, pipe read, explicit sleep etc.) will be interruptible like select(2), read(2), connect(2) and you must be prepared for that. What exactly happens when a signal arrives during waiting for such function to complete is described in it's manual page.
Simple example of signal aware code
The simplest case is sleep(3) which is implemented using nanosleep(2). If it's interrupted by a signal it exits returning number of seconds left to sleep. If you want to sleep 10s regardless of signals that are handled by your application you must do something like:
#include <unistd.h> #include <signal.h> static void hdl (int sig) { } void my_sleep (int seconds) { while (seconds > 0) seconds = sleep (seconds); } int main (int argc, char *argv[]) { signal (SIGTERM, hdl); my_sleep (10); return 0; }
This example works, but if you try it and send few signals during sleep you can see that it may sleep different amount of time. This is because sleep(3) takes the argument and returns the value with 1s resolution so it can't be precise telling you how long it need to sleep after interruption.
Data transferring and signals
Very important thing in daemon programs is proper handling of interruption of system functions. One part of the problem is that common functions that transfer data like recv(2), write(2) and similar like select(2) may be interrupted by a signal which is handled in it's handler, so you need to continue receiving data, restart select(2) etc. We've just seen a simple example how to handle it in case of sleep(3).
See an example of how to handle interruption of system calls.
This program reads from it's standard input and copies the data to the standard output. Additionally, when SIGUSR1
is received it prints to stderr
how many bytes has been already read and written. It installs a signal handler which sets a global flag to 1 if called. Whatever the program does at the moment it receives the signal, the numbers are immediately printed. It works because read(2) and write(2) functions are interrupted by signals even during operation. In case of those functions two things might happen:
- When read(2) waits for data or write(2) waits for
stdout
to put some data and no data were yet transfered in the call andSIGUSR1
arrives those functions exit with return value of -1. You can distinguish this situation from other errors by reading the value of theerrno
variable. If it'sEINTR
it means that the function was interrupted without any data transfered and we can call the function again with the same parameters. - Another case is that some data were transfered but the function was interrupted before it finished. In this case the functions don't return an error but a value less that the supplied data size (or buffer size). Neither the return value nor the
errno
variable tells us that the function was interrupted by a signal, if we want to distinguish this case we need to set some flag in the signal handler (as we do in this example). To continue after interruption we need to call the function again keeping in mind that some data were consumed or read adn we must restart from the right point. In our example only the write(2) must be properly restarted, we use thewritten
variable to track how many bytes were actually written and properly call write(2) again if there are data left in the buffer.
Remember that not all system calls behave exactly the same way, consult their manual page to make sure.
Reading the sigaction(2) manual page you can think that setting the SA_RESTART
flag is simpler that handling system call interruption. The documentation says that setting it will make certain system calls automatically restartable across signals. It's not specified which calls are restarted. This flag is mainly used for compatibility with older systems, don't use it.
Comments
Hello, Thank you for good
I don't think it's possible.
Restarting system calls
very useful and readable blog about Unix signals
Thanks
Ctrl+Z Signal
Hi, typo jerk here
i love type jerk!
Thanks. I don't consider it
I was looking for linux
Great write-up
Signals - That's not everything
Problem regarding signals.
Hi, because while handling