📜  C++ 中的信号处理

📅  最后修改于: 2022-05-13 01:54:32.703000             🧑  作者: Mango

C++ 中的信号处理

信号是强制操作系统停止其正在进行的任务并参加已发送中断的任务的中断。这些中断可以暂停操作系统的任何程序中的服务。同样,C++ 也提供了可以在程序中捕获和处理的各种信号。以下是 C++ 提供给用户使用的各种信号及其操作的列表。

Signals

Operations

SIGINT

produces a receipt for an active signal

SIGTERM

Sends a termination request to the program

SIGBUS

Bus error which indicates access to an invalid address

SIGILL

Detects an illegal command

SIGALRM

This is used by the alarm() function and indicates the expiration of the timer.

SIGABRT

Termination of a program, abnormally

SIGSTOP

The signal cannot be blocked, handled, and ignored and can stop a process

SIGSEGV

Invalid access to storage

SIGFPE

Overflow operations or mathematically incorrect operations like divide by zero

SIGUSR1

SIGSUR2

User-Defined Signals

此 signal()函数由信号库提供,用于捕获意外中断或事件。

句法:

signal(registered signal, signal handler)

第一个参数是一个整数,表示信号编号,第二个参数是指向信号处理函数的指针。我们必须记住,我们要捕获的信号必须使用信号函数注册,并且必须与信号处理函数相关联。信号处理函数应该是 void 类型。

例子:

CPP
// CPP Program to demonstrate the signal() function
#include 
#include 
using namespace std;
  
void signal_handler(int signal_num)
{
    cout << "The interrupt signal is (" << signal_num
         << "). \n";
  
    // It terminates the  program
    exit(signal_num);
}
  
int main()
{
    // register signal SIGABRT and signal handler
    signal(SIGABRT, signal_handler);
  
    while (true)
        cout << "Hello GeeksforGeeks..." << endl;
    return 0;
}


CPP
// CPP Program to demonstrate the raise() function
#include 
#include 
  
using namespace std;
  
void signal_handler(int signal_num)
{
    cout << "Interrupt signal is (" << signal_num << ").\n";
  
    // It terminates program
    exit(signal_num);
}
  
int main()
{
    int count = 0;
    signal(SIGSEGV, signal_handler);
    // register signal SIGSEGV and signal handler
  
    while (++count) {
        cout << "Hello GeeksforGeeks..." << endl;
        if (count == 5)
            raise(SIGSEGV);
    }
    return 0;
}


输出:在无限循环中,此代码将显示以下输出,直到遇到中断:

Hello GeeksforGeeks...
Hello GeeksforGeeks...
Hello GeeksforGeeks...
Hello GeeksforGeeks...

现在如果我们按Ctrl+C发送中断,程序将通过打印退出:

Hello GeeksforGeeks...
Hello GeeksforGeeks...
Hello GeeksforGeeks...
Hello GeeksforGeeks...
The interrupt signal is (22).

raise()函数

raise()函数用于生成信号。

句法:

raise( signal )

它接受一个参数作为列表中提到的任何函数。

例子:

CPP

// CPP Program to demonstrate the raise() function
#include 
#include 
  
using namespace std;
  
void signal_handler(int signal_num)
{
    cout << "Interrupt signal is (" << signal_num << ").\n";
  
    // It terminates program
    exit(signal_num);
}
  
int main()
{
    int count = 0;
    signal(SIGSEGV, signal_handler);
    // register signal SIGSEGV and signal handler
  
    while (++count) {
        cout << "Hello GeeksforGeeks..." << endl;
        if (count == 5)
            raise(SIGSEGV);
    }
    return 0;
}
输出
Hello GeeksforGeeks...
Hello GeeksforGeeks...
Hello GeeksforGeeks...
Hello GeeksforGeeks...
Hello GeeksforGeeks...
Interrupt signal is (11).