Do you know what signal handling is in C++? May wish to take a look at this

Name: Ah Yue's Little Dongdong

Learning: python, C/C++

Blog homepage:  A Yue's Xiaodongdong's blog_CSDN blog-python&&c++ advanced knowledge, essential for Chinese New Year, blogger in the field of C/C++ knowledge explanation

Table of contents

signal() function

raise() function

function declaration

Call functions

Notes


Signals are interrupts passed to a process by the operating system to terminate a program prematurely. On UNIX, LINUX, Mac OS X, or Windows systems, interrupts can be generated by pressing Ctrl+C.

Some signals cannot be caught by the program, but the signals listed in the following table can be caught in the program, and appropriate actions can be taken based on the signal. These signals are defined in the C++ header file <csignal>.

Signal describe
SIGABRT Abnormal termination of the program, such as calling  abort .
SIGFPE Incorrect arithmetic operations, such as division by zero or operations that cause overflow.
SEAL Detect illegal instructions.
SIGINT Program termination (interrupt) signal.
SIGSEGV Illegal memory access.
TARGET TERM Termination request sent to the program.

signal() function

The C++ signal processing library provides  the signal  function to capture unexpected events. Following is the syntax of signal() function −

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

This looks a bit laborious, the following syntax format is easier to understand:

signal(registered signal, signal handler)

This function receives two parameters: the first parameter is the identifier of the signal to be set, and the second parameter is a pointer to the signal handler function. The function return value is a pointer to the previous signal handler function. If no signal handler was previously set, the return value is SIG_DFL. If the previously set signal handler is SIG_IGN, the return value is SIG_IGN.

Let's write a simple C++ program that catches the SIGINT signal using the signal() function. No matter what signal you want to catch in your program, you must use  the signal  function to register the signal and associate it with a signal handler. Take a look at the example below:

#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 << "123456789" << endl;
       sleep(1);
    }
 
    return 0;
}

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

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

Now, press Ctrl+C to interrupt the program, you will see that the program catches the signal, the program prints the following and exits:

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

 

raise() function

 You can generate signals using the function  raise() , which takes an integer signal number as an argument, with the following syntax:

int raise (signal sig);

Here, sig  is the number of the signal to be sent, these signals include: SIGINT, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGTERM, SIGHUP. Here is an example where we use the raise() function to generate a signal internally:

#include <iostream>
#include <csignal>
#include <unistd.h>
 
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 << "123456789" << 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.

function declaration

A function declaration tells the compiler the name of the function and how to call it. The actual body of the function can be defined separately.

A function declaration consists of the following parts:

return_type function_name( parameter list );

For the function max() defined above, the following is the function declaration:

int max(int num1, int num2);

In a function declaration, the name of the parameter is not important, only the type of the parameter is required, so the following is also a valid declaration:

int max(int, int);

A function declaration is required when you define a function in one source file and call the function in another file. In this case you should declare the function at the top of the file where the function is called.

Call functions

When you create a C++ function, you define what the function does and then call the function to accomplish the defined task.

When a program calls a function, program control is transferred to the called function. The called function performs the defined task, and when the function's return statement is executed, or when the function's closing bracket is reached, program control is returned to the main program.

When calling a function, you pass the required parameters, and if the function returns a value, you can store the return value. For example:

#include <iostream>
using namespace std;
 
// 函数声明
int max(int num1, int num2);
 
int main ()
{
   // 局部变量声明
   int a = 100;
   int b = 200;
   int ret;
 
   // 调用函数来获取最大值
   ret = max(a, b);
 
   cout << "Max value is : " << ret << endl;
 
   return 0;
}
 
// 函数返回两个数中较大的那个数
int max(int num1, int num2) 
{
   // 局部变量声明
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}

Put the max() function and the main() function together and compile the source code. When running the final executable, the following results are produced:

Max value is : 200

Notes

sleep function

Function: Execution is suspended for a period of time, that is, waiting for a period of time to continue execution

Usage: Sleep(time)

Notice:

  •  (1) Sleep is case-sensitive, some compilers are uppercase, some are lowercase.
  •  (2) The time in parentheses of Sleep is in milliseconds in Windows, but in seconds in Linux.
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    int a = 1;
    while (a)
    {
        cout << "欢迎来到阿玥的教程!" << endl;
        Sleep(100);
    }
    system("pause");
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_64122244/article/details/131028458