XCode优雅程序关闭 [英] XCode graceful program shutdown

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

问题描述

我正在xcode中的一个类似服务器的程序,我想让它在调试运行之间正常关闭。基本上以下行不起作用

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
});

我试过捕获其他信号,但到目前为止SIGINT,SIGTERM和SIGABRT没有工作。如果打印出来,xcode如何终止程序

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


程序以退出代码结束:9

Program ended with exit code: 9

到控制台?

EDIT
显然SIGKILL不能被捕获,请参阅此维基百科条目


与SIGTERM和SIGINT不同,此信号不能被捕获或
被忽略,接收过程不能在
接收到此信号后执行任何清除操作。

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


推荐答案

您不能捕获 SIGKILL 。当进程接收到 SIGKILL 时,它立即终止。这用于杀死不会响应或忽略常规 SIGINT 的错误进程。如果你可以捕获 SIGKILL ,并且可能忽略它,那么除了重新启动机器之外,没有办法杀掉一个buggy进程。

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.

此外,请注意,您在信号处理程序中调用 printf(),这是不允许的。

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

您可以在信号中查看信号安全函数。 : http://man7.org/linux/man-pages/man7/ signal.7.html - printf 不是其中的一部分,因此所有投注均已关闭。

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.

从联机帮助页:


信号处理函数必须非常小心,因为处理
在别处可能会在某个任意点在程序的执行
中。 POSIX具有安全功能的概念。如果
信号中断不安全函数的执行,并且处理程序
调用不安全函数,则程序的行为是
未定义。

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.

有关安全的替代方法,请参阅此问题:使用写或异步安全函数从信号处理程序打印int

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

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

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