Signal () Functions

Source: Internet
Author: User
Tags sigint signal

Signal () function understanding

In the header file <signal. h>.

Signal (parameter 1, parameter 2 );

Parameter 1: the signal to be processed. You can enter kill-l in the terminal to view the system signal (64 in total ). In fact, these signals are macro defined by the system.

Parameter 2: the processing method (whether the system defaults to ignore or capture ).

Generally, three operations are performed.

(1) eg: Signal (SIGINT, sig_ing );

// Sig_ing indicates that the SIGINT signal is ignored, and the SIGINT signal is generated by interruptkey, usually CTRL
+ C or delete. The process that is sent to all foreground groups.

Let's write an infinite loop:


In this case, save and execute.

Pressing CTRL _ C does not respond. That's right.

To end the program, press Ctrl + \.

In fact, when we press Ctrl + \, The sigquit signal is generated.

(2) eg: Signal (SIGINT, sig_dfl );

// The SIGINT signal is generated by interruptkey, usually Ctrl + C or delete. The process that is sent to all foregroundgroups.
Sig_dfl indicates to execute the default system operation. In fact, the process is terminated when the default system action of most signals is performed. This is the same as not writing this handler function.

We changed the above program


Press Ctrl + C to terminate the process. Remove signal (SIGINT, sig_dfl);. The effect is the same.

(3) void (* signal (INT Sig, void (* Handler) (INT );
INT (* p )();
This is a function pointer. P points to a function that does not contain any parameters and returns an int value.
INT (* Fun ())();
The difference between this formula and the above formula is that fun () is used to replace P, while fun () is a function. Therefore, after the function is executed, its return value is a function pointer. This function pointer (actually the above P) points to a function without any parameters and returns an int value.

Void (* signal (INT signo, void (* Handler) (INT); you can think of it as the signal () function (which includes two parameters, one is an integer and the other is a function pointer), and the return value of this signal () function is also a function pointer. This function pointer points to a function with an integer parameter, and return a function of void.

When writing a signal processing function, the function for signal processing is also void sig_fun (INT signo). This type exactly corresponds to the above signal () the function pointer returned by the function points to the same function. void (* signal () (INT );

Signal is a function that returns a function pointer,
The function to which the latter points accepts an integer parameter and does not return a value. check whether it is the 2nd parameter of siganal (INT signo, void (* Handler) (INT). By the way, in fact, he returns the 2nd signal processing functions of signal, pointing to the signal processing function, you can execute the function (
In signal, signal transmits the signal as a parameter to the handler signal processing function, and then the signal function returns the pointer,
And point to the signal processing function, and then start to execute it)

So what are the parameters of the signal function? The signal function accepts two parameters: an integer signal number and a pointer to a user-defined signal processing function. We have previously defined the SFP pointer to the User-Defined signal processing function:

Void (* SFP) (INT );

The SFP type can be obtained by removing the SFP declared above, namely void (*) (INT ). In addition, the return value of the signal function is a pointer to the User-Defined signal processing function before the call. The pointer type is consistent with the SFP pointer type. Therefore, we can declare the signal function as follows:

Void (* signal (INT, void (*) (INT );

Similarly, using typedef can simplify the above function declaration:

Typedef void (* Handler) (INT );
Handler signal (INT, Handler );

OK; let's look at an example:


This program executes the defined signal processing function when we press Ctrl + C.

Every time we press Ctrl + C, the number of the signal is printed. We can see that the num of the signal is 2.

To exit, press Ctrl + \ to print the result as the last line.

Some commonly used signal are as follows:

Note: The following is a query from Baidu Library (* ^__ ^ ......

Signal Description
SIGABRT Generated by calling the abort function. The process exits abnormally.
Sigalrm Timer timeout set by the alarm function or interval timer timeout set by the setitimer Function
Sigbus A specific hardware exception is usually caused by memory access.
Sigcancel It is used internally by the Solaris thread library and is usually not used
Sigchld When the process is terminate or stop, sigchld will send it to its parent process. This signal is ignored by default.
Sigcont It is automatically sent when the stopped process resumes running.
Sigemt Hardware exceptions related to implementation
Sigfpe Mathematical exceptions, such as Division by 0, floating point overflow, and so on
Sigfreeze Special for Solaris, sent when hiberate or susponded
Sighup Send to controlling process with terminal. When terminal is disconnect, send
Sigill Invalid Command exception
Siginfo BSD signal. Generated by the Status key, usually Ctrl + T. Processes sent to all foreground groups
SIGINT Generated by interrupt key, usually Ctrl + C or delete. Processes sent to all foreground groups
Sigio Asynchronous Io events
Sigiot Implementation-related hardware exceptions, usually corresponding to SIGABRT
Sigkill Cannot be processed or ignored. Abort a process
Siglwp Used internally by Solaris thread libray
Sigpipe Sent when the pipe is written after the reader is aborted
Sigpoll When an event is sent to the pollable Device
Sigprof Generated by the profiling interval timer specified by setitimer
Sigpwr Related to the system. Related to UPS.
Sigquit When the quit key is input (CTRL + \), it is sent to all foreground group processes.
SIGSEGV Illegal Memory Access
Sigstkflt Stack exception for Linux-specific mathematical coprocessor
Sigstop Stop the process. Cannot be processed or ignored.
Sigsys Invalid system call
Sigterm Request to abort the process. The kill command is sent by default.
Sigthaw Special for Solaris, sent when restoring from suspend
Sigtrap Hardware exceptions. Generally, it is a debugging exception.
Sigtstp Suspend key, usually Ctrl + Z. Processes sent to all foreground groups
Sigttin Sent when the background group process tries to read Terminal
Sigttou Sent when the background group process tries to write Terminal
Sigurg It may be sent when out-of-band data is received
SIGUSR1 User-Defined signal 1
Sigusr2 User-Defined Signal 2
Sigvtalrm When the virtual interval timer set by the setitimer function times out
Sigwaiting Internal Implementation of Solaris thread Library
Sigwinch When the window size of terminal changes, all processes sent to foreground Group
Sigxcpu When the CPU time limit times out
Sigxfsz The process exceeds the file size limit.
Sigxres Special for Solaris, sent when the process exceeds the resource limit

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.