Use of the signal function

Source: Internet
Author: User

Signal System Function calls provide a simple example. However, the C prototype Declaration makes it look more complex than the actual one. The signal function associates a given function with a specific signal. Here is the definition in FreeBSD (with a typedef ):
Reference:
Typedef void (* sig_t) (INT );
Sig_t signal (INT Sig, sig_t func );

The first parameter is the target signal. The func parameter is a pointer pointing to a function that processes the signal. This signal processing function carries an int parameter and returns void. The func parameter in the signal function can also be set to the following values:
Reference:
Sig_ign: If the func parameter is set to sig_ign, this signal is ignored.
Sig_dfl: If the func parameter is set to sig_dfl, the signal will be processed according to the determined behavior.
PS: Possible sig signal types:
# Define sighup 1/* hangup */
Sighup is a commonly used signal by UNIX system administrators. Many background service processes will re-read their configuration files after receiving the signal. However, the actual function of this signal is to notify the process that its control terminal is disconnected. The default action is to terminate the process.
# Define SIGINT 2/* interrupt */
For Unix users, SIGINT is another common signal. The combination of many shell CTRL-C makes this signal well known. The formal name of the signal is the interrupt signal. The default action is to terminate the process.
# Define sigquit 3/* Quit */
Sigquit signals are used to receive shell Ctrl-/combinations. In addition, it is used to notify the process to exit. This is a common signal used to notify the application to close calmly (Note: execute some exit actions before the end. The default action is to terminate the process and create a core dump.
# Define sigill 4/* illegal instr. (not reset when caught )*/
If the process being executed contains invalid commands, the operating system sends a sigill signal to the process. If your program uses threads or pointer functions, you can try to capture this signal to assist debugging. The default action is to terminate the process and create a core dump.
# Define sigtrap 5/* trace trap (not reset when caught )*/
The sigtrap signal is defined by the POSIX standard for debugging purposes. When the debugging process receives this signal, it means that it has reached a debugging breakpoint. Once this signal is delivered, the debugged process stops and its parent process is notified. The default action is to terminate the process and create a core dump.
# Define SIGABRT 6/* abort ()*/
SIGABRT provides a way to create a core dump while terminating a process with an exception (abort. However, if the signal is captured and the signal processing handle does not return, the process will not terminate. The default action is to terminate the process and create a core dump.
# Define sigfpe 8/* floating point exception */
When a floating point error occurs in a process, the sigfpe signal is sent to the process. For programs that process complex mathematical operations, we recommend that you capture the signal. The default action is to terminate the process and create a core dump.
# Define sigkill 9/* Kill (cannot be caught or ignored )*/
Sigkill is the most difficult one to deal. As you can see in the comments next to it, this signal cannot be captured or ignored. Once the signal is delivered to a process, the process will be terminated. However, in rare cases, sigkill will not terminate the process. These rare cases occur when dealing with a "non-disruptive operation" (such as disk I/O. Although such a situation rarely occurs, once it occurs, it will cause a process deadlock. The only way to end the process is to restart. The default action is to terminate the process.
# Define sigbus 10/* Bus Error */
As its name implies, when the CPU detects an error on the data bus, it generates a sigbus signal. This signal is generated when the program attempts to access a memory address that is not correctly aligned. The default action is to terminate the process and create a core dump.
# Define SIGSEGV 11/* segmentation violation */
SIGSEGV is a familiar signal from another C/C ++ programmer. When the program does not have the right to access a protected memory address, or access an invalid virtual memory address (dirty pointer, dirty pointers: this is caused by the absence of synchronization with the content in the backup storage. For the wild pointer, see the explanation of the http://en.wikipedia.org/wiki/Wild_pointer .) This signal is generated. The default action is to terminate the process and create a core dump.
# Define sigsys 12/* non-existent System Call invoked */
The sigsys signal is delivered when the process executes a non-existent system call. The operating system will deliver this signal and the process will be terminated. The default action is to terminate the process and create a core dump.
# Define sigpipe 13/* write on a pipe with no one to read it */
The role of a pipeline is like a phone number that allows communication between processes. If the process tries to write data to the MPs queue, but the other side of the MPs queue does not have a responder, the operating system will deliver the sigpipe signal to this annoying process (here is the process to be written ). The default action is to terminate the process.
# Define sigalrm 14/* Alarm clock */
When the timer of a process expires, the sigalrm signal is delivered to the process. These timers are set by the setitimer and alarm call settings described later in this chapter. The default action is to terminate the process.
# Define sigterm 15/* Software termination signal from kill */
The sigterm signal is sent to the process, notifying the process that it is terminated and cleaning activities are performed before it is terminated. The sigterm signal is the default signal sent by the Unix kill command. It is also the default signal sent to the process when the operating system is disabled. The default action is to terminate the process.
# Define sigurg 16/* urgent condition on Io channel */
When a process is opened on a socket, The sigurg is sent to the process. If the process does not capture this signal, it will be discarded. The default action is to discard this signal.
# Define sigstop 17/* sendable stop signal not from tty */
This signal cannot be captured or ignored. Once the process receives the sigstop signal, it immediately stops until it receives another sigcont signal. The default action is to stop the process until a sigcont signal is received.
# Define sigtstp 18/* stop signal from tty */
Similar to sigstop, sigstp differs in that sigstp signals can be captured or ignored. When the shell receives a CTRL-Z from the keyboard, it delivers the (deliver) signal to the process. The default action is to stop the process until a sigcont signal is received.
# Define sigcont 19/* continue a stopped process */
Sigcont is also an interesting signal. As mentioned above, when a process stops, this signal is used to tell the process to resume running. The interesting thing about this signal is that it cannot be ignored or blocked, but can be captured. This makes sense: the process is probably unwilling to ignore or block the sigcont signal. Otherwise, what if the process receives sigstop or sigstp? The default action is to discard the signal.
# Define sigchld 20/* to parent on child stop or exit */
Sigchld is introduced by Berkeley UNIX and has better interfaces than the implementation on SRV 4 UNIX. (If the signal is a non-traceable process, the implementation of the sigchid signal of BSD is better. In the implementation of system v unix, if the process requires this signal to be captured, the operating system checks whether there are any unfinished sub-processes (these sub-processes are exited) and wait for the parent process that calls wait to collect their statuses ). If a sub-process exits with terminating information, the signal processing handle will be called. Therefore, simply capturing this signal will cause the signal processing handle to be called (note: this is the "signal tracing capability" mentioned above), but this is a rather confusing situation .)
Once the sub-process status of a process changes, the sigchld signal will be sent to the process. As I mentioned in the previous chapter, although the parent process can fork out of the child process, there is no need to wait for the child process to exit. In general, this is not very good, because in this case, once the process exits, it may become a zombie process. However, if the parent process captures the sigchld signal, it can use one of the wait series calls to collect the sub-process status or determine what happened. When the sigstop, sigstp, or sigconf signal is sent to the sub-process, the sigchld signal is also sent to the parent process. The default action is to discard the signal.
# Define sigttin 21/* to readers pgrp upon background tty read */
When a background process attempts to perform a read operation, the sigttin signal is sent to the process. The process will be blocked until it receives the sigcont signal. The default action is to stop the process until the sigcont signal is received.
# Define sigttou 22/* Like ttin if (TP-> t_local & ltostop )*/
The sigttou signal is very similar to sigttin. The difference is that the sigttou signal is generated only when the background process tries to perform a write operation on a tty with the tostop attribute. However, if tty does not set this attribute, sigttou will not be sent. The default action is to stop the process until the sigcont signal is received.
# Define sigio 23/* input/output possible signal */
If a process has an I/O operation on a file descriptor, The sigio signal is sent to the process. The process can be set by calling fcntl. The default action is to discard the signal.
# Define sigxcpu 24/* exceeded CPU time limit */
If a process exceeds its CPU limit (CPU limit), The sigxcpu signal is sent to it. You can use the setrlimit settings discussed later. The default action is to terminate the process.
# Define sigxfsz 25/* exceeded file size limit */
If a process exceeds the file size limit, the sigxfsz signal is sent to it. We will continue to discuss this signal later. The default action is to terminate the process.
# Define sigvtalrm 26/* virtual time alarm */
If a process exceeds its virtual timer count, the sigvtalrm signal is sent to it. The default action is to terminate the process.
# Define sigprof 27/* profiling time alarm */
When a timer is set, sigprof is another signal that will be sent to the process. The default action is to terminate the process.
# Define sigwinch 28/* window size changes */
When the process adjusts the terminal's rows or columns (such as increasing your xterm size), The sigwinch signal is sent to the process. The default action is to discard the signal.
# Define SIGUSR1 29/* user defined signal 1 */
# Define sigusr2 30/* user defined Signal 2 */
The SIGUSR1 and sigusr2 signals are designed to be specified by the user. They can be set to fulfill any of your needs. In other words, the operating system does not associate any behavior with these two signals. The default action is to terminate the process.

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.