/*
* function: 使用sigaction函数注册一个信号的捕捉函数
*
* 2020-12-26
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
// 信号的捕捉函数
void func(int signum)
{
printf("hello world\n");
return;
}
int main(int argc, char *argv[])
{
struct sigaction act, oldact;
act.sa_handler = func; // 信号的捕捉函数的函数名
sigemptyset(&act.sa_mask); // 清空sa_mask屏蔽字,只在func工作时有效
act.sa_flags = 0; // 默认值,使用默认属性
int ret = sigaction(SIGINT, &act, &oldact); // 捕捉2号信号,产生条件按键Ctrl+c
if (-1 == ret)
{
perror("sigaction failed");
exit(1);
}
while(1); // 等待信号产生
return 0;
}
sigaction.cpp
最新推荐文章于 2022-11-10 14:44:07 发布