Signal concept
Signals are software interrupts, and many of the more important applications need to process the signals, which provide a way to handle asynchronous events.
Each signal has a name that begins with the three-character sig. For example, Sigabort is the signal to be loaded, which is generated when the process calls the Abort function. No signal with number 0 is present.
Many conditions can generate a signal:
- When the user presses certain terminal keys, the signal generated by the terminal is raised. The terminal signal (SIGINT) is usually generated by pressing the DELETE key (or the CTRL + C key in many systems) at the terminal. This is a way to stop a program that has been out of control.
- A hardware exception generates a signal: a divisor of 0, an invalid memory reference, and so on. These conditions are usually detected by the hardware and notified to the kernel. The kernel then generates the appropriate signal for the process that is running when the condition occurs. For example, a SIGSEGV signal is generated for a process that executes an invalid memory reference.
- The process calls the Kill (2) function to send a signal to another process or process group. Naturally, there is a limit to this: the owner of the receiving signal process and the sending signal process must be the same, or the owner of the sending signal process must be a superuser.
- The user can use the Kill (1) command to send a signal to another process. This command is only the interface of the KILL function. This command is commonly used to terminate a runaway background process. A signal is also generated when a software condition has been detected and should be notified of the process. This refers not to hardware-generated conditions (such as dividing by 0), but rather to software conditions. For example Sigurg (the Out-of-band data is generated on a network connection), Sigpipe (generated when a process writes this pipeline after the pipeline's read process has been terminated), and SIGALRM (generated when the clock clock that the process sets is timed out).
signal is a classic example of an asynchronous event。 The event that generates the signal is randomly occurring for the process. The process cannot simply test a variable to determine if a signal has occurred, but must tell the kernel "to do the following when the secondary signal appears".
You can require the kernel to be processed in one of the following three ways when a signal appears: ignore this signal, capture this signal, and perform the system default action.
Signal mechanism function Signal Set POSIX.1 defines the data type sigset_t to contain a signal set, and defines the following five functions to process the signal set:
- Sigemptyset (): Initialize the signal set to NULL
- Sigfillset (): Initializes the signal set to include all defined signal sets
- Sigaddset (): Adding the specified signal to the signal set
- Sigdelset (): Removes the specified signal from the signal set
- Sigismember (): Queries whether the specified signal is in the signal set
The Sigprocmask function calls the Sigprocmask function to detect or change its signal mask word:
Required header File |
#include <signal.h> |
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 |
sigpending function
The Sigpending function is used to query for "pending" signals. Its function prototype and description are as follows:
& nbsp; sigpending (query pending signal) |
required header file |
#include < Signal.h> |
|
The set of signals to be shelved is returned by the parameter set pointer to |
|
int sigpending (sigset_t *set) |
function passed in value |
set: to detect signal set |
function return value |
success: 0 |
|
error code |
Efault: Parameter set pointer address cannot access EINTR: This call is interrupted |
The Sigactionsigaction 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 <signal.h> |
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 |
The Sigsuspend function Sigsuspend function accepts a signal set pointer, sets the signal mask Word to the value in the signal set, and the process hangs when the process receives a signal, when a signal is captured, the signal handler is executed first, and then returned from the Sigsuspend, Finally, the signal mask Word reverts to the value before calling Sigsuspend.
Required header File |
#include <signal.h> |
Function description |
The set of signals to be shelved is returned by the parameter set pointer |
Function prototypes |
NT Sigsuspend (const sigset_t *sigmask); |
function passed in value |
Sigmask: Set of signals to block |
function return value |
Success: No successful return value |
Error:-1, set errno to Eintr |
|
|