Communication between Linux Environment Processes (2): Signals (on)
content:
Signal and signal source signals The sector of the signal response signal of the signal (setting signal association action) signal set and signal set operation function signal blocking and signal unreasonable reference information about the author
related information:
Communication between Linux Environment Processes (1): Pipelines and famous pipes
In the Linux area:
Tutorial Tools & Product Codes & Component Articles
Zheng Yanxing (mlinux@163.com) January 2003
The Linux signal mechanism is far more complicated than the imagination. This article strives to use the shortest space to analyze the mechanism in depth. The signal application instance will be given in the signal (below).
I. Signal and signal source signal nature signal is an analog to the interrupt mechanism at the software level. In principle, one process receives a signal and the processor receives an interrupt request. The signal is asynchronous, one process does not have to wait for any operation to wait for the arrival, in fact, the process does not know when the signal arrives. The signal is the only asynchronous communication mechanism in the inter-process communication mechanism, which can be considered as an asynchronous notification, and what is the process of notifying the receiving signal. After the POSIX live expansion, the signal mechanism is more powerful, in addition to the basic notification function, additional information can be passed. There are two sources of signal source signal events: Hardware sources (such as we press keyboard or other hardware failure); software source, the system function of the most commonly transmitted signal is Kill, Raise, Alarm and SetitiMer, Sigqueue functions, Source Source It also includes some illegal operations. Second, the type of signal can be classified from two different classification angles: (1) Reliability: reliability signal and unreliable signal; (2) The relationship between the time: real-time signal and the non-real time signal. All signals supported by the system are listed in the "Linux Environment Process (1): Pipes and Anti-Nurse Pipes. 1. The reliable signal and the unreliable signal "unreliable signal" Linux signal mechanism is basically inherited from the UNIX system. The signal mechanism in the early UNIX system is relatively simple and original. Later, some problems are exposed in practice, so the signals that establish in the early mechanism are called "unreliable signals", and the signal value is less than Sigrtmin (Red Hat 7.2, Sigrtmin = 32, SigrtMax = 63) The signal is unreliable signal. This is the source of "unreliable signal". Its main problem is:
After each process of processing the signal each time, the response to the signal is set to the default action. In some cases, it will result in an error handling of the signal; therefore, if the user does not want this, then the signal processing function is again called Signal (), reinstalling the signal. The signal may be lost, and will be explained in detail later. Therefore, the unreliable signal under early UNIX mainly refers to the process may be missed to the signal and the signal may be lost. Linux supports unreliable signals, but improves the unreliable signaling mechanism: After the signal processing function is called, it is not necessary to recall the installation function of the signal (the signal installation function is implemented in a reliable mechanism). Therefore, the unreliable signal problem under Linux mainly refers to the signal may be lost. "Reliable signal" has proved necessary to improve and expand the original mechanism of the signal. Therefore, the various UNIX versions of the later appearing have studied in this regard, and the "reliable signal" is achieved. Since there are many applications for the original defined signal, it is not good to do again, and eventually add some signals, and will define them as a reliable signal at the beginning, which supports queuing and will not be lost. At the same time, the send and installation of the signal also appear: Signal Send Function Sigqueue () and Signal Installation Functions SigAction (). POSIX.4 has made standardization on reliable signaling mechanisms. However, the POSIX only has standardized the function of the reliable signal mechanism and the external interface of the signaling mechanism, and the implementation of the signal mechanism does not make specific regulations. The signal value is located between Sigrtmin and Sigrtmax is a reliable signal, and the reliable signal overcomes the problem that the signal may be lost. LINUX still supports early Signal () signal installation functions while supporting the new version of the signal installation function sigqueue () while supporting the early Signal () signal installation function, support signal transmission function kill (). Note: Don't have such misunderstandings: Send by sigqueue (), SigAction installed signal is reliable. In fact, the reliable signal refers to the new signal added later (the signal value is between Sigrtmin and Sigrtmax); the unreliable signal is a signal that is less than Sigrtmin. The reliability of the signal is related to the signal value, regardless of the signal transmission and installation functions. At present, Signal () in Linux is implemented via the sigation () function, so even if the signal mounted by signal (), the signal installation function is not required again at the end of the signal processing function. At the same time, the real-time signal supported by Signal () is queued, and it will not be lost. For two signal installation functions currently Linux: Signal () and SigAction (), they cannot turn Sigrtmin previous signals into reliable signals (not supported queuing, still possible to lose, still unreliable signals), And the signals after Sigrtmin have supported queuing. The greatest difference between these two functions is that the signals installed through the SigAction can pass the information to the signal processing function (all signals are established), while the Signal installed signal cannot transmit information to the signal processing function. The same is true for signal delivery functions. 2, real-time signal and non-real-time signal early UNIX system only define 32 signals, RET HAT7.2 supports 64 signals, numbered 0-63 (sigrtmin = 31, sigrtmax = 63), will further increase further, this requires kernel support.
The first 32 signals already have a predefined value, each signal has a determined use and meaning, and each signal has its own default action. If the Ctrl ^ C is pressed, the SIGINT signal is generated, the default reaction of the signal is the termination of the process. The latter 32 signals represent real-time signals, which is equivalent to the reliable signals set forth in the previous. This ensures that multiple real-time signals sent are received. Real-time signal is part of the POSIX standard and can be used in application processes. Non-real-time signals do not support queuing, all unreliable signals; real-time signals support queuing, all reliable signals. Third, the process of responding to the signal can be in three ways to respond to a signal: (1) ignore the signal, that is, the signal does not do any processing, where there are two signals that cannot be ignored: sigkill and sigstop; (2) capture signal . Define the signal processing function, perform the corresponding processing function when the signal occurs; (3) Perform the default operation, Linux specifies the default operation for each signal, detailed, please refer to [2] and other information. Note that the default response to the real-time signal is the termination of the process. Which of the three modes of Linux uses the above three ways depending on the parameters passing to the corresponding API function. Fourth, the main functions of the transmission signal of the signal are: kill (), raise (), sigqueue (), alarm (), setitimer (), and abort (). 1, Kill () # include
The value of the value of the parameter PID is received the process PID> 0 process ID for the PID process PID = 0 Process PID <0 PID! = - 1 process group ID is -pid all processes PID = -1 In addition to send processes Outside it, all process IDs are greater than 1 process SINNO is the signal value, when 0 (ie space signal), does not send any signals, but perform an error check as usual, and therefore, can be used to check if the target process exists, and the current process Whether to have permissions to the target transmission signal (the process of root privileges can send signals to any process, the process of non-root privileges can only send signals to the same session or the same user). Kill () is most commonly used in the signal transmission of PID> 0, the call successfully returns 0; otherwise, returns -1. Note: For the case of PID <0, which process will accept signals, various versions are different, in fact, it is very simple, referred to the kernel source code kernal / signal.c, the rules in the above table are reference RED HAT 7.2. 2, raise () # include
int SiVal_INT;
Void * sival_ptr;
} SIGVAL_T;
SigQueue () is transmitted more additional information than Kill (), but sigqueue () can only send a signal to a process without sending a signal to a process group. If SIGNO = 0, an error check will be performed, but do not send any signals, 0 value signals can be used to check the validity of the PID and whether the current process has permission to send signals to the target process. When calling SIGQUEUE, the information specified by SIGVAL_T will copy to the 3 parameter signal processing function (3 Parameter signal processing function refers to the signal processing function is installed by the SigAction, and sets the SA_SIGAction pointer, will be explained later) SIGINFO_T structure, This signal processing function can handle this information. Since the Sigqueue system call supports sends a parameter signal, the function called by the KILL () system is much more flexible. Note: When SigQueue () Sends a non-real-time signal, the information contained in the third parameter is still able to pass to the signal processing function; when SIGQUEUE () is transmitted, the queue is still not supported, that is, in the execution of the signal processing function. All the same signals are merged into a signal. 4, ALARM () # include
ITIMER_REAL: Sets the absolute time; after the specified time, the kernel will send a sigalRM signal to this process; the ITIMER_VIRTUAL setup program execution time; after the specified time, the kernel will send the SIGVTALRM signal to this process; ITIMER_PROF setting process execution and The time consumed due to the process of this process and after the specified time, the kernel will send the ITIMER_VIRTUAL signal to this process; setitimer () first parameter which specifies the timer type (one of the above); the second parameter is An instance of architecture ITIMERVAL, Structure ITIMERVAL is in the form of attachment 1. The third parameter may not be processed. SetitiMer () call successfully returns 0, otherwise returns -1. 6, abort () # include
The first parameter of this function is the value of the signal, which can be a signal installation error to be a signal installation error in addition to any specific valid signal except Sigkill and SigStop (for these two signals). The second parameter is a pointer to an instance of the structure SigAction. In the instance of the structural SIGAction, the processing of the specific signal is specified, which can be empty, the process will process the signal in the default manner; the third parameter Oldact points to The object is used to save the processes for the corresponding signal, which can specify the OldACT to NULL. If the second and third parameters are set to NULL, the function can be used to check the validity of the signal. The second parameter is most important, including the processing of the processing of the specified signal, the signal passed, and which functions should be shielded during the execution of the signal processing function. The SIGAction structure is defined as follows: struct sigAction {
Union {
__sighandler_t _sa_handler;
Void (* _SA_SIGACTION) (int, struct siginfo *, void *);
} _U
Sigset_t sa_mask;
UNSIGNED long sa_flags;
Void (* sa_restorer) (void);
}
Among them, SA_RESTORER has been outdated, POSIX does not support it, and should not be used again. 1. Two elements in the joint data structure _sa_handler and * _sa_sigAction specify the signal association function, that is, the user-specified signal processing function. In addition to the user-defined processing function, it is also possible for SIG_DFL (using default processing mode), or SIG_IGN (Ignore). 2, only one parameter, ie, the signal value specified by _sa_handler, so the signal cannot transmit any information other than the signal value; three parameters are used by _sa_sigaction, which is the specified signal processing function, which is set for real-time signals. (Of course, it also supports a non-real-time signal), which specifies a 3 parameter signal processing function. The first parameter is the signal value, the third parameter is not used (POSIX is not specified using the standard), and the second parameter is a pointer to the SIGINFO_T structure, and the data value of the signal is included in the structure, the configuration points indicated by the parameter. as follows:
SigInfo_t {
INT SI_SIGNO; / * Signal value, meaningful for all signals * /
Int si_errno; / * errno value, meaningful for all signals * /
INT SI_CODE; / * The cause of the signal is generated, meaningful for all signals * /
Union {/ * United data structure, different members adapt to different signals * /
/ / Ensure that there is enough storage space
INT _PAD [SI_PAD_SIZE];
/ / Meaningful structure for Sigkill
Struct {
...
}
...
...
/ / Meaningful structure for SIGILL, SIGFPE, SIGSEGV, Sigbus
Struct {
...
}
...
}
}
Note: In order to read it easier to read, the structure is often expressed as the form representation represented by Appendix 2. The joint data member in the SIGINFO_T structure ensures that the structure adapts to all signals, for example, for real-time signals, the following structural form is actually used:
Typedef struct {
INT SI_SIGNO;
INT SI_ERRNO;
Int si_code;
Union SigVal Si_Value;
} SIGINFO_T;
The fourth domain of the structure is also a joint data structure:
Union sigval {
int SiVal_INT;
Void * sival_ptr;
}
With the joint data structure, Si_Value in the SIGINFO_T structure either holds a 4-byte integer value, or holds a pointer, which constitutes data related to the signal. In the processing function of the signal, such a signal related data pointer is included, but there is no specification how to operate these data, and the operation method should be agreed by the program developer according to the specific task. The third parameter of Sigqueue is the SIGVAL federated data structure when discussing the signal in the discussion of the system, and when the SIGQUEUE is called, the data in the data structure will be copied to the second parameter of the signal processing function. In this way, the signal can be transmitted to some additional information at the same time. Signals can pass information. It is very meaningful to development. The transfer process of the signal parameters can be shown as follows:
3. SA_MASK specifies which signals should be blocked during the execution of the signal processing program. By default, the current signal itself is blocked, and the nested transmission of the signal is prevented, unless specified SA_NODEFER or SA_NOMASK flag. Note: Please note that the prerequisite for SA_MASK specified by SA_MASK is to block the signal specified by sa_mask during execution of SigAction () installation signals. 4, SA_FLAGS contains many flags, including SA_Nodefer and SA_Nomask flags just mentioned. Another important marker bit is SA_SIGINFO. When the flag is set, the parameter indicating that the signal can be passed to the signal processing function, and therefore, the processing function should be specified for the SA_SIGAction in the sigAction structure, and should not be SA_HANDLER Specifies the signal processing function, otherwise, setting the flag is meaningless. Even if the signal processing function is specified for the sa_sigAction, if sa_siginfo is not set, the signal processing function can also obtain the data passed from the signal, and the access to this information will result in Segmentation Fault in the signal processing function. Note: Many documents are considered when the flag is elaborated. If this flag is set, the three-parameter signal processing function must be defined. This is not the case, the verification method is simple: you can implement a single parameter signal processing function, and set the flag in the program to view the run results of the program. In fact, the flag can be regarded as a switch that the signal is transmitted, and if the bit is set, the parameters are passed; otherwise, the parameters are not delivered. 6. Signal sets and signal set operation functions: Signal Set is defined as a data type:
Typedef struct {
Unsigned long Sig [_nsig_words];
} sigset_t
Signal sets to describe the set of signals, all signals supported by Linux can all or part appear in signal concentration, mainly with signal blocking correlation functions. Below is a related function defined for signal set operation:
#include
INT SiGemptySet (SIGSET_T * SET);
INT SigfillSet (SIGSET_T * SET);
INT Sigaddset (SigSet_t * set, int signum)
INT Sigdelset (SIGSET_T * SET, INT SIGNUM);
INT Sigismember (const sigset_t * set, intigation); sigemptyset (SIGSET_T * SET) initializes the signal set specified by the set, all signals inside the signal set are emptied;
SIGFILLSET (SIGSET_T * SET) When the function is called, the Sight pointing to the SET will contain 64 signals supported by Linux;
SigAddSet (SIGSET_T * SET, INT SIGNUM) adds a SIGNUM signal in the SIGNUM in SET;
SigDelset (SIGSET_T * SET, INT SIGNUM) Removes the Signum signal in the signal pointing to by SET;
Sigismember (const Sigset_t * set, int signum) Decision Signal Signum is concentrated in SET.
7. Signal blocked and signals have not decreasy: Each process has a signal set to describe which signals to be blocked when it is delivered, and all signals in this signal will be blocked after delivery to the process. Below is several functions associated with signal blocking:
#include
INT SigProcmask (int how, const sigset_t * set, sigset_t * oldset);
INT SIGPENDING (SIGSET_T * SET));
INT Sigsuspend (const sigset_t * mask);
SigProcmask () function can implement the operation of the signal set according to the parameter how, there are three main types:
Parameter HOW Process Current Signal Set SIG_BLOCK In Process Current Block Signal Concentration Add SET Pointing Signal Signal Signal Sign Sig_unblock If the process blocked signal set contains SET to point to signal set, release the block SIG_SETMASK update process blocking signal set to SET Signal set sigpending (SIGSET_T * SET)) Get all signals that are currently delivered, but blocked, and the result is set in the Sightset in SET. Sigsuspend (const sigset_t * mask) is used to temporarily replace the signal mask for the process with MASK before receiving a signal, and suspend the process until the signal is received. Sigsuspend is returned to the call to the call before calling. After the signal processing function is complete, the process will continue. The system call always returns -1 and set errno to Eintr. Appendix 1: Structure ITIMERVAL:
Struct itimerval {
Struct TimeVal IT_Interval; / * Next Value * /
Struct TimeVal IT_Value; / * Current Value * /
}
Struct Timeval {
Long TV_sec; / * seconds * /
Long TV_usec; / * microseconds * /
}
Appendix 2: Descriptive description of the second parameter in the three parameter signal processing function:
SigInfo_t {
INT SI_SIGNO; / * Signal value, meaningful for all signals * /
Int si_errno; / * errno value, meaningful for all signals * /
INT SI_CODE; / * The cause of the signal is generated, meaningful for all signals * /
PID_T Si_PID; / * Send Signal Process ID, meaning for Kill (2), real-time signal, and sigchld * /
UID_T SI_UID; / * Send the real user ID of the signal process, meaning for Kill (2), real-time signal, and sigchld * /
INT SI_STATUS; / * Exit status, meaningful for sigchld * / clock_t si_utime; / * The time consumes for the user, meaningful for sigchld * /
Clock_t si_stime; / * The time of core consumption, meaningful for sigchld * /
SigVal_t si_value; / * signal value, it makes sense for all real-time, a joint data structure, which can be an integer (indicated by Si_INT, can also be a pointer, marked by Si_PTR) * /
Void * si_addr; / * Triggered the Fault's memory address, meaningful for SIGILL, SIGFPE, SIGSEGV, Sigbus signal * /
Int si_band; / * Meaning on SigPoll signals * /
INT Si_fd; / * Meaning on SigPoll signals * /
} In fact, in addition to the first three elements, other elements are tissue in a joint structure, in the joint data structure, and there are different structures according to different signals. Note The meaning of some signal is meaningful, and these domains can be accessed in the processing function of the signal to obtain meaningful information related to the signal, but only specific signals are only interested in specific information. references:
linux kernel source code Scenario Analysis (on), Maud parade, Hu Ximing with Zhejiang University Press, when you want to verify a conclusion, when the idea of the best reference materials; UNIX high-level programming environment, Author: W.Richard Stevens, translator : Yugukan, etc., Machinery Industry Press. The more detailed details of the development process of the signal mechanism. Signal, SigAction, Kill, etc., the most direct and reliable reference. http://www.linuxjournal.com/modules.php?op=modload&name=ns-help&file=man provides online guidelines such as many system calls, library functions, and more. Http://www.opengroup.org/onlinepubs/007904975/ You can query many key functions (including system calls) here, very good URL. http://unix.org/WhitePapers/reeTrant.html elaborates the functions of the function. http://www.uccs.edu/~compsvcs/doc-cdrom/docs/html/aps33dte/docu_006.htm gives a considerable description of the real-time signal. About the author: Zheng Yanxing, National Defense Science and PhD. Contact: mlinux@163.com