我如何编程方式获得SIGTERM的自定义信号处理器中的默认行为? [英] How can I programmatically get the default behavior of sigterm inside a custom signal handler?

查看:185
本文介绍了我如何编程方式获得SIGTERM的自定义信号处理器中的默认行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于男人-S 7信号,当它没有定义的信号处理程序接收 SIGTERM

默认的操作是期限

我注册了 SIGTERM 自定义信号处理程序,但我想最后的陈述(具体清理工作完成后)在我的自定义处理程序为函数调用实现为期限完全一样的效果。

什么是正确的功能在这里打电话?

我曾尝试退出(0),但导致静态初始化对象的析构函数被调用,这不符合的行为期限。我也曾尝试中止(),并导致核心转储。

在下面的例子中,我使用静态分配对象的生命周期为说明性的例子,但我的问题是不是静态分配的对象只是寿命更加广阔。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&signal.h中GT;结构美孚{
   〜富(){
       fprintf中(标准错误,垂死\\ n!);
   }
   美孚(){
       fprintf中(标准错误,构建\\ n!);
   }
};富富;无效sig_handler(INT预兆报){
//出口(0);
//中止();
}
诠释主(){
//信号(SIGTERM,sig_handler);
    而(1);
}

下面是各种行为:


  1. 的code作为,是将打印构造!而不是快死了!如果 SIGTERM 被接收。

  2. //信号行被注释回,和退出(0)在评论后面,在code将打印构造!快死了!

  3. 退出(0)已被注释掉,而中止()的评论中,该计划将输出构造!然后按中止(核心转储)

要问我的问题,以另一种方式,我想知道什么,我需要把 signal_handler 的身体为了模仿在各个方面的行为1(不只是我已经表明了输出)。


解决方案

 信号(SIGTERM,SIG_DFL);
杀号(getpid(),SIGTERM);

有没有函数调用,如在信号终止是由内核,而不是用户进程处理,但这将恢复默认的处理程序,然后发送信号给自己,杀死就像信号从未过程抓住了。

Based on man -S 7 signal, when a program which has not defined a signal handler receives SIGTERM, the default action is to Term.

I am registering a custom signal handler for SIGTERM, but I want the final statement (after specific cleanup is done) in my custom handler to be a function call that achieves the exact same effect as Term.

What is the correct function to call here?

I have tried exit(0), but that causes the destructors of statically initialized objects to be called, which does not match the behavior of Term. I have also tried abort(), and that causes a core dump.

In the following example, I am using the lifetime of statically allocated objects as an illustrative example, but my question is more broad than just the lifetime of statically allocated objects.

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

struct Foo{
   ~Foo()  {
       fprintf(stderr, "Dying!\n");
   }
   Foo() {
       fprintf(stderr, "Constructing!\n");
   }
};

Foo foo;

void sig_handler(int signo) {
//   exit(0); 
//   abort();
}


int main(){
//    signal(SIGTERM, sig_handler);
    while(1);
}

Here are various behaviors:

  1. The code as-is will print Constructing! but not Dying! if SIGTERM is received.
  2. When the //signal line is commented back in, and exit(0) is commented back in, the code will print both Constructing! and Dying!
  3. When exit(0) is commented out, and abort() is commented in, the program will output Constructing! followed by Aborted (core dumped).

To ask my question in another way, I want to know what I need to put in the body of signal_handler in order to mimic behavior 1 in every way (not just the output I have shown).

解决方案

signal(SIGTERM, SIG_DFL);
kill(getpid(), SIGTERM);

There is no function call, as termination upon signals is handled by the kernel, not the user process, but this will restore the default handler, then send a signal to yourself, which kills the process just like the signal had never been caught.

这篇关于我如何编程方式获得SIGTERM的自定义信号处理器中的默认行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