Unix signal

[toc]

Signals are a limited form of inter-process communication used in Unix, Unix-like, and other POSIX-compliant operating systems. A signal is an asynchronous notification sent to a process or to a specific thread within the same process in order to notify it of an event that occurred.

When a signal is sent, the operating system interrupts the target process' normal flow of execution to deliver the signal. Execution can be interrupted during any non-atomic instruction. If the process has previously registered a signal handler, that routine is executed. Otherwise, the default signal handler is executed.

lmz: 信号是为了进程间通信的。信号发给某一个进程后,操作系统会中断目标进程的正常执行,此信号将会被进程的handler(default or register)处理。

Sending signals

Exceptions such as division by zero or a segmentation violation will generate signals (here, SIGFPE "floating point exception" and SIGSEGV "segmentation violation" respectively, which both by default cause a core dump and a program exit).

The kernel can generate signals to notify processes of events. For example, SIGPIPE will be generated when a process writes to a pipe which has been closed by the reader; by default, this causes the process to terminate, which is convenient when constructing shell pipelines.

Typing certain key combinations at the controlling terminal of a running process causes the system to send it certain signals:

  • Ctrl-C (in older Unixes, DEL) sends an INT signal ("interrupt", SIGINT); by default, this causes the process to terminate.
  • Ctrl-Z sends a TSTP signal ("terminal stop", SIGTSTP); by default, this causes the process to suspend execution.
  • Ctrl-\ sends a QUIT signal (SIGQUIT); by default, this causes the process to terminate and dump core.
  • Ctrl-T (not supported on all UNIXes) sends an INFO signal (SIGINFO); by default, and if supported by the command, this causes the operating system to show information about the running command.

lmz: 给目标进程发送信号可以通过命令行也可以通过组合键。

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.

lmz:

  • 信号发给目标进程后,进程如何响应?可以自己注册handler处理,也可以使用默认的handler处理。自己注册的优先级高。但是有些信号是不能被注册的handler所拦截的。

tags:wikipedia

results matching ""

    No results matching ""