Linux- signal Introduction

Signal in Linux

In Linux, the signal is an interprocess communication mechanism, in this multi-user multi-process Linux system, there are signals it is inevitable. Signal is a software interrupt, which provides a method for processing asynchronous events, and only asynchronous communication between processes. According to the POSIX standard signaling mechanism after expansion, a signal not only can be used to inform the process of what happens to an event, you can also pass data to process.
Linux are supported in the signal to SIGbegin with.

Signal Source

Source of the signal can be many ways, in accordance with the conditions produced different hardware and software can be divided into two ways.
1. hardware

  • User Key generation: when the user presses certain keys on the terminal, the generated signal. After such Press <Ctrl + C> SIGINT signal to produce a key combination, the default is to terminate the process.
  • Hardware detects an abnormality occurs: hardware (such as CPU) detects some error, and notifies the Linux operating system kernel, and the kernel generates a corresponding signal, and transmits a signal to the program when the event is running. For example, storage access and other division by zero, invalid error.

2. Software way

  • User calls: the user calls at the terminal killcommand to send any signal to the process.
  • Process calls: calling process killor sigqueuefunction can send a signal to other processes.
  • Timer Send: sends a signal when detecting certain software conditions have been met, such as a alarmor settimertimer timeout will generate signal SIGALRM.

The type of signal

In a terminal kill -lall signals supported Linux system can be displayed.

  1. SIGHUP
    This signal is sent in a user terminal connected to the end (normal or abnormal), typically at the end of the process control terminal, notifies each of the session in the same job, they are no longer associated with this case.
    When you log Linux, the system will assign the user to a login terminal (Session). In this procedure all operation of the terminal, and comprising a front group, generally belong to this Session. When the user exits Linux login, foreground and background processes have on the output of the terminal will receive a SIGHUP signal. The default action of this signal is to terminate the process, so the foreground and background processes have terminal output will be suspended. But it can capture the signal, such as wget can capture SIGHUP signal and ignore it, so even out of the Linux login, wget can continue to download.
    In addition, from the relationship of the terminal, this signal for informing it to re-read the configuration file.
  2. SIGINT
    program termination (interrupt) signal is sent when the user types the INTR character (usually Ctrl-C), is used to notify the foreground process group to terminate the process.
  3. SIGQUIT
    and SIGINT similar, but controlled by the QUIT character (usually Ctrl-). In the process due to receipt of producing a core file SIGQUIT exit, similar to a program error signal in this sense.
  4. SIGILL
    has performed an illegal instruction error usually occurs because of itself, or trying to perform. It is also possible when generating the signal.
  5. SIGTRAP
    generated by the breakpoint instruction trap or other instructions used by the Debugger.
  6. SIGABRT
    call signal generated.
  7. SIGBUS
    illegal address, including alignment (Alignment) error. Such as access to a four integer multiple of 4 but not its address. It differs from SIGSEGV is that the latter is due to the unauthorized access to the memory address of a legitimate trigger (such as read-only access or do not own).
  8. SIGFPE
    issued when a fatal error occurs. Includes not only wrong, but also including the overflow and divide by zero all the other arithmetic and other errors.
  9. SIGKILL
    for an immediate end to run the program. This signal can not be blocked, treatment and neglect. If the administrator can not find a process to terminate, you can try to send this signal.
  10. SIGUSR1
    left to the user to use
  11. SIGSEGV
    trying to access its own memory not allocated to, or attempt to not have write access to write data.
  12. SIGUSR2
    left to the user to use
  13. SIGPIPE
    pipe rupture. This signal is usually generated, such as the use of two processes FIFO (pipe) to communicate, read the pipeline did not go out into the open or terminated unexpectedly pipe write, write process will receive a SIGPIPE signal. Also with two processes Socket Communications, the writing process when writing the Socket, the reading process has been terminated.
  14. SIGALRM
    clock, the calculated time or the actual clock time. Alarm function using the signal.
  15. SIGTERM
    program end (Terminate) signal, and SIGKILL except that the signal can be blocked and processed. Often used to ask an application exits normally kill command to generate the signal. If the process is not terminated, we will try SIGKILL.
  16. SIGCHLD
    At the end of the child process will receive the signal.
    If you do not deal with this signal, there is no wait (wait) the child, although the child process to terminate, but also possession of entries in the kernel process table, then called the child process. In this case we should avoid (or ignore SIGCHILD signal, or capturing it, or wait it derived the child, or parent process to terminate, then the child automatically terminate the process of taking over the origin).
  17. SIGCONT
    make a stop (stopped) process continues. This signal can not be blocked. May use a handler to let the program accomplish a specific task when stopped by the state to continue. For example, the prompt is displayed again
  18. SIGSTOP
    . Stop (stopped) process and the implementation of its attention and interrupt the difference terminate: the process is not over, just suspend the implementation of this signal can not be blocked, processed or ignored.
  19. SIGTSTP
    stop running process, but the signal can be processed and ignored. This signal is sent when the user types the SUSP character (usually Ctrl-Z)
  20. SIGTTIN
    when a background job from the user terminal to read data, all processes in the job will receive SIGTTIN signal. These processes will be suspended.
  21. SIGTTOU
    similar SIGTTIN, but received at the write terminal (terminal mode or modified).
  22. SIGURG
    the "urgent" data or generating out-of-band data arrives.
  23. SIGXCPU
    than CPU time limit. This limit can be read / changed by / setrlimit.
  24. SIGXFSZ
    when the process attempted to expand the file that exceeds the limit large source files.
  25. SIGVTALRM
    virtual Similar SIGALRM, but the process is calculated CPU time.
  26. SIGPROF
    similar SIGALRM / SIGVTALRM, but including CPU time used and the process of time.
  27. SIGWINCH
    is issued to change the window size.
  28. SIGIO
    ready to start input / output operations.
  29. SIGPWR
    Power failure
  30. SIGSYS
    illegal.

In the signal listed above, the program can not be captured, blocked or ignored signals are: SIGKILL, SIGSTOP

Not return to the default action of the signal have:
SIGILL, SIGTRAP

The default would cause the process to have abortion signal: SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGIOT, SIGQUIT, SIGSEGV, SIGTRAP, SIGXCPU, SIGXFSZ

The default would cause the process to exit signal there: SIGALRM, SIGHUP, SIGINT, SIGKILL, SIGPIPE, SIGPOLL, SIGPROF, SIGSYS, SIGTERM, SIGUSR1, SIGUSR2, SIGVTALRM

The default would cause the process to stop the signal has: SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU

The default process ignores the signal has: SIGCHLD, SIGPWR, SIGURG, SIGWINCH

In addition, SIGIO is at exits in 4.3BSD is ignored; SIGCONT suspended when the process is to continue, otherwise it is ignored and can not be blocked

Reliability of the signal

Signal into a reliable signal and a signal is not reliable, No. 31 to No. 1 SIGHUP all signals between the signal SIGSYS inherit the UNIX system, is not reliable signal; SIGRTMAX between No. 64 to No. 33 SIGRTMIN are a reliable signal (also known as real-time signal).
Linux system, the reliability of the signal means that if the signal is lost, or if the signal line up support, to do something to explain this below:

We must first understand the process of generation and transmission signal
after signal is generated when the event occurs, the kernel generates a signal, the signal is generated, the kernel will set up some kind of mark in the process table. When the kernel sets this flag, we say that the kernel sends a signal to the process. Signal generation and transmission time interval is called the signal pending (pending)
and then to clear the blocking signal
if a signal to a process blocked, then the next if this signal is generated again, the process of this action signals that capture the signal (ie not ignore signals), the kernel will process the captured signal is set to a pending status until the process signal to unblock this.
To learn then unblocked after the
former if a signal to unblock the process, this signal has occurred many times, if the signal is passed several times, which means that the signal is queued in the pending signal queue inside, then released after blocking performs signal, this signal line is reliable signal in accordance with the order. However, if the signal is passed only once (to produce several times, passing once), claimed that this signal is not reliable signal.

Sentence summary is this: when the signal blocking, repeatedly received under the premise of the signal, when the unblocking signal is a reliable signal of each signal will respond; receive many times, only once the signal response is not reliable signal.

Process to respond to signals

When a signal occurs, the user may be required to process in one of the following methods to respond to the signals:

  • Capture signal: the process will capture the signal, and the signal can be specified signal processing functions, automatically performs the signal processing functions after receiving a similar break.
  • Ignore signals: Most signals can be processed using this method, but maybe SIGKILL and SIGSTOP signals can not be ignored, and maybe a signal can not be captured and obstruction.
  • Default processing: the default action is to terminate the process most of the signal, so a reliable signal (real-time signal) default action is to terminate the process.
Published 62 original articles · won praise 188 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43743762/article/details/101306704