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!
Sending signals
Sending signal from keyboard
There are two special key combinations that can be used in a terminal to send a signal to the running application:
- CTRL-C - sends
SIGINTwhich default action is to terminate the application. - CTRL-\ - sends
SIGQUITwhich default action is to terminate the application dumping core. - CTRL-Z - sends
SIGSTOPthat suspends the program.
kill()
The simplest way to send a signal to the process is to use kill(2). It takes two arguments:
pid (PID of the process) and sig (the signal to send). Although the function has a simple interface it's worth to read the manual page because there are few more things we can do than just sending a signal to a process:- The
pidcan be 0, the signal will be sent to all processes in the process group. - The
pidcan be -1, the signal is sent to every process you have permission to send signals except init and system processes (you won't kill system threads). - The
pidcan be less than -1 to send signal to all processes in the process group whose ID is -pid. - You can check is a process exists sending signal 0. Nothing is really sent, but the kill(2) return value will be as if it sent a signal, so if it's OK it means that the process exists.
Sending signals to yourself
There are two standard function that will help you to send signals to yourself:
- raise(3) - Just send the specified signal to yourself, but if it's a multithreaded program it sends the signal to the thread, not the process.
- abort(3) - Sends
SIGABRT, but before that it will unblock this signal, so this function works always, you don't need to bother about unblocking this signal. It will also terminates you program even if you have handler forSIGABRTby restoring the default signal handler and sending the signal again. You can prevent it as was mentioned in signal handling chapter.
Sending data along with signal - sigqueue()
The sigqueue(2) function works very similar to kill(2) but is has a third argument of type
const union sigval which can be used to send an integer value or a pointer that can be read in the signal handler if it reads the siginfo_t argument. If you use this function instead of 32) the handler can distinguish this with the si_code field because it will have SI_QUEUE value.


Hi, typo jerk here
Thanks. I don't consider it
I was looking for linux
Great write-up
Signals - That's not everything