Signals
Notes
- This is a bit of a side trip, but not really
- our next topic is interprocess communications
- And this is a very primitive form
- Probably not intended for that purpose, but it can be used as such
- And it ties into interrupts very well.
- My primary reference is Kerrisk, The Linux Programming Interface
- Signals are notifications to a process that an event has occurred
- These may be a result of a hardware exception
- Or a user action such as ctrl-z
- Or a software error, or action, or event (child process terminating generates a signal)
- Signals can be used for process synchronization.
- While processes with permission can send signals to each other
- the kernel is the primary source of signals
- And signals go through the kernel.
-
kill is the primary command to send signals to processes
- Signals, like interrupts, are a small integer
-
kill -l lists signals.
- These are not all standardized
- The lower ones are but higher ones are not.
- (9) or SIGKILL is standard
- (64) SIGRTMAX, whatever that is, is not.
- There is no level of intensity
- 8 ( SIGFPE) is not just a little weaker than 9 (SIGKILL)
- The lifetime of a signal
- Something generates the signal
- It is then delivered to the process
- It may be
- caught and handled or action taken on it
- It possibly could be blocked, in which case it is pending
- Or it could be ignored, in which case the kernel discards it.
- Some signals can not be blocked, ignored or caught.
- Some important signals to us right now
- SIGKILL causes a process to terminate, it can not be blocked, caught or ignored
- SIGABRT causes a process to terminate and dump core
- SIGCHLD - one of the process' children has terminated or been stopped or continued
- SIGSTOP- causes the process to stop, can not be blocked, caught or ignored
- SIGCONT - causes a stopped process to continue
- SIGALRM - a real time timer has expired
- In programs some signals have
- signal handlers - or routines that are called when a signal is caught
- signal mask - or a indicator of what signals to catch, block and ignore.
- There are two ways to catch a signal in *nix
- Needs
#include <signal.h>
- And
typedef typeof (void(int)) *sighandler_t;
- We will use this one as it is simpler
int sigaction(int signum,
const struct sigaction *_Nullable restrict act,
struct sigaction *_Nullable restrict oldact);"
- This is superior and far more portable.
A process can send a signal with the kill call
-
int kill(pid_t pid, int sig)
- You need
#include <signal.h>
- Different rules for 0 and negative.
You can send a signal to yourself with int raise(int sig)
See thrower.cpp