Linux under Signal () function detailed

Source: Internet
Author: User

First, describe the function pointer definition form:

< storage type > data type (* function pointer name) (parameter table);

Where the storage type is generally not written, in the default form. Auto type, static type and extern type can be selected. The data type is the data type of the return value of the function that the pointer points to. A parameter table is a parameter table that points to a function.

Again, the return value of a function is defined as a function pointer (an understanding that this function a actually returns a pointer to another function B):

< storage type > data type (* Function name (parameter table 1)) (parameter table 2);

Table header file #include <signal.h>

function

Set a corresponding action for a signal

Function Prototypes:

void (*signal int signum,void (* handler) (int))) (int);

Or: typedef void (*sig_t) (int); sig_t signal (int signum,sig_t handler);

Parameter description:

The first parameter signum indicates the type of signal to be processed, and it can take any signal except Sigkill and sigstop.

The second parameter handler describes the action associated with the signal, which can take the following three values:

(1) A function address with a positive return value This function must be declared before the signal () is invoked, and the name of the function in handler. When a signal of type sig is received, the function specified by handler is executed.   This function should have the following form definition: intfunc (int sig); The sig is the only parameter passed to it. After the signal () call is executed, the process simply receives a signal of type sig, and executes the Func () function immediately, regardless of which part of the program is executing. When the Func () function completes, control returns to the point at which the process is interrupted.

(2) Sigign This symbol indicates that the signal is ignored, and after the corresponding signal () call is performed, the process ignores the type sig signal.

(3) SIGDFL This symbol indicates that the recovery system is handling the signal by default.

Function Description: 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. When the signal processing function of a signal executes, if the process receives the signal again, the signal is automatically stored without interrupting the execution of the signal processing function until the signal processing function is finished and the corresponding handler function is called again. However, if the process receives other types of signals when the signal processing function executes, execution of the function is interrupted.

Return value: Returns the previous signal processing function pointer and returns SIG_ERR (-1) If there is an error.

Additional note: After the signal occurs to the custom handler processing function execution, the system will automatically swap this handler back to the original system preset processing, if you want to change this operation, please use Sigaction ().

The following conditions can produce signal:

1. Press CTRL + C to produce SIGINT

2. Hardware interrupts, such as in addition to 0, illegal memory access (SIGSEV), etc.

3. The KILL function can send signal to the process

4. Kill command. is actually a wrapper over the kill function.

5. Software interruption. If the alarm clock timeout (Sigurg), write the data to the pipe after the reader is aborted (sigpipe), and so on

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.