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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Scala 专题指南

    Scala 专题指南

    2022年1月7日
    37
  • shell中的while循环实例[通俗易懂]

    shell中的while循环实例[通俗易懂]1.利用while循环计算1到100的和:示例代码1:#!/bin/bashi=1sum=0while[$i-le100]do letsum=sum+$i leti++doneecho$sum示例代码2:利用while循环计算1到100之间所有奇数之和#!/bin/bashi=1sum=0while[$i-le100]do letsum=sum+$i leti…

    2022年7月24日
    10
  • Boltzmann/Softmax Exploration Strategy[通俗易懂]

    Boltzmann/Softmax Exploration Strategy[通俗易懂]Boltzmann/SoftmaxExplorationStrategy玻尔兹曼探索策略转自:Google图书《TheLogicofAdaptiveBehavior》

    2022年7月12日
    46
  • ASMM、AMM_AMS5666

    ASMM、AMM_AMS56661自动内存管理(AMM):就是在总的内存不变的状态下,实现了内存组件之间的优化配置。不会造成内存溢出的错误。SGA和PGA之间以及SGA内部组件都可以进行内存的储备调整。通过MEMORY_TARGET启用的。2自动共享内存管理(ASMM):是在共享池、高速缓冲区、大池、JAVA池和流池之间进行内存动态重新分配,以提高内存的使用效率。AS

    2025年7月6日
    5
  • gzip和gunzip 解压参数「建议收藏」

    gzip和gunzip 解压参数「建议收藏」Linux压缩保留源文件的方法:gzip–cfilename>filename.gzLinux解压缩保留源文件的方法:gunzip–cfilename.gz>filenamegunzip的用法  1.作用gunzip命令作用是解压文件,使用权限是所有用户。2.格式gunzip[-acfhlLnNqrtvV][-s-Linux压缩保留源文件的方法: g…

    2025年8月27日
    6
  • linux命令_linux挂载cifs报错

    linux命令_linux挂载cifs报错[pcd@localhostax_peta]$petalinux-config–get-hw-description../SG400_top_hw_platform_1INFO:Gettinghardwaredescription…cp:omittingdirectory‘/home/pcd/peta_prj/SG400_top_hw_platform_1/cache…

    2025年10月29日
    4

发表回复

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

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