three types of actions:
- for most signals, kill the process.
- SIGSTOP: stop the process
- SIGCONT: continue the program.
- SIGCHLD, SIGIO, SIGURG, SIGWINCH: ignore the signal.
- discard all pending signals, whether it is blocked or not. the new signals are discarded, too.
- or a pointer to a function (signal handler)
- the function is called in a manner equivalent to the code sequence of : signal (sig_no, SIG_DFL); (*func)(sig_no);
- Initially, all signals are set to SIG_DFL or SIG_IGN, prior to entry of main() routine.
- The default action for most signals is to terminate the process.
- int sigaction (int sig, const struct sigaction *act, struct sigaction *oact) examine or specify the action associated with a signal.
- void *signal (int sig, void (*func)(int)) specify an action to take place when the process is hit by the signal.
- signal() will call sigaction().
More about signal handler
- What you can do:
- Call exit() or abort() to terminate the program
- Call longjmp(), or siglongjmp().
- After return from the signal handler, the receiving process resumes execution at the point where it was interrupted.
- signal handlers are invoked asynchronously with process execution, so you should be taken into account the same sort of things that you would in a multithread environment, when inspecting or manipulating shared resources.
- When a signal handler is invoked, the responsible signal is masked before its handler is called. If the handler returns normally, OS restores the signal mask. Any signal mask change using pthread_sigmask() in the handler are undone..
pthread_sigmask(int how, const sigset_t* set, sigset_t oset) exmain or/and change the calling thread’s signal mask.
SIG_BLOCK: block the “set”, together with the current signal mask.
SIG_UNBLOCK: unblock the “set”.
SIG_SETMASK: block only the “set”.
Generate a signal
1) int raise (int sig) == pthread_kill(pthread-self(), sig)
2) pthread_kill(thread, int sig)
Manipulate a signal set
sigfillset(*set): initialize set to contain all signals.
Wait for a signal
1) sigwait(*set, *signo)
returns 0 if success, EINTR, EINVAL,, etc, otherwise.
Note: the signals defined by “set” must be blocked before you call sigwait(). otherwise, the behavior is undefined.
2) sigwaitinfo(*set, *info). blocks until the specified signal (defined in *set) is pending. If already pending, returns immediately.
returns: a signal number, or -1 (with errno set).
3) sigtimedwait(*set, *info, const sruct timespec *timeout)