C C + + signal programming

Source: Internet
Author: User

1. Header files
#include <signal.h>

2. function
Set the corresponding action of a signal

3. Function prototypes
void (*signal (int signum,void (* handler) (int)))) (int);

Decomposition to see:
typedef void (*sig_t) (int);
sig_t signal (int sig, sig_t func);

The first parameter is a target signal. The func parameter is a pointer to a function that processes the signal. This processing signal function has an int parameter and should return void.
The Func parameter can also be set to some of the following values:
sig_ign: If the func parameter is set to Sig_ign, the signal is ignored.
SIG_DFL: If the func parameter is set to SIG_DFL, the signal is processed according to the determined behavior.

This is the most shameless of the wording, sincerely do not want to let people see ...
    1. void (*signal (int signum,void (* handler) (int)))) (int);
Needs bark a little bit of the notation is:
    1. typedef void (*sig_handler) (int);
    2. Sig_handler signal (int signum, Sig_handler handler);
The first sentence defines a function pointer type called Sig_handler, which takes an int parameter with no return value.
The second sentence declares the signal function, accepts a signum and a sig_handler, and returns the old Sig_handler.

4. Possible types of SIG signals
1) #define SIGHUP 1/* hangup */

Sighup is a common signal used by UNIX system administrators. Many background service processes will re-read their configuration files after receiving the signal. However, the actual function of the signal is to inform the process that its control terminal is disconnected. The default behavior is to terminate the process.

2) #define SIGINT 2/* Interrupt */
For UNIX users, SIGINT is another common signal. Many of the shell's ctrl-c combinations make this signal known to everyone. The official name of the signal is the interrupt signal. The default behavior is to terminate the process.
3) #define SIGQUIT 3/* Quit */
The sigquit signal is used to receive the ctrl-/combination of the shell. In addition, it is used to tell the process to exit. This is a common signal that notifies the application to be closed (i.e., to perform some exit actions before the end). The default behavior is to terminate the process and create a core dump.

4) #define Sigill 4/* illegal instr. (not reset when caught) */
If an illegal instruction is included in the process being executed, the operating system sends a SIGILL signal to the process. If your program uses a thread, or pointer functions, then you can try to capture the signal to assist in debugging. ([color=red] Note: The original sentence is: "If your program makes use of the use of the threads, or pointer functions, try to catch this signal If Possi BLE for aid in debugging. ". In the middle of the two use of the use of, I do not know the original book typesetting flaws or I really do not understand the meaning of it; In addition, I often hear functions pointer, for pointer Functions,google, should be the contents of Fortran, Anyway, really don't know, the exact meaning also please know brother treatise. [/color]) The default behavior is to terminate the process and create a core dump.

5) #define SIGTRAP 5/* Trace trap (not reset when caught) */
Sigtrap This signal is defined by the POSIX standard for debugging purposes. When the signal is received by the debug process, it means that it has reached a debug breakpoint. Once this signal is delivered, the debugged process stops and its parent process is notified. The default behavior is to terminate the process and create a core dump.

6) #define SIGABRT 6/* ABORT () */
SIGABRT provides a way to create a core dump at the same time as an aborted (abort) process. However, if the signal is captured and the signal processing handle is not returned, the process does not terminate. The default behavior is to terminate the process and create a core dump.

7) #define SIGFPE 8/* Floating point exception */
When a floating-point error occurs in the process, the SIGFPE signal is sent to the process. For programs that handle complex math operations, it is generally recommended that you capture the signal. The default behavior is to terminate the process and create a core dump.

8) #define SIGKILL 9/* Kill (cannot be caught or ignored) */
Sigkill is one of the most difficult to deal with in these signals. As you can see in the note next to it, this signal cannot be captured or ignored. Once the signal is delivered to a process, the process terminates. However, there are a few rare cases where sigkill will not terminate the process. These rare situations occur when dealing with a "non-disruptive operation" (such as disk I/O). While such a situation rarely occurs, it can cause a deadlock in the process once it occurs. The only way to end a process is to restart it. The default behavior is to terminate the process.

9) #define Sigbus */Bus Error */
As its name implies, the CPU generates a Sigbus signal when it detects errors on the data bus. This signal is generated when the program attempts to access a memory address that is not properly aligned. The default behavior is to terminate the process and create a core dump.
#define SIGSEGV/* Segmentation violation */
SIGSEGV is a familiar signal to another 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, because there is no synchronization with the contents of the backup memory. For wild pointers, see Http://en.wikipedia.org/wiki/Wild_pointer's explanation. ), this signal is generated. The default behavior is to terminate the process and create a core dump.

ONE) #define SIGSYS */* non-existent system call invoked */
The Sigsys signal is delivered when the process executes a nonexistent system call. The operating system will deliver the signal, and the process will be terminated. The default behavior is to terminate the process and create a core dump.

#define SIGPIPE/* Write on a pipe with no one to read it */
A pipeline acts like a telephone, allowing communication between processes. If a process tries to write to the pipeline, but the other side of the pipeline does not have a response, the operating system will deliver the sigpipe signal to this nasty process (this is the process that intends to write). The default behavior is to terminate the process.

#define SIGALRM/* Alarm Clock */
The SIGALRM signal is delivered (delivered) to the process when the timer for the process expires. These timers will be mentioned later in this chapter
The Setitimer and alarm call settings. The default behavior is to terminate the process.

