C++ 信号处理

C++ 信号处理

 

[root@flink mediaC]# g++ main.cpp ; ./a.out;
Going to sleep...
Going to sleep...
Going to sleep...
^CInterrupt signal ( 2 ) received.
Going to sleep...
Going to sleep...
Going to sleep...
Going to sleep...
Going to sleep...
Going to sleep...
Going to sleep...
^CInterrupt signal ( 2 ) received.
Going to sleep...
^CInterrupt signal ( 2 ) received.
Going to sleep...
^CInterrupt signal ( 2 ) received.
Going to sleep...
^CInterrupt signal ( 2 ) received.
Going to sleep...
Going to sleep...
Going to sleep...
Going to sleep...

#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;
}

  

当上面的代码被编译和执行时,它会产生下列结果:

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

现在,按 Ctrl+C 来中断程序,您会看到程序捕获信号,程序打印如下内容并退出:

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


Going to sleep...
^CInterrupt signal ( 2 ) received.
Going to sleep...
^CInterrupt signal ( 2 ) received.
Going to sleep...
^CInterrupt signal ( 2 ) received.
Going to sleep...
^CInterrupt signal ( 2 ) received.
Going to sleep...
^CInterrupt signal ( 2 ) received.
Going to sleep...
^CInterrupt signal ( 2 ) received.
Going to sleep...
^CInterrupt signal ( 2 ) received.
Going to sleep...
Going to sleep...
Going to sleep...
Going to sleep...

 

 

无法终止

 

#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;
}

  

raise() 函数

您可以使用函数 raise() 生成信号,该函数带有一个整数信号编号作为参数,语法如下:

int raise (signal sig);

在这里,sig 是要发送的信号的编号,这些信号包括:SIGINT、SIGABRT、SIGFPE、SIGILL、SIGSEGV、SIGTERM、SIGHUP。以下是我们使用 raise() 函数内部生成信号的实例:

 

信号是由操作系统传给进程的中断,会提早终止一个程序。在 UNIX、LINUX、Mac OS X 或 Windows 系统上,可以通过按 Ctrl+C 产生中断。

有些信号不能被程序捕获,但是下表所列信号可以在程序中捕获,并可以基于信号采取适当的动作。这些信号是定义在 C++ 头文件 <csignal> 中。

信号描述
SIGABRT 程序的异常终止,如调用 abort
SIGFPE 错误的算术运算,比如除以零或导致溢出的操作。
SIGILL 检测非法指令。
SIGINT 接收到交互注意信号。
SIGSEGV 非法访问内存。
SIGTERM 发送到程序的终止请求。

 

 

 

 



 

posted @ 2018-10-24 23:13  papering  阅读(248)  评论(0编辑  收藏  举报