The signal is associated with a certain process. In other words, a process can determine which signals are handled in the process. For example, a process can ignore some signals and handle only some other signals, and a process can also choose how to handle the signal. In short, these are always associated with a particular process. Therefore, the first to establish its signal and process correspondence, this is the signal installation registration.
Linux mainly has two functions to implement the installation registration of the signal: signal and sigaction. Where signal is implemented on the basis of system invocation, it is a library function. It has only two parameters, does not support signal transmission information, mainly for the first 32 non-real-time signal installation, and sigaction is a newer function (implemented by two system calls: Sys_signal and Sys_rt_sigaction), three parameters, support signal transmission information, Used primarily with sigqueue system calls. Of course, Sigaction also supports the installation of non-real-time signals, sigaction better than signal mainly reflected in the support signal with parameters.
For the signal that the application processes itself, the life cycle of the signal is four stages of the signal installation registration, signal set operation, signal sending and signal processing. Signal installation registration refers to the installation of this signal processing method in the application. The function of a signal set operation is to signal the masking of one or more of the signals specified, which is not required for some applications. Signal transmission refers to the signal sent by the hardware (such as the ctrl-c on the terminal) sent by the signal and the software (such as through the Kill function) sent signals. Signal processing refers to the operating system processing of the receiving signal process, the processing method is to check whether the signal set operation function to block this signal, if there is no shield, the operating system will be registered in the signal installation function of the processing function to complete the processing of this process.
1. Signal function
(1) Function description
In the signal function, there are two parameters representing the signal number value to be processed and the pointer to process the signal function. It is mainly used for the first 32 kinds of non-real-time signal processing, does not support signal transmission information. But because it is easy to use and easy to understand, it is used by programmers on many occasions.
For UNIX systems, when the signal function is used, the custom processing signal function fails once and the processing of the signal is returned to the default processing mode. Here is an example of a program that uses the signal (Sigquit, my_func) function call, where My_func is a custom function. When the application process receives the sigquit signal, it jumps to the custom processing signal function My_func execution, after executing the signal registration function My_func invalidation, the processing of the sigquit signal back to the operating system default processing mode, when the application process receives the sigquit signal again, Processed by the operating system by default (that is, the my_func processing function is no longer executed). In the Linux system, the signal function has been rewritten, implemented by the Sigaction function encapsulation, there is no such problem.
(2) Signal function prototype
Signal (Set signal processing mode) |
Required header File |
#include |
Function description |
Sets the signal processing mode. Signal () Sets the processing function of the signal according to the signal number specified by the parameter signum. When the specified signal arrives, it jumps to the function execution specified by the parameter handler |
Function prototypes |
void (*signal (int signum,void (* handler) (int))) (int) |
function passed in value |
Signum |
Specify the signal number |
Handle |
Sig_ign: Ignoring the signal specified by the parameter Signum |
SIG_DFL: Resets the signal specified by the parameter Signum to the core preset signal processing mode, which uses the system default mode to process the signal |
Custom signal function Processing pointers |
function return value |
Success |
Returns the previous signal processing function pointer |
Error |
Sig_err (-1) |
Additional Instructions |
In a UNIX environment, after the signal has jumped to the custom handler handler function, the system will automatically swap the processing function back to the original system preset, if you want to change this situation, use the Sigaction function instead. This problem does not exist in the Linux environment |
The signal function prototype is more complex and can be simplified if the following typedef is used.
typedef void sign (int);
Sign *signal (int, handler *);
As can be seen, the function prototype first points to a function pointer with no return value with an integer parameter, which is the original configuration function of the signal. The prototype then comes with two parameters, and the second parameter can be a function pointer to a user-defined signal handler function. The format of this function can not be understood, but need to learn to imitate use.
(3) Signal function Usage Example
This example shows how to use the signal function to install a registration signal handler function. When this signal occurs, the registered signal processing function captures the corresponding signal and makes a given processing. Here, My_func is a function pointer to signal processing. Readers can also change my_func to Sig_ign or SIG_DFL to see the results of the run.
SIGNAL.C source code is as follows:
#include
#include
#include
/* Custom Signal processing function */
void My_func (int sign_no)
{
if (sign_no==sigint)
printf ("I have get sigint\n");
else if (sign_no==sigquit)
printf ("I have get sigquit\n");
}
int main ()
{
printf ("Waiting for signal SIGINT or sigquit \ n");
/* Send the corresponding signal and jump to the signal processing function */
Signal (SIGINT, my_func);
Signal (Sigquit, my_func);
Pause ();
Pause ();
Exit (0);
}
Compile GCC signal.c–o signal.
Execute the./signal and execute the results as follows:
Waiting for signal SIGINT or Sigquit
I have get SIGINT/* Press CTRL + C, the operating system will send SIGINT signal to the process */
I have get sigquit/*/press Ctrl-\ (exit), the operating system will send sigquit signal to the process */
2. sigaction function
(1) Sigaction function prototype
The Sigaction function is used to query and set the signal processing method, which is used to replace the earlier signal function. Sigaction function prototypes and descriptions are as follows:
Sigaction (query and set signal processing mode) |
Required header File |
#include |
Function description |
Sigaction () Sets the processing function of the signal according to the signal number specified by the parameter Signum |
Function prototypes |
int sigaction (int signum,const struct sigaction *act, struct sigaction *oldact) |
function passed in value |
Signum |
All signals except Sigkill and sigstop can be specified. |
Act |
The parameter structure sigaction is defined as follows struct sigaction { void (*sa_handler) (int); void (*sa_sigaction) (int, siginfo_t *, void *); sigset_t Sa_mask; int sa_flags; void (*sa_restorer) (void); } ①sa_handler: This parameter is the same as the parameter handler for signal (), which is mainly used to support the signal old installation function signal () processing form ②sa_sigaction: The new signal installation mechanism, when the processing function is called, not only can get the signal number, but also can learn the reason of being called and the information about the context of the problem. ③sa_mask: Used to set the Sa_mask specified signal to be temporarily shelved when processing the signal ④sa_restorer: This parameter is not used ⑤sa_flags: To set up other related operations for signal processing, the following values are available. Available with or operations (|) combination ? A_nocldstop: If the parameter Signum is SIGCHLD, the parent process is not notified when the child process pauses ? Sa_oneshot/sa_resethand: To change this signal processing mode to a system preset before invoking a new signal processing function ? Sa_restart: A system call that is interrupted by a signal will restart itself ? Sa_nomask/sa_nodefer: Ignore this signal again before processing this signal is not over ? Sa_siginfo: The signal processing function is a sa_sigaction with three parameters |
Oldact |
If the parameter oldact is not a null pointer, the original signal processing method is returned by this structure sigaction |
function return value |
Success: 0 |
Error:-1, error reason |
Additional Instructions |
Two new and old mechanisms for signal processing installation: ① uses the old processing mechanism: struct Sigaction Act; Act.sa_handler=handler_old; ② uses a new processing mechanism: struct Sigaction Act; Act.sa_sigaction=handler_new; and set the Sa_siginfo bit of sa_flags |
Error code |
EINVAL: Parameter Signum illegal or attempt to intercept sigkill/sigstop signal Efault: Parameter act,oldact pointer address cannot be accessed EINTR: This call is interrupted |
(2) Sigaction function Usage Example
SIGACTION.C source code is as follows:
#include
#include
#include
#include
#include
void New_op (int, siginfo_t *, void *);
int main (int argc,char**argv)
{
struct Sigaction Act;
int sig;
Sig=atoi (argv[1]);
Sigemptyset (&act.sa_mask);
Act.sa_flags=sa_siginfo;
Act.sa_sigaction=new_op;
if (Sigaction (Sig,&act,null) < 0)
{
Perror ("Install Sigal error");
return-1;
}
while (1)
{
Sleep (2);
printf ("Wait for the signal\n");
}
return 0;
}
void New_op (int signum,siginfo_t *info,void *myact)
{
printf ("Receive signal%d\n", signum);
Sleep (5);
}
Compile gcc sigaction.c-o sigaction.
Execute the./sigaction 2 with the following results:
Wait for the signal
Receive signal 2/* Press CTRL + C */
Exit/* Press Ctrl-\ */
3. Signal set Operation function
Since it is sometimes necessary to treat multiple signals as a set, so that the signal set is generated, the signal set is used to describe a set of signals, the signals supported by Linux can appear in all or part of the signal set. The most common place for signal set operation functions is for signal shielding. For example, sometimes you want a process to execute correctly, rather than the process being affected by some signals, you need to use the signal set operation function to complete the shielding of these signals.
The signal set operation function is divided into three categories according to function and use order, which is to create signal set function, set signal shielding function and signal function that query is shelved (pending). Creating a signal set function simply creates a collection of signals, sets the signal shield function to mask the signals in the specified signal set, and queries the used signal function to query the current "pending" signal set. The signal Set function group is not able to complete the signal installation registration work, the signal installation registration needs through the Sigaction function or the signal function to complete.
The signal being shelved is the next step in signal processing, but is not required. Because sometimes the process requires blocking some signals during a certain period of time, the program completes the specific work to unblock the signal, and the blocked signal in this time period is called "Pending" signal. These signals have been generated, but have not been processed, and the sigpending function is used to detect these "pending" signals of the process and further determine what to do with them (including non-processing).
(1) Creating a signal set function
There are 5 functions for creating a signal set:
①sigemptyset: Initialization signal set is empty.
②sigfillset: Add all the signals to the set, and the signal set will contain 64 kinds of signals supported by Linux.
③sigaddset: Adds the specified signal to the signal set.
④sigdelset: Deletes the specified signal from the signal set.
⑤sigismember: Queries whether the specified signal is in the signal set.
Creating a signal aggregate function prototype |
Required header File |
#include |
Function prototypes |
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 (sigset_t *set,int Signum) |
function passed in value |
Set: Signal Set |
Signum: Specify signal value |
function return value |
Success: 0 (Sigismember function exception, successfully returned 1, failure returned 0) |
Error:-1, error reason |
(2) Setting the signal shielding bit function
Each process has a set of signals to describe which signals will be blocked when they are delivered to the process, and all signals in that set will be blocked after they are delivered to the process. The call function Sigprocmask can set the signal in the signal set to block or not block. Its function prototype and description are as follows:
Sigprocmask (set signal screen bit) |
Required header File |
#include |
Function prototypes |
int sigprocmask (int how,const sigset_t *set,sigset_t *oset) |
function passed in value |
How (determines how the function is manipulated) |
Sig_block: Adds a signal set to the blocking set of the current process |
Sig_unblock: Removes a set of signals from the current blocking set |
Sig_setmask: Sets the current signal collection to the signal blocking set |
Set: Specify the signal set |
Oset: Signal Shielding Word |
function return value |
Success: 0 |
Error:-1, error reason |
(3) query is shelved (pending) Signal function
The Sigpending function is used to query for "pending" signals. Its function prototype and description are as follows:
; sigpending (query pending signal) |
Required header File |
#include |
Function description |
The set of signals to be shelved is returned by the parameter set pointer |
Function prototypes |
int sigpending (sigset_t *set) |
function passed in value |
Set: To detect a signal set |
function return value |
Success: 0 |
Error:-1, error reason |
Error code |
Efault: Parameter set pointer address cannot be accessed EINTR: This call is interrupted |
(4) How to use the signal set operation function
The usage and order of the signal set operation functions are as follows:
① uses the signal or Sigaction function to install and register the signal processing.
② defines the signal set using SIGEMPTYSET and other defined signal set functions.
The ③ uses the Sigprocmask function to set the signal shielding bit.
④ uses the Sigpending function to detect pending signals, not required steps.
(5) Example of using signal set operation function
The instance first uses the Sigaction function to register the SIGINT signal, and the installation registration uses both the new and the old mechanisms, where # if 0 is commented out as a new mechanism for signal installation. Then the program adds Sigquit, SIGINT two signals to the signal set, and sets the signal set as blocking state. The program starts to sleep 30 seconds, at this time the user presses CTRL + C, the program will test to this outstanding signal (SIGINT), then the program sleeps 30 seconds after the SIGINT signal unblocked, this time will process SIGINT registered signal function My_func. Finally, the process execution can be terminated with the sigquit (ctrl+\) signal.
SIGSET.C source code is as follows:
#include
#include
#include
#include
#include
/* Custom Signal processing function */
#if 0
void my_funcnew (int signum, siginfo_t *info,void *myact)
#endif
void My_func (int signum)
{
printf ("If you want to quit,please try sigquit\n");
}
int main ()
{
sigset_t set, Pendset;
struct Sigaction action1,action2;
/* Set the Signal processing method */
Sigemptyset (&action1.sa_mask);
#if 0/* Signal New Installation mechanism */
Action1.sa_flags= Sa_siginfo;
Action1.sa_sigaction=my_funcnew;
#endif
/* Old installation mechanism of signal */
action1.sa_flags= 0;
Action1.sa_handler=my_func;
Sigaction (Sigint,&action1,null);
/* Initialize signal set is empty */
if (Sigemptyset (&set) <0)
{
Perror ("Sigemptyset");
return-1;
}
/* Add the corresponding signal to the signal set */
if (Sigaddset (&set,sigquit) <0)
{
Perror ("Sigaddset");
return-1;
}
if (Sigaddset (&set,sigint) <0)
{
Perror ("Sigaddset");
return-1;
}
/* Set the signal set screen word */
if (Sigprocmask (sig_block,&set,null) <0)
{
Perror ("Sigprocmask");
return-1;
}
Else
{
printf ("blocked\n");
}
/* Test signal is added to the signal set */
if (Sigismember (&set,sigint)) {
printf ("SIGINT in set\n");
}
Sleep (30);
/* Test pending signal */
if (sigpending (&pendset) <0)
{
Perror ("Get Pending Mask Error");
}
if (Sigismember (&pendset, SIGINT))
{
printf ("Signal SIGINT is pending\n");
}
Sleep (30);
if (Sigprocmask (sig_unblock,&set,null) <0)
{
Perror ("Sigprocmask");
return-1;
}
Else
printf ("unblock\n");
while (1)
{
Sleep (1);
}
return 0;
}
Compile gcc sigset.c-o sigset.
Execute the./sigset and execute the results as follows:
Blocked
SIGINT in Set/* Press CTRL + C */
Signal SIGINT is pending
If you want to quit,please try Sigquit/* Press CTRL + C */
Exit
Excerpt from the "in-depth Linux tools and Programming"
signal function, sigaction function and signal set (sigemptyset,sigaddset) Operation function