前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >sigaction实例

sigaction实例

作者头像
全栈程序员站长
发布2022-08-24 18:27:34
3880
发布2022-08-24 18:27:34
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <errno.h>

#include <time.h>

#include <sys/stat.h>

#include <sys/fcntl.h>

#include <syslog.h>

#include <signal.h>

static void signal_handle(int sign)

{

printf(“Test:System signal(%d) received,exit \n”,sign);

exit(0);

}

void signal_oact(int sign)

{

printf(“Test:in signal_oact”);

}

void signal_set()

{

struct sigaction act,old_act;

//act

act.sa_handler = signal_handle;

sigemptyset(&act.sa_mask);

act.sa_flags = 0;

//oact

old_act.sa_handler = signal_oact ;

//

if(sigaction(SIGHUP,&act,&old_act) <0)

{

printf(“FATAL errorfor sigaction in function signal_set \n”);

exit(-1);

}

sigaction(SIGSEGV,&act,NULL);

sigaction(SIGINT,&act,&old_act);

sigaction(SIGTERM,&act,NULL);

sigaction(SIGQUIT,&act,NULL);

return;

}

int main()

{

signal_set();

while(1)

{

printf(“test:waitsignal\n”);

sleep(1);

}

return 0;

}

运行后,执行ctrl+c命令,产生一个SIGINT信号,程序会执行 signal_handle函数

SIGINT信号,程序终止(interrupt)信号,在用户键入INTR字符(通常是Ctrl-C)时发出,用于通知前台进程组终止进程。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/141109.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年5月9,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档