C++信号处理

小微 科技C++信号处理已关闭评论130字数 1065阅读模式
摘要信号是迫使操作系统停止其正在进行的任务并执行已为其发送中断的任务的中断。 这些中断可以在OS的任何程序中暂停服务。同样,C++还提供了各种可以在程序中捕获和处理的信号。这是C++提...

信号是迫使操作系统休止其正在进行的任务并执行已为其发送中止的任务的中止。 这些中止可以在OS的任何程序中暂停服务。一样,C++还提供了各种可以在程序中捕获以及处理的信号。这是C++提供给用户使用的各种信号及其操作的列表。

该signal函数由signal库提供,用于捕获意外的中止或事件。文章源自微观生活(93wg.com)微观生活-https://93wg.com/8075.html

语法:文章源自微观生活(93wg.com)微观生活-https://93wg.com/8075.html

signal

第一个参数是一个整数,表示信号编号,第二个参数是指向信号处理函数的指针。咱们必需记住,咱们要捕获的信号必需使用signal函数进行注册,并且必需与信号处理函数相关联。信号处理功能应为void类型。文章源自微观生活(93wg.com)微观生活-https://93wg.com/8075.html

示例:文章源自微观生活(93wg.com)微观生活-https://93wg.com/8075.html

include <csignal>
using namespace std; 文章源自微观生活(93wg.com)微观生活-https://93wg.com/8075.html

void signal_handler {
cout << &34;; 文章源自微观生活(93wg.com)微观生活-https://93wg.com/8075.html

// terminate program
exit;
} 文章源自微观生活(93wg.com)微观生活-https://93wg.com/8075.html

int main {
signal;
// register signal SIGABRT and signal handler 文章源自微观生活(93wg.com)微观生活-https://93wg.com/8075.html

while
cout << &34; << endl;
return 0;
}

在无穷循环中,此代码将显示输出,直到呈现中止:文章源自微观生活(93wg.com)微观生活-https://93wg.com/8075.html

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

现在,如果咱们按ctrl + c发送一个中止,程序将通过打印退出:文章源自微观生活(93wg.com)微观生活-https://93wg.com/8075.html

Hello GeeksforGeeks...
Hello GeeksforGeeks...
Hello GeeksforGeeks...
Hello GeeksforGeeks...
The interrupt signal is .

raise函数

raise函数用于生成信号。

语法:

raise

示例:

include <csignal>

using namespace std;

void signal_handler {
cout << &34;;

// terminate program
exit;
}

int main {
int count = 0;
signal;
// register signal SIGSEGV and signal handler

while {
cout << &34; << endl;
if
raise;
}
return 0;
}

输出:

Hello GeeksforGeeks...
Hello GeeksforGeeks...
Hello GeeksforGeeks...
Hello GeeksforGeeks...
Hello GeeksforGeeks...
Interrupt signal is .

以上就是微观生活(93wg.com)关于“C++信号处理”的详细内容,希望对大家有所帮助!

继续阅读
 
小微
  • 版权声明: 本文部分文字与图片资源来自于网络,转载此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请立即通知我们(管理员邮箱:81118366@qq.com),情况属实,我们会第一时间予以删除,并同时向您表示歉意,谢谢!
  • 转载请务必保留本文链接:https://93wg.com/8075.html