📜  C++信号处理

📅  最后修改于: 2020-12-17 05:16:45             🧑  作者: Mango


信号是操作系统传递给进程的中断,可以过早地终止程序。您可以通过在UNIX,LINUX,Mac OS X或Windows系统上按Ctrl + C来生成中断。

有些信号无法被程序捕获,但是下面列出了一些信号,您可以在程序中捕获它们,并可以根据信号采取适当的措施。这些信号在C++头文件中定义。

Sr.No Signal & Description
1

SIGABRT

Abnormal termination of the program, such as a call to abort.

2

SIGFPE

An erroneous arithmetic operation, such as a divide by zero or an operation resulting in overflow.

3

SIGILL

Detection of an illegal instruction.

4

SIGINT

Receipt of an interactive attention signal.

5

SIGSEGV

An invalid access to storage.

6

SIGTERM

A termination request sent to the program.

signal()函数

C++信号处理库提供函数信号以捕获意外事件。以下是signal()函数的语法-

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

为简单起见,此函数接收两个参数:第一个参数为表示信号编号的整数,第二个参数为指向信号处理函数的指针。

让我们编写一个简单的C++程序,使用signal()函数捕获SIGINT信号。无论您想在程序中捕获什么信号,都必须使用信号函数注册该信号并将其与信号处理程序关联。检查以下示例-

#include 
#include 

using namespace std;

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

   // cleanup and close up stuff here  
   // terminate program  

   exit(signum);  
}

int main () {
   // register signal SIGINT and signal handler  
   signal(SIGINT, signalHandler);  

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

   return 0;
}

编译并执行上述代码后,将产生以下结果-

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

现在,按Ctrl + c中断程序,您将看到您的程序将捕获信号并通过打印以下内容将其输出-

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

raise()函数

您可以通过函数raise()生成信号,该函数采用整数信号号作为参数,并具有以下语法。

int raise (signal sig);

此处, sig是发送任何信号的信号编号:SIGINT,SIGABRT,SIGFPE,SIGILL,SIGSEGV,SIGTERM,SIGHUP。以下是我们使用raise()函数在内部产生信号的示例,如下所示:

#include 
#include 

using namespace std;

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

   // cleanup and close up stuff here  
   // terminate program  

   exit(signum);  
}

int main () {
   int i = 0;
   // register signal SIGINT and signal handler  
   signal(SIGINT, signalHandler);  

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

   return 0;
}

当上面的代码被编译和执行时,它会产生以下结果,并且会自动产生-

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