Linux signal, signal processing and signal processing functions

Source: Internet
Author: User
Tags arithmetic function prototype posix sigint signal signal handler

A signal (signal) is a software interrupt that provides a way to handle asynchronous events and is the only asynchronous communication between processes. In a Linux system, the subsequent signaling mechanism is extended according to the POSIX standard, which can be used to inform not only what happened to a program, but also the process of passing data to it.

First, the source of the signal

The source of the signal can have many kinds of test, according to the different conditions can be divided into hardware and software two.

1. Hardware mode

When a user presses a key on the terminal, a signal is generated. A SIGINT signal is generated when a combination of keys is pressed.

Hardware exceptions generate signals: Except for data, invalid storage access, and so on. These events are typically detected by hardware (such as CPU) and notified to the Linux operating system kernel, which then generates the appropriate signal and sends the signal to the program that is in progress at the time the event occurred.

2. Software mode

The user sends a task signal to the process by using the KILL command at the terminal.

The process calls the kill or Sigqueue function to send a signal.

A signal is signaled when a software condition is detected, such as when a timer set by alarm or settimer times out, a SIGALRM signal is generated.

Ii. Types of Signals

Enter kill–l under the shell to display all the dependencies supported by the Linux system, with the following list of signals:

1) SIGHUP 2) SIGINT 3) Sigquit 4) Sigill
5) SIGTRAP 6) SIGABRT 7) Sigbus 8) SIGFPE
9) SIGKILL) SIGUSR1 SIGSEGV) SIGUSR2
Sigpipe) sigalrm SIGTERM) Sigstkflt
SIGCHLD) Sigcont SIGSTOP) SIGTSTP
) (Sigttin) Sigttou () Sigurg) sigxcpu
SIGXFSZ) sigvtalrm sigprof) sigwinch
SIGIO) SIGPWR Sigsys) sigrtmin
(sigrtmin+1) (sigrtmin+2) sigrtmin+3) sigrtmin+4
sigrtmin+5) sigrtmin+6 sigrtmin+7) sigrtmin+8
sigrtmin+9) (sigrtmin+10) sigrtmin+11) sigrtmin+12
sigrtmin+13) (sigrtmin+14) sigrtmin+15) SIGRTMAX-14
SIGRTMAX-13) SIGRTMAX-12-SIGRTMAX-11) SIGRTMAX-10
SIGRTMAX-9) SIGRTMAX-8 () SIGRTMAX-7) SIGRTMAX-6
SIGRTMAX-5) SIGRTMAX-4 () SIGRTMAX-3) SIGRTMAX-2
SIGRTMAX-1) Sigrtmax

The value of the signal is defined in signal.h and there are no 16 and 32 signals in Linux. The above signal has the following meanings:

(1) SIGHUP: When the user exits the shell, all processes that are sent by the shell are returned to this signal, and the default action is to terminate the process.

(2) SIGINT: When the user presses the key combination, the client sends this signal to the running program that is started by the terminal. The default action is to terminate the process.

(3) Sigquit: When the user presses the key combination to generate the signal, the user terminal sends this signal to the running program initiated by the terminal. The default action is to terminate the process and produce a core file.

(4) Sigill:cpu detects that a process has executed an illegal instruction. The default action is to terminate the process and produce a core file.

(5) SIGTRAP: This signal is generated by a breakpoint instruction or other trap instruction. The default action is to terminate the process and produce a core file.

(6) SIGABRT: This signal is generated when the abort function is called. The default action is to terminate the process and produce a core file.

(7) Sigbus: Illegal access to memory address, including memory address alignment (alignment) error, the default action is to terminate the process and produce a core file.

(8) SIGFPE: Generated when a fatal arithmetic error occurs. This includes not only floating-point run errors, but also all arithmetic errors such as overflow and divisor 0. The default action is to terminate the process and produce a core file.

(9) SIGKILL: Unconditionally terminate the process. This signal cannot be ignored, processed, or blocked. The default action is to terminate the process. It provides the system administrator with a way to kill any process.

(e) SIGUSR1: A user-defined signal that the program can define and use in the program. The default action is to terminate the process.

(one) SIGSEGV: Indicates that the process has made invalid memory access. The default action is to terminate the process and use the signal. The default action is to terminate the process.

SIGUSR2: This is another user-defined signal that the programmer can define and use in the program. The default action is to terminate the process.

