信号是迫使操作系统休止其正在进行的任务并执行已为其发送中止的任务的中止。 这些中止可以在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
void signal_handler { // terminate program int main { whileinclude <csignal>
using namespace std; 文章源自微观生活(93wg.com)微观生活-https://93wg.com/8075.html
cout << &34;; 文章源自微观生活(93wg.com)微观生活-https://93wg.com/8075.html
exit;
} 文章源自微观生活(93wg.com)微观生活-https://93wg.com/8075.html
signal;
// register signal SIGABRT and signal handler 文章源自微观生活(93wg.com)微观生活-https://93wg.com/8075.html
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
示例:
using namespace std;
void signal_handler { // terminate program int main { while {include <csignal>
cout << &34;;
exit;
}
int count = 0;
signal;
// register signal SIGSEGV and signal handler
cout << &34; << endl;
if
raise;
}
return 0;
}
输出:
Hello GeeksforGeeks...
Hello GeeksforGeeks...
Hello GeeksforGeeks...
Hello GeeksforGeeks...
Hello GeeksforGeeks...
Interrupt signal is .
以上就是微观生活(93wg.com)关于“C++信号处理”的详细内容,希望对大家有所帮助!
评论