#define SIGTERM */software termination signal from Kill */
The sigterm signal is sent to the process, notifying the process that it is time to terminate, and doing some cleanup activities before terminating. The sigterm signal is the default signal sent by the UNIX kill command, and is the default signal sent to the process when the operating system shuts down. The default behavior is to terminate the process.

#define Sigurg */Urgent condition on IO channel */
When certain conditions occur on a socket that the process has opened, Sigurg is sent to the process. If the process does not capture this signal, it will be discarded. The default behavior is to discard this signal.

#define SIGSTOP ~/* sendable stop signal not from TTY */
This signal cannot be captured or ignored. Once the process receives the sigstop signal, it stops (stop) immediately until it receives another Sigcont
Signal. The default behavior is to stop the process until a sigcont signal is received.

#define SIGTSTP */* stop signal from TTY */
SIGSTP is similar to Sigstop, where the difference is that SIGSTP signals can be captured or ignored. When the shell receives the ctrl-z from the keyboard, the signal is delivered (deliver) to the process. The default behavior is to stop the process until a sigcont signal is received.

#define SIGCONT */Continue a stopped process */
Sigcont is also an interesting signal. As mentioned earlier, when the process stops, this signal is used to tell the process to resume running. The interesting part of the signal is that it cannot be ignored or blocked, but can be captured. This makes sense: because the process is probably reluctant to ignore or block the sigcont signal, what if the process receives Sigstop or SIGSTP? The default behavior is to discard the signal.
 
) #define SIGCHLD/* To parent on child stop or exit */

#define Sigttin/* to readers pgrp upon background tty read */
When a background process attempts a read operation, the Sigttin signal is sent to the process. The process will block until the sigcont signal is received. The default behavior is to stop the process until the sigcont signal is received.

#define Sigttou */* like Ttin if (tp->t_local&ltostop) */
The Sigttou signal is similar to Sigttin, except that the Sigttou signal is generated when a background process attempts to write to a TTY that has the Tostop property set. However, if the TTY does not have this attribute set, Sigttou will not be sent. The default behavior is to stop the process until the sigcont signal is received.

#define SIGIO */input/output possible signal */
If the process has an I/O operation on a file descriptor, the Sigio signal is sent to the process. The process can be set using the Fcntl tune. The default behavior is to discard the signal.

#define SIGXCPU/* exceeded CPU time limit */
If the process exceeds the CPU limit that it can use (CPU limit), the SIGXCPU signal is sent to it. This restriction can be used with the Setrlimit settings discussed later. The default behavior is to terminate the process.

#define SIGXFSZ/* exceeded file size limit */
If the process exceeds the file size limit it can use, the SIGXFSZ signal is sent to it. We will continue to discuss this signal later. The default behavior is to terminate the process.

#define SIGVTALRM */* Virtual time alarm */
If the process exceeds its set virtual timer count, the SIGVTALRM signal is sent to it. The default behavior is to terminate the process.

#define SIGPROF/* Profiling time alarm */
When a timer is set, SIGPROF is another signal that will be sent to the process. The default behavior is to terminate the process.

#define SIGWINCH/* Window size changes */
When the process adjusts the rows or columns of the terminal (such as increasing the size of your xterm), the sigwinch signal is sent to the process. The default behavior is to discard the signal.

#define SIGUSR1/* User Defined signal 1 */
) #define SIGUSR2/* User Defined Signal 2 */

The two signals SIGUSR1 and SIGUSR2 are designed to be user-specified. They can be set up to complete any of your needs. In other words, the operating system does not have any behavior associated with these two signals. The default behavior is to terminate the process. There seems to be a contradiction between the two words in the original meaning. )

5. Example
5.1. The implementation of CTRL + C under Linux under Windows

The usual practice under Linux:
Signal (SIGINT, sigfunc); Set the signal
void Sigfunc (int signo)
{
...//handling signal-related operations
}

below is the implementation of CTRL + C under Linux under Windows
#include <stdio.h>
#include <windows.h>
static is_loop = 1;
//Capture console functions for CTRL + C events
BOOL Ctrlhandler (DWORD fdwctrltype)
   {
switch (fdwctrltype)
      {
/ * Handle the CTRL-C signal. * *
Case Ctrl_c_event:
printf ("ctrl_c_event \ n");
Break ;
Case Ctrl_close_event:
printf ("ctrl_close_event \ n");
Break ;
Case Ctrl_break_event:
printf ("ctrl_break_event \ n");
Break ;
Case Ctrl_logoff_event:
printf ("ctrl_logoff_event \ n");
Break ;
Case Ctrl_shutdown_event:
printf ("ctrl_shutdown_event \ n");
Break ;
Default:
return FALSE;
      }
is_loop = 0;
return (TRUE);
   }

int main (int argc, char *argv[])
{
printf ("Set Console Ctrl handler\n");
SetConsoleCtrlHandler ((phandler_routine) Ctrlhandler, TRUE);
while (Is_loop);
return 0;
}
 
    5.2.Linux Ctrl + C under Windows implementation two
    #include <stdio.h>
    #include <windows.h>
    #define CONTRL_C_HANDLE () signal (3, exit)
   int main (int argc, char *argv[])
   {
      printf ("Set Console Ctrl handler\n");
      while (1);
      System ("PAUSE");
      return 0;
   }

C C + + signal programming

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.