X $ C $ç优雅程序关闭 [英] XCode graceful program shutdown

查看:157
本文介绍了X $ C $ç优雅程序关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作以x code服务器般的程序,我希望它的调试运行之间的正常关闭。基本上下面的行不工作

 的std ::信号(SIGKILL,[](INT){
    的printf(得到SIGKILL这里\\ n!);
    //关机code在这里
});

我试过诱捕其他信号,但到目前为止SIGINT,SIGTERM和SIGABRT都没有奏效。 X code是如何终止程序,如果它打印


  

程序,退出code结束:9


到控制台?

修改
显然SIGKILL不能被捕获,见此维基百科条目。


  

在对比SIGTERM和SIGINT,这个信号不能被捕获或
  忽略,并且接收处理不能执行对任何清理
  接收到该信号



解决方案

您不能赶上 SIGKILL 。当一个进程接收 SIGKILL ,它会立即终止。这是用来杀死不会响应或忽略常规 SIGINT 越野车的过程。如果你能赶上 SIGKILL ,并可能忽略它,那么就没有办法从重启机器拆开杀死一个越野车的过程。

此外,请注意,我们在调用的printf()在信号处理程序,这是不允许的。你不能指望这个工作。

您可以在信号手册页看看信号安全功能:的 http://man7.org/linux/man-pages/man7/signal.7.html - 的printf 是不是它的一部分,因此,所有的赌注都关闭。

从手册页:


  

一个信号处理函数必须非常小心,因为处理
  别处可能在执行某任意点中断
  该方案。 POSIX有安全功能的概念。如果一个
  信号中断不安全函数的执行和处理程序
  调用一个不安全的功能,则程序的行为是
  不确定的。


请参阅安全的替代品这个问题:<一href=\"http://stackoverflow.com/questions/14573000/print-int-from-signal-handler-using-write-or-async-safe-functions\">Print用写或异步的安全保护功能从信号处理程序INT

I'm working on a server-like program in xcode, and I want it to shut down gracefully between debug runs. Basically the following line is not working

std::signal(SIGKILL, [](int){
    printf("got sigkill here!\n"); 
    //shut-down code here
});

I've tried trapping other signals, but so far SIGINT, SIGTERM and SIGABRT have not worked. How is xcode terminating the program, if it prints

Program ended with exit code: 9

to the console?

EDIT Apparently SIGKILL can not be caught, see this wikipedia entry.

In contrast to SIGTERM and SIGINT, this signal cannot be caught or ignored, and the receiving process cannot perform any clean-up upon receiving this signal

解决方案

You can't catch SIGKILL. When a process receives SIGKILL, it immediately terminates. This is used to kill buggy processes that won't respond or ignore the regular SIGINT. If you could catch SIGKILL, and possibly ignore it, then there would be no way to kill a buggy process apart from rebooting the machine.

Also, note that you're calling printf() in a signal handler, which is not allowed. You can't expect this to work.

You can have a look at signal-safe functions in signal manpage: http://man7.org/linux/man-pages/man7/signal.7.html - printf is not part of it, so all bets are off.

From the manpage:

A signal handler function must be very careful, since processing elsewhere may be interrupted at some arbitrary point in the execution of the program. POSIX has the concept of "safe function". If a signal interrupts the execution of an unsafe function, and handler calls an unsafe function, then the behavior of the program is undefined.

See this question for safe alternatives: Print int from signal handler using write or async-safe functions

这篇关于X $ C $ç优雅程序关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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