Unix信号
此條目目前正依照其他维基百科上的内容进行翻译。 (2009年11月24日) |
在计算机科学中,信号是Unix、类Unix以及其他POSIX兼容的操作系统中进程间通讯的一种有限制的方式。它是一种异步的通知机制,用来提醒进程一个事件已经发生。当一个信号发送给一个进程,操作系统中断了进程正常的控制流程,此时,任何非原子操作都将被中断。如果进程定义了信号的处理函数,那么它将被执行,否则就执行默认的处理函数。
发送信号
- 在一个运行的程序的控制终端键入特定的组合键可以向它发送某些信号:
- kill(2)系统调用会在权限允许的情况下向进程发送特定的信号,类似地,kill(1)命令允许用户向进程发送信号。
raise(3)
库函数可以将特定信号发送给当前进程。 - 像除数为零、断错误这些异常也会产生信号(这里分别是SIGFPE和SIGSEGV,默认都会导致进程终止和核心转储).
- 内核可以向进程发送信号以告知它一个事件发生了。例如当进程将数据写入一个已经被关闭的管道是将会收到SIGPIPE信号,默认情况下会使进程关闭。
Handling signals
Signal handlers can be installed with the signal()
system call. If a signal handler is not installed for a particular signal, the default handler is used. Otherwise the signal is intercepted and the signal handler is invoked. The process can also specify two default behaviors, without creating a handler: ignore the signal (SIG_IGN) and use the default signal handler (SIG_DFL). There are two signals which cannot be intercepted and handled: SIGKILL and SIGSTOP.
Risks
Signal handling is vulnerable to race conditions. Because signals are asynchronous, another signal (even of the same type) can be delivered to the process during execution of the signal handling routine. The sigprocmask()
call can be used to block and unblock delivery of signals.
Signals can cause the interruption of a system call in progress, leaving it to the application to manage a non-transparent restart.
Signal handlers should be written in a way that doesn't result in any unwanted side-effects, e.g. errno alteration, signal mask alteration, signal disposition change, and other global process attribute changes. Use of non-reentrant functions, e.g. malloc or printf, inside signal handlers is also unsafe.
Relationship with Hardware Exceptions
A process's execution may result in the generation of a hardware exception, for instance, if the process attempts to divide by zero or incurs a TLB miss. In Unix-like operating systems, this event automatically changes the processor context to start executing a kernel exception handler. With some exceptions, such as a page fault, the kernel has sufficient information to fully handle the event and resume the process's execution. In other exceptions, however, the kernel cannot proceed intelligently and must instead defer the exception handling operation to the faulting process. This deferral is achieved via the signal mechanism, wherein the kernel sends to the process a signal corresponding to the current exception. For example, if a process attempted to divide by zero on an x86 CPU, a divide error exception would be generated and cause the kernel to send the SIGFPE signal to the process. Similarly, if the process attempted to access a memory address outside of its virtual address space, the kernel would notify the process of this violation via a SIGSEGV signal. The exact mapping between signal names and exceptions is obviously dependent upon the CPU, since exception types differ between architectures.
List of signals
The Single Unix Specification specifies the following signals which are defined in <signal.h>:
Signal | Description |
---|---|
SIGABRT | Process aborted |
SIGALRM | Signal raised by alarm |
SIGBUS | Bus error: "access to undefined portion of memory object" |
SIGCHLD | Child process terminated, stopped (or continued*) |
SIGCONT | Continue if stopped |
SIGFPE | Floating point exception: "erroneous arithmetic operation" |
SIGHUP | Hangup |
SIGILL | Illegal instruction |
SIGINT | Interrupt |
SIGKILL | Kill (terminate immediately) |
SIGPIPE | Write to pipe with no one reading |
SIGQUIT | Quit and dump core |
SIGSEGV | Segmentation violation |
SIGSTOP | Stop executing temporarily |
SIGTERM | Termination (request to terminate) |
SIGTSTP | Terminal stop signal |
SIGTTIN | Background process attempting to read from tty ("in") |
SIGTTOU | Background process attempting to write to tty ("out") |
SIGUSR1 | User-defined 1 |
SIGUSR2 | User-defined 2 |
SIGPOLL | Pollable event |
SIGPROF | Profiling timer expired |
SIGSYS | Bad syscall |
SIGTRAP | Trace/breakpoint trap |
SIGURG | Urgent data available on socket |
SIGVTALRM | Signal raised by timer counting virtual time: "virtual timer expired" |
SIGXCPU | CPU time limit exceeded |
SIGXFSZ | File size limit exceeded |
Note: Where a section is marked by an asterisk, this denotes an X/Open System Interfaces (XSI) extension. Wording in quotes appended with (SUS) denotes the wording from the SUS[1].
See also
<signal.h>
External links
- Introduction to Unix Signals Programming
- Another Introduction to Unix Signals Programming
- UNIX and Reliable POSIX Signals by Baris Simsek
- Signal Handlers by Henning Brauer