c语言sigaction,使用的sigaction(),C(Using sigaction(), c)

c语言sigaction,使用的sigaction(),C(Using sigaction(), c)让我们试着去了解什么是你的代码的修改版本发生:#include#includevoidtermination_handler(intsignum){printf(“Hellofromhandler\n”);sleep(1);}intmain(void){//Structsthatwilldescribetheoldactionandthenewaction//a…

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

让我们试着去了解什么是你的代码的修改版本发生:

#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, NULL, &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,&new_action,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信号杀死应用程序。)

如果您想了解更多,并与信号更多的乐趣,你有信号手动和sigaction的手册 ,告诉了很多。 请注意,你也有sigaction的结构的详细描述。

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

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

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


相关推荐

  • laravel 多个项目共享SESSION

    laravel 多个项目共享SESSION

    2021年11月7日
  • Oracle创建表空间和创建临时表空间

    Oracle创建表空间和创建临时表空间/*第1步:创建临时表空间 */createtemporarytablespacekc_temptempfile’C:\app\Administrator\oradata\orcl\kc_temp.dbf’ size50m  autoextendon  next50mmaxsize20480m  extentmanagementlocal;   

    2022年7月27日
  • python官网下载步骤64位-windows下载并安装Python的具体步骤

    python官网下载步骤64位-windows下载并安装Python的具体步骤安装Python下载并安装PythonPython的官网是www.python.org,我们可以直接从官网下载Python。这里介绍在微软Windows和苹果MacOS两种系统中的安装方式。如果Python官网页面之后有所更新,那请大家用自己的思维能力和观察力,大胆地尝试,推测如何下载安装,解决问题。1.5.1Windows系统进入https://www.python.org/页面,选…

    2022年6月3日
  • IIS 安装失败之解决方案[通俗易懂]

    IIS 安装失败之解决方案[通俗易懂]首先删除IIS后,再开始下面的操作:1.运行:regsvr32%windir%/system32/vbscript.dll(其实这一步也可以省,确保万一,还是执行吧)2.运行:msdtc-resetlog3.重新安装IIS…

    2022年6月4日
  • Java 单例模式[通俗易懂]

    Java 单例模式[通俗易懂]单例模式(SingletonPattern)是Java中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。注意:1、单例类只能有一个实例。 2、单例类必须自己创建自己的唯一实例。 3、单例类必须给所有其他对象提供这一实例。…

    2022年7月9日
  • java.net.SocketTimeoutException: connect timed out 的解决办法

    java.net.SocketTimeoutException: connect timed out 的解决办法问题概述在项目中使用FastDFSClient上传图片/文件时,一直上传失败的,报错:java.net.SocketTimeoutException:connecttimedout表示连接失败的,错误信息部分截图如下:具体信息如下:2020-03-0816:41:12,423[localhost-startStop-1-SendThread(192.168.159…

    2022年10月20日

发表回复

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

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