c语言sigaction,使用sigaction(),c[通俗易懂]

c语言sigaction,使用sigaction(),c[通俗易懂]让我们试着了解修改后的代码版本会发生什么:#include#includevoidtermination_handler(intsignum){printf(“Hellofromhandler\n”);sleep(1);}intmain(void){//Structsthatwilldescribetheoldactionandthenewaction//ass…

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

让我们试着了解修改后的代码版本会发生什么:

#include

#include

void termination_handler(int signum)

{

printf(“Hello from handler\n”);

sleep(1);

}

int main (void)

{

//Structs that will describe the old action and the new action

//associated to the SIGINT signal (Ctrl+c from keyboard).

struct sigaction new_action,old_action;

//Set the handler in the new_action struct

new_action.sa_handler = termination_handler;

//Set to empty the sa_mask. It means that no signal is blocked

// while the handler run.

sigemptyset(&new_action.sa_mask);

//Block the SEGTERM signal.

// It means that while the handler run,the SIGTERM signal is ignored

sigaddset(&new_action.sa_mask,SIGTERM);

//Remove any flag from sa_flag. See documentation for flags allowed

new_action.sa_flags = 0;

//Read the old signal associated to SIGINT (keyboard,see signal(7))

sigaction(SIGINT,&old_action);

//If the old handler wasn’t SIG_IGN (it’s a handler that just

// “ignore” the signal)

if (old_action.sa_handler != SIG_IGN)

{

//Replace the signal handler of SIGINT with the one described by new_action

sigaction(SIGINT,NULL);

}

while(1)

{

printf(“In the loop\n”);

sleep(100);

}

return 0;

}

因此,如果您编译并启动它,然后按Ctrl C,那么您将执行处理程序消息,然后您立即返回主要的睡眠状态.您可以根据需要多次执行此操作,并且仍会显示处理程序消息和内联消息.

因此,您提供了一个函数,sigaction会执行将信号与处理程序挂钩所需的所有操作.

现在,sigterm怎么样?如果你在termination_handler中增加了睡眠时间,你可以在按下Ctrl C后键入类似“pkill –signal SIGTERM ./a.out”的内容.然后,会发生什么?没有!在termination_handler运行时,SIGTERM信号被阻止.但是一旦你回到主,现在SIGTERM将杀死应用程序.

(请记住,在测试此代码时,您仍然可以通过发送SIGKILL信号来终止应用程序.)

如果你想了解更多,并且对信号有更多的乐趣,你可以使用signal manual和sigaction manual来说明更多信息.请注意,您还具有sigaction结构的详细说明.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

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

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Spring Batch 之 Hello World教程

    Spring Batch 之 Hello World教程SpringBatch之HelloWorld教程本文我们基于springboot和springbatch实现一个简单helloworld入门批处理程序。如果你刚刚接触springbatch,这篇教程会让你花最短时间理解springbatch框架。SpringBatch框架介绍开始代码之前,我们先了解框架中的核心组件,见下图:批处理过程有Job组成,job是封装整…

    2022年5月27日
  • latex参考文献为网址「建议收藏」

    latex参考文献为网址「建议收藏」1、引入hyperref包:\usepackage{hyperref }2、举例:@misc{A:2005,  author = {AlexanderTotok},  title = {hello },  howpublished = {\url{http://cs.nyu.edu/totok/professional/software/tpcw/tpcw.html}}

    2022年9月10日
  • vue组件通讯之$attrs

    vue组件通讯之$attrs使用场景我们会遇到这样的场景,比如对element-ui的el-table组件进行二次开发,但是el-table组件有很多属性比如控制高度的height,传入数据的data还有border、size、fit等一个个的去传显然不大现实,这个使用用$attrs处理起来就会特别优雅。举例子组件:自定义组件对el-table进行封装,用$attrs接收父组件传过来的属性<template&g…

    2022年10月18日
  • Java树工具类,mysql树工具类

    Java树工具类,mysql树工具类依赖<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.6.0</version></dependency><dependency><groupI

    2022年6月29日
  • date函数

    date函数

    2021年10月31日
  • tcping在linux用法,tcping的安装和使用[通俗易懂]

    tcping在linux用法,tcping的安装和使用[通俗易懂]Tcping网上比较少资料是关于linux对tcp端口ping测试的方法,我这里简单介绍2种方法:1.yum安装,编译安装方法1-1.wgethttp://linuxco.de/tcping/tcping-1.3.5.tar.gz###下载tcping1-2tarzxvftcping-1.3.5.tar.gz####解压缩tcping-1.3.51-3cdtcping-1….

    2022年6月23日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号