Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Signal processing in C


May 11, 2021 C++


Table of contents


The signal processing for C+

The signal is an interrupt from the operating system to the process, which terminates a program early. On UNIX, LINUX, Mac OS X, or Windows systems, you can generate interrupts by pressing Ctrl-C.

Some signals cannot be captured by the program, but the signals listed in the table below can be captured in the program and can be used to take appropriate action based on the signals. These signals are defined in the C++ header file, slt;csignal.

Signal describe
SIGABRT The exception termination of the program, such as call abort
SIGFPE Error arithmetic operations, such as divided by zero or resulting in overflow operations.
SIGILL Detecting illegal instructions.
SIGINT Receive interaction attention signals.
SIGSEGV Illegal access memory.
SIGTERM Send to the termination request of the program.

Signal() function

The signal processing library provides a signal function to capture unexpected events. Here is the syntax of the signal() function:

void (*signal (int sig, void (*func)(int)))(int); 

This function receives two arguments: the first argument is an integer, which represents the number of the signal, and the second argument is a pointer to the signal handler.

Let's write a simple C++ program that uses the signal() function to capture the SIGINT signal. N o matter what signal you want to capture in your program, you must use the signal function to register the signal and associate it with the signal handler. Take a look at the following example:

#include <iostream>
#include <csignal>
#include <unistd.h>

using namespace std;

void signalHandler( int signum )
{
    cout << "Interrupt signal (" << signum << ") received.\n";

    // 清理并关闭
    // 终止程序  

   exit(signum);  

}

int main ()
{
    // 注册信号 SIGINT 和信号处理程序
    signal(SIGINT, signalHandler);  

    while(1){
       cout << "Going to sleep...." << endl;
       sleep(1);
    }

    return 0;
}

When the above code is compiled and executed, it produces the following results:

Going to sleep....
Going to sleep....
Going to sleep....

Now, press Ctrl-C to interrupt the program, and you'll see the program capture the signal, and the program prints the following and exits:

Going to sleep....
Going to sleep....
Going to sleep....
Interrupt signal (2) received.

Raise() function

You can use the function raise() to generate a signal with an integer signal number as an argument, as follows:

int raise (signal sig);

Here, sig is the number of signals to be sent, including: SIGINT, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGTERM, SIGHUP. Here's an example of us generating a signal inside using the raise() function:

#include <iostream>
#include <csignal>

using namespace std;

void signalHandler( int signum )
{
    cout << "Interrupt signal (" << signum << ") received.\n";

    // 清理并关闭
    // 终止程序 

   exit(signum);  

}

int main ()
{
    int i = 0;
    // 注册信号 SIGINT 和信号处理程序
    signal(SIGINT, signalHandler);  

    while(++i){
       cout << "Going to sleep...." << endl;
       if( i == 3 ){
          raise( SIGINT);
       }
       sleep(1);
    }

    return 0;
}

When the above code is compiled and executed, it produces the following results and exits automatically:

Going to sleep....
Going to sleep....
Going to sleep....
Interrupt signal (2) received.