在OSx中处理sigterm [英] handling sigterm in OSx

查看:167
本文介绍了在OSx中处理sigterm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有XCode 6内置的控制台C ++应用程序,想向其中添加SIGTERM处理程序.有很多示例,但我无法使它们正常工作.

I have console C++ application built in XCode 6 and want to add SIGTERM handler to it. There are a lot of examples, but I can't get them to work.

#include <csignal>

namespace
{
  volatile std::sig_atomic_t gDone = 0;
}

static void term_handler(int i)
{
  gDone = 1;
}

int main(int argc, const char * argv[])
{
  std::signal(SIGTERM, term_handler);
  while (!gDone);
  return 0;
}

调试器在while语句上停止,但未调用处理程序.这段代码有同样的问题

The debugger stopped on the while statement, but the handler was not called. The same problem with this code

#include <signal.h>

volatile sig_atomic_t gDone = 0;

void term_handler(int i)
{
  gDone = 1;
}

int main(int argc, char* argv[])
{
    struct sigaction sa;
    sigset_t newset;
    sigemptyset(&newset);
    sigaddset(&newset, SIGHUP);
    sigprocmask(SIG_BLOCK, &newset, 0);
    sa.sa_handler = term_handler;
    sigaction(SIGTERM, &sa, 0);

  while(!gDone);  
  return 0;
}

代码是否有问题?在OSX中处理信号的正确方法是什么?

Is there a problem with the code? What is the right way to handle signals in OSX?

推荐答案

好的,我现在回家了,正在Mac上工作.同样,您的代码(特别是第二个示例)已被证明是很好的.确认是在终端用gcc和"kill -TERM"完成的.像正常情况一样,Source指的是SIGTERM,而kill指的是(在OS X上)TERM.您看到的XCode暂停是由于XCode引起的,而不是您的代码.我尝试了两种方式,Terminal和XCode.但是,我找不到阻止这种干扰的偏好.

OK, I'm home now and working on my Mac. Again, your code (second sample specifically) has proven just fine. The confirmation was done in Terminal with gcc and "kill -TERM ". Source refers to SIGTERM, like normal, but kill refers (on OS X) to TERM. The XCode pause you are seeing is due to XCode, not your code. I tried it both ways, Terminal and XCode. I could not find a pref to inhibit that interruption, however.

仅在这里重点介绍...您问,代码是否有问题?答:不.您问,在OSX中处理信号的正确方法是什么?答:您已经在做的方式.新问题:出现信号时如何使XCode(lldb)不暂停?答案:如何告诉LLDB调试器不要处理SIGBUS?

Just to focus here ...You asked, Is there a problem with the code? Answer: No. You asked, What is the right way to handle signals in OSX? Answer: The way you're already doing it. New question: How do I get XCode (lldb) to not pause when signals occur? Answer: How to tell LLDB debugger not to handle SIGBUS?

这篇关于在OSx中处理sigterm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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