Skip to content

signal(7)

DESCRIPTION

Linux supports both POSIX reliable signals (hereinafter "standard signals") and POSIX real-time signals.

Signal dispositions

NOTE: "disposition"是"处置、处理"的意思

Each signal has a current disposition, which determines how the process behaves when it is delivered the signal.

The entries in the "Action" column of the table below specify the default disposition for each signal, as follows:

Term Default action is to terminate the process.
Ign Default action is to ignore the signal
Core Default action is to terminate the process and dump core (see core(5))
Stop Default action is to stop the process.
Cont Default action is to continue the process if it is currently stopped

NOTE: term VS stop?被stop的process是可以再continue的;

A process can change the disposition of a signal using sigaction(2) or signal(2). (The latter is less portable when establishing a signal handler; see signal(2) for details.) Using these system calls, a process can elect one of the following behaviors to occur on delivery of the signal:

1、perform the default action;

2、ignore the signal;

3、catch the signal with a signal handler, a programmer-defined function that is automatically invoked when the signal is delivered.

By default, a signal handler is invoked on the normal process stack. It is possible to arrange that the signal handler uses an alternate stack; see sigaltstack(2) for a discussion of how to do this and when it might be useful.

NOTE:

sigaltstack(2) custom stack

The signal disposition is a per-process attribute: in a multithreaded application, the disposition of a particular signal is the same for all threads.

tag-thread share公共有signal disposition is a per-process attribute

A child created via fork(2) inherits a copy of its parent's signal dispositions. During an execve(2), the dispositions of handled signals are reset to the default; the dispositions of ignored signals are left unchanged.

Sending a signal

NOTE:

如何发送一个signal

The following system calls and library functions allow the caller to send a signal:

system call
raise(3) Sends a signal to the calling thread.
kill(2) Sends a signal to a specified process, to all members of a specified process group, or to all processes on the system.
killpg(3) Sends a signal to all of the members of a specified process group.
pthread_kill(3) Sends a signal to a specified POSIX thread in the same process as the caller.
tgkill(2) Sends a signal to a specified thread within a specific process. (This is the system call used to implement pthread_kill(3).)
sigqueue(3) Sends a real-time signal with accompanying data to a specified process.

Waiting for a signal to be caught

The following system calls suspend execution of the calling thread until a signal is caught (or an unhandled signal terminates the process):

system call
pause(2) Suspends execution until any signal is caught.
sigsuspend(2) Temporarily changes the signal mask (see below) and suspends execution until one of the unmasked signals is caught.

Synchronously accepting a signal

tag-Async to sync-blocking-等待-synchronously accepting a signal

Rather than asynchronously catching a signal via a signal handler, it is possible to synchronously accept the signal, that is, to block execution until the signal is delivered, at which point the kernel returns information about the signal to the caller. There are two general ways to do this:

1、sigwaitinfo(2), sigtimedwait(2), and sigwait(3) suspend execution until one of the signals in a specified set is delivered. Each of these calls returns information about the delivered signal.

2、signalfd(2) returns a file descriptor that can be used to read information about signals that are delivered to the caller. Each read(2) from this file descriptor blocks until one of the signals in the set specified in the signalfd(2) call is delivered to the caller. The buffer returned by read(2) contains a structure describing the signal.

tag-notify via fd通过文件来通知-Self-Pipe Trick-signalfd

Signal mask and pending signals

A signal may be blocked, which means that it will not be delivered until it is later unblocked. Between the time when it is generated and when it is delivered a signal is said to be pending.

per-thread signal mask

Each thread in a process has an independent signal mask, which indicates the set of signals that the thread is currently blocking. A thread can manipulate its signal mask using pthread_sigmask(3). In a traditional single-threaded application, sigprocmask(2) can be used to manipulate the signal mask.

A child created via fork(2) inherits a copy of its parent's signal mask; the signal mask is preserved across execve(2).

NOTE:

这和signal disposition是不同的

Signal direction

A signal may be generated (and thus pending) for a process as a whole (e.g., when sent using kill(2)) or for a specific thread (e.g., certain signals, such as SIGSEGV and SIGFPE, generated as a consequence of executing a specific machine-language instruction are thread directed, as are signals targeted at a specific thread using pthread_kill(3)). A process-directed signal may be delivered to any one of the threads that does not currently have the signal blocked. If more than one of the threads has the signal unblocked, then the kernel chooses an arbitrary thread to which to deliver the signal.

NOTE: process-directed signal的一个典型代表就是SIGALRM,根据APUE12.8节的介绍:闹钟定时器是进程资源,并且所有的线程共享相同的闹钟。所以,进程中的多个线程不可能互不干扰地使用闹钟定时器。参见redis中的SIGALARM

A thread can obtain the set of signals that it currently has pending using sigpending(2). This set will consist of the union of the set of pending process-directed signals and the set of signals pending for the calling thread.

A child created via fork(2) initially has an empty pending signal set; the pending signal set is preserved across an execve(2).

Execution of signal handlers

NOTE:

未读

Standard signals

NOTE:

未读

Queueing and delivery semantics for standard signals

Signal numbering for standard signals

Real-time signals

Interruption of system calls and library functions by signal handlers

NOTE:

在 "APUE 10.5 Interrupted System Calls" 中对此也有说明

If a signal handler is invoked while a system call or library function call is blocked, then either:

1、the call is automatically restarted after the signal handler returns; or

2、the call fails with the error EINTR.

Which of these two behaviors occurs depends on the interface and whether or not the signal handler was established using the SA_RESTART flag (see sigaction(2)). The details vary across UNIX systems; below, the details for Linux.

If a blocked call to one of the following interfaces is interrupted by a signal handler, then the call is automatically restarted after the signal handler returns if the SA_RESTART flag was used; otherwise the call fails with the error EINTR:

NOTE:

重点在于 "may block for an indefinite time(无限时间)",前提条件之一是:

"blocked call",因此system call必须是blocked。

下面是回避"may block for an indefinite time(无限时间)"的方式:

1、non-blocking IO

需要注意的是,设置timeout,并不能够阻止

1、read(2), readv(2), write(2), writev(2), and ioctl(2) calls on "slow" devices.

A "slow" device is one where the I/O call may block for an indefinite time(无限时间), for example, a terminal, pipe, or socket. If an I/O call on a slow device has already transferred some data by the time it is interrupted by a signal handler, then the call will return a success status (normally, the number of bytes transferred). Note that a (local) disk is not a slow device according to this definition; I/O operations on disk devices are not interrupted by signals.

NOTE:

If an I/O call on a slow device has already transferred some data by the time it is interrupted by a signal handler, then the call will return a success status (normally, the number of bytes transferred).对于这种情况要如何处理呢?

2、open(2), if it can block (e.g., when opening a FIFO; see fifo(7)).

NOTE:

3、wait(2), wait3(2), wait4(2), waitid(2), and waitpid(2).

NOTE:

这是肯定会block

4、Socket interfaces: accept(2), connect(2), recv(2), recvfrom(2), recvmmsg(2), recvmsg(2), send(2), sendto(2), and sendmsg(2), unless a timeout has been set on the socket (see below).

NOTE:

这段话的意思是: 如果设置了"timeout",则

5、File locking interfaces: flock(2) and the F_SETLKW and F_OFD_SETLKW operations of fcntl(2)

6、POSIX message queue interfaces: mq_receive(3), mq_timedreceive(3),mq_send(3), and mq_timedsend(3).

7、futex(2) FUTEX_WAIT (since Linux 2.6.22; beforehand, always failed with EINTR).

8、getrandom(2).

9、pthread_mutex_lock(3), pthread_cond_wait(3), and related APIs.

10、futex(2) FUTEX_WAIT_BITSET.

11、POSIX semaphore interfaces: sem_wait(3) and sem_timedwait(3) (since Linux 2.6.22; beforehand, always failed with EINTR).

12、read(2) from an inotify(7) file descriptor (since Linux 3.8; beforehand, always failed with EINTR).

The following interfaces are never restarted after being interrupted by a signal handler, regardless of the use of SA_RESTART; they always fail with the error EINTR when interrupted by a signal handler:

NOTE:

下面的这些system call总是"fail with the error EINTR when interrupted by a signal handler"

1、"Input" socket interfaces, when a timeout (SO_RCVTIMEO) has been set on the socket using setsockopt(2): accept(2), recv(2), recvfrom(2), recvmmsg(2) (also with a non-NULL timeout argument), and recvmsg(2).

NOTE:

前提是设置了timeout

2、"Output" socket interfaces, when a timeout (SO_RCVTIMEO) has been set on the socket using setsockopt(2): connect(2), send(2), sendto(2), and sendmsg(2).

3、Interfaces used to wait for signals: pause(2), sigsuspend(2), sigtimedwait(2), and sigwaitinfo(2).

4、File descriptor multiplexing interfaces: epoll_wait(2), epoll_pwait(2), poll(2), ppoll(2), select(2), and pselect(2).

5、System V IPC interfaces: msgrcv(2), msgsnd(2), semop(2), and semtimedop(2).

6、Sleep interfaces: clock_nanosleep(2), nanosleep(2), and usleep(3).

7、io_getevents(2).

The sleep(3) function is also never restarted if interrupted by a handler, but gives a success return: the number of seconds remaining to sleep.

Interruption of system calls and library functions by stop signals

On Linux, even in the absence of signal handlers, certain blocking interfaces can fail with the error EINTR after the process is stopped by one of the stop signals and then resumed via SIGCONT. This behavior is not sanctioned(制裁) by POSIX.1, and doesn't occur on other systems.