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


相关推荐

  • WPF中ListView排序

    WPF中ListView排序//后台代码privatevoidlvList_Click_1(objectsender,RoutedEventArgse){if(e.OriginalSourceisGridViewColumnHeader){//获得点击的列

    2022年10月3日
    3
  • war包解压后怎么重新打war包_war包和zip

    war包解压后怎么重新打war包_war包和zip$ClipboardContent$

    2022年10月4日
    3
  • C语言typedef函数指针用法

    C语言typedef函数指针用法1 简单的函数指针的应用形式 1 返回类型 函数名 参数表 char pFun int charglFun inta return voidmain pFun glFun pFun 2 第一行定义了一个指针变量 pFun 首先我们根据前面提到的 形式 1 认识到它是一个指向某种函数的指针 这种函数参数是一个 int 型 返回值是 char 类型 只有第一句我们还无法使用这个指针 因为我们还未对它进行赋值 第二行定义了一个函数 glFun

    2025年9月21日
    4
  • vim复制粘贴_vim如何复制粘贴

    vim复制粘贴_vim如何复制粘贴一、打开文件        执行vimtest命令打开test文件        在命令模式下输入setnu,展示文本行号二、按字符复制与粘贴        在命令行模式下输入字符v(小写),便可以进入按字符选择模式,通过h、i、j、k键移动光标选择要进行复制的字符串。        完成选择后按下y键进行复制,将鼠标移动到最后一行,按下p执行粘贴操作就完成了对选择…

    2022年4月19日
    60
  • 深入理解CMA【转】

    深入理解CMA【转】

    2021年6月11日
    103
  • springsecurity自定义密码验证_数字图形验证码怎么填

    springsecurity自定义密码验证_数字图形验证码怎么填SpringSecurity添加图形验证码认证功能第一步:图形验证码接口1.使用第三方的验证码生成工具Kaptchahttps://github.com/penggle/kaptcha@ConfigurationpublicclassKaptchaImageCodeConfig{@BeanpublicDefaultKaptchagetDefaultKaptcha(){DefaultKaptchadefaultKaptcha=newDefa

    2025年11月25日
    5

发表回复

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

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