IPC:signal basics (2)

Signal actions

three types of actions:

  • SIG_DFL,
    • for most signals, kill the process.
    • SIGSTOP: stop the process
    • SIGCONT: continue the program.
    • SIGCHLD, SIGIO, SIGURG, SIGWINCH: ignore the signal.
  • SIG_IGN,
    • 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.

Related functions

  • 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

  1. What you can do:
    • Return
    • Call exit() or abort() to terminate the program
    • Call longjmp(), or siglongjmp().
  2. After return from the signal handler, the receiving process resumes execution at the point where it was interrupted.
  3. 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.
  4. 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..

Signal Mask

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”.

Or sigprocmask()

Generate a signal
1) int raise (int sig) == pthread_kill(pthread-self(), sig)

2) pthread_kill(thread, int sig)

3)kill(pid, sig)

Manipulate a signal set

sigfillset(*set): initialize set to contain all signals.


Sigaddset(*set, signo)

Sigdelset(*set, signo)

sigismember(*set, signo)

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)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s