Sigpipe:broken Pipe: Writes data to a pipe that has no read end. The default action is to terminate the process.

SIGALRM: Timer Timeout, timeout time is set by system call alarm. The default action is to terminate the process.

SIGTERM: End of program (terminate) signal, unlike Sigkill, the signal can be blocked and processed. Usually used to require the program to exit normally. This signal is missing when executing the shell command kill. The default action is to terminate the process.

SIGCHLD: The parent process will receive this signal at the end of the subroutine. The default action is to ignore the signal.

Sigcont: Let a paused process continue execution.

SIGSTOP: Stop (Stopped) the execution of the process. Notice the difference between it and Sigterm and SIGINT: The process is not over yet, just pause execution. This signal cannot be ignored, processed, or blocked. The default is to pause a process.

SIGTSTP: Stops the action of the process, but the signal can be processed and ignored. This signal is emitted when a combination of keys is pressed. The default action is to pause the process.

Sigttin: When a background process is going to read data from a user terminal, all processes in that terminal receive a sigttin signal. The default action is to pause the process.

Sigttou: This signal is similar to Sigtin, which is generated when the background process is going to output data to the terminal. The default action is to pause the process.

Sigurg: When there is an emergency data on the socket (socket), this signal is sent to the currently running process, which reports the arrival of emergency data. The default action is to ignore the signal.

SIGXCPU: The process execution time exceeds the CPU time allocated to the process, and the system generates the signal and sends it to the process. The default action is to terminate the process.

Sigxfsz: exceeds the maximum file length limit. The default action is YL to terminate the process and produce the core file.

SIGVTALRM: This signal is generated when the virtual clock times out. Similar to SIGALRM, but it only calculates the CPU time that the process is useful for. The default action is to terminate the process.

Sigprof: Similar to SIGVTALRM, it includes not only the CPU time that the process consumes but also the time it takes to execute the system call. The default action is to terminate the process.

Sigwinch: Emitted when the window size changes. The default action is to ignore the signal.

SIGIO: This signal sends an asynchronous IO event to the process indication. The default action is ignore.

() SIGPWR: Shut down the machine. The default action is to terminate the process.

(sigrtmin~sigrtmax:linux) real-time signal, which does not have a fixed meaning (or can be freely used by the user). Note that the Linux threading mechanism uses the first 3 real-time signals. The default action for all real-time signals is to terminate the process.

1. Reliable signal and unreliable signal

In a Linux system, the reliability of a signal is whether the signal is lost, or whether the signal supports exclusion. The signals between SIGHUP (1) ~ Sigsys (31) are inherited from the Unix system and are unreliable signals. The Linux system defines the signals between Sigrtmin and Sigrtmax (64) According to the POSIX standard, both of which are reliable signals, also known as real-time signals.

The kernel generates a signal when the event that caused the signal has occurred. After the signal is generated, the kernel usually sets some form of flag in the process table. When the kernel sets this flag, we say that the kernel delivers a signal to a process. The time interval between signal generation (generate) and delivery (delivery), called Master Signal Pending (pending).

The process can call sigpending to block the signal, and if a blocking signal is generated for the process, and the action of the signal is to capture the signal (that is, the signal is not ignored), the kernel will remain in the pending state for that signal until the process is unblocked or the response to the signal is changed to ignore. If the signal is repeated several times before the process unlocks a signal, it is called a reliable signal if the signal is delivered multiple times (that is, the signal is queued inside the pending signal queue), and the signal that is delivered only once is called an unreliable signal.

2, the priority of the signal

The signal is essentially a soft interrupt, the interrupt has a priority, and the signal has a priority level. If a process has multiple pending signals, the kernel will deliver the signal in the order it was sent for the same pending real-time signal. If there are multiple pending signals, the smaller the value (or number) is delivered first. If there is an unreliable signal, and there is a reliable signal (real-time signal), although POSIX does not explicitly stipulate this situation, but the Linux system and most of the POSIX-compliant operating systems, the priority is to deliver unreliable signals.

Third, the process of the response to the signal

When a signal occurs, the user can request that the process respond to the signal in one of the following 3 ways.

1, capture the signal: for the signal to be captured, it can be assigned a signal processing function, the signal occurs when the function is automatically called, within the function to achieve the processing of the signal.

2, ignoring the signal: most of the signals can be processed in this way, but Sigkill and sigstop these two signals can not be ignored, and neither of these signals can be captured and blocked. In addition, the behavior of a process is unpredictable if some of the signals produced by a hardware exception are ignored, such as illegal storage access or by dividing by 0.

3, in accordance with the system default mode processing. The default action for most signals is to terminate the process, and the default action for all real-time signals is to terminate the process.

Four, the default processing of various signals

Signals that are not captured, blocked, or ignored by the program are: Sigkill,sigstop

Signals that cannot be restored to the default action are: Sigill,sigtrap

The default causes for process abortion are: SIGABRT, Sigbus, SIGFPE, Sigill, Sigiot, Sigquit, SIGSEGV, SIGTRAP, SIGXCPU, Sigxfsz

The default causes the process to exit with the following signals: SIGALRM, SIGHUP, SIGINT, SIGKILL, Sigpipe, Sigpoll, Sigprof, Sigsys, SIGTERM, SIGUSR1, SIGUSR2, SIGVTALRM

The default causes the process to stop with the following signals: SIGSTOP, SIGTSTP, Sigttin, Sigttou

The default process ignores signals are: SIGCHLD, SIGPWR, Sigurg, Sigwinch

Five, signal processing function and related structure

1. Signal Installation

(1), Signal ()
#include
void (*signal (int signum, void (*handler))) (int));
If the function prototype is not easy to understand, you can refer to the following decomposition methods to understand:
typedef void (*sighandler_t) (int);
sighandler_t signal (int signum, sighandler_t handler));
The first parameter specifies the value of the signal, the second parameter specifies the processing of the preceding signal value, which can be ignored (the parameter is set to Sig_ign), the signal can be processed by default (the parameter is set to SIG_DFL), or it can be handled by itself (parameter specifies a function address).
If the signal () call succeeds, returns the handler value of the last Call to signal () for the installation signal Signum, and the failure to return Sig_err.

(2), sigaction ()
#include
int sigaction (int signum,const struct sigaction *act,struct sigaction *oldact));

The Sigaction function is used to change the behavior of a process after it receives a specific signal. The first parameter of the function is the value of the signal and can be any specific valid signal except Sigkill and sigstop (defining its own processing function for both signals, which will result in a signal installation error). The second parameter is a pointer to an instance of the struct sigaction, in the case of the struct sigaction, specifies the processing of a particular signal, which can be null, the process defaults to the signal processing, and the third parameter oldact the object to hold the original processing of the corresponding signal. Oldact can be specified as null. If the second and third parameters are set to NULL, then the function can be used to check the validity of the signal.

2. Send Signal function

(1) int raise (int sig); Sends a specified signal to the current process

(2) int pause (void); Suspending a process waiting signal

(3) int Kill (pid_t pid,int SIG); Send signal via process number

(4) unsigned int alarm (unsigned int seconds); Specifies the time (in seconds) to send the SIGALRM signal. Seconds cancel all the set alarm requests at 0;

(5) Int sigqueue (pid_t pid,int sig,const Union sigval Val), similar to the Kill function, with the number of the shared Union Sigval form, the member int sival_int or void * in the common body The value of Sival_ptr is passed to the definition type in the signal handler function siginfo_t int si_int or void *si_ptr;

(6) int setitimer (int which,const struct itimerval *value,struct itimerval *oldvalue); The signal can be sent periodically, according to the which can be specified three types of signal: SIGALRM, SIGVTALRM and Sigprof, the action time is different from the which value, the member itimerval of struct it_interval define the interval, it_ Value is 0 o'clock, which invalidates the timer;

(7) void abort (void) will cause the process to terminate, unless the sigabort signal is captured;

3. Signal set and signal set operation

Sigfillset (sigset_t *set); Set all signals to the set signal set;
Sigemptyset (sigset_t *set); Clears all signals from the set signal set;
Sigaddset (sigset_t *set,int sig); The sig signal is added to the set signal;
Sigdelset (sigset_t *set,int sig); remove sig signal in set signal set;

4. Blocking signal correlation function

int sigprocmask (int how,const sigset_t *set,sigset_t *set); Sets the block signal set, or releases the blocked signal set based on the How value

int sigpending (sigset_t *set); Get all the signals in the block;

int sigsuspend (const sigset_t *set); Similar to the pause () function!

Linux signal, signal processing, and signal processing functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.