Intro to Signals
Objectives
We would like to :
- Learn the basics of signals.
Notes
- This is chapter 20.
- I know Michael wanted chapter 24, but it relies on chapters 20,21,22
- And it is also needed for the Daemon chapter.
- And this is good stuff.
- A signal is a notification to a process that an event has occurred
- Think of it as a software exception.
- They are delivered by the OS.
-
kill
is the way a user can ask for a signal to be sent to a process
- But in general you can only send signals to your process
- Unless you are root, then you can send a signal to any process.
-
kill -l
- Signal numbers are not uniform across all UNIX implementations.
- Signal numbers are not ranked
- Signal 2 is not stronger than signal 1.
- Signals occur when the problem occurs
- They can occur at any time.
- And this leads to all sorts of problems.
- But more of that later.
- When a signal is sent, it is described as pending.
- Some signals can be blocked, or not received until later.
- When a signal is received it can
- Ignored: IE thrown away
- Cause the process to terminate
- Cause the process to save a core dump file
- Cause the process to stop
- Cause the process to resume
- Some signals (like kill) can not be ignored or blocked.
- Section 20.2 provides a list of signals and their cause.
- 1 hup is a terminal disconnect.
- 9 kill is a termination signal
- 18 SIGCONT and 19-20 are for process control (stop, continue)
- This is forced, but here are some programs.
-
int kill(int pid, int sig)
- Delivers signal sig to process pid
- If you are allow to send that signal to that process.
- If pid = -1, it sends the signal to every process to which the program can send a signal.
- If sig = 0, no signal is sent, but permission is checked
- This is one way to check for process existence.
- See killer.cpp.
- Declaring a signal handler
- static void
- parameters depend on how it is called.
- just a int signal.
- int sig - the signal that caused the handler to be called.
- (optional) siginfo_t * si - a structure holding information about the signal
- (optional) void * ucontext - information about the context of the signal.
- Installing a signal handler
-
int sigaction(int signum,
const struct sigaction *_Nullable restrict act,
struct sigaction *_Nullable restrict oldact);
- signum : the signal nummber
- act - a structure that defines the action to take place
- restrict says that fields in this can not overlap
- sa_flags modify the behavior of the signal
- SA_SIGINFO says give me the three parameter version of the signal handler.
- SA_NOCLDWAIT and SA_NOCLDSTOP control the behavior of SIGCHILD
- SA_NOCLDWAIT - don't make my children zombies
- SA_NOCLDSTOP - don't tell me about the child change of state
- Many others.
- oldact is the old action that was registered.
- By default most signals lead to termination when not handled.