简体   繁体   English

Mac OS X等效于TerminateProcess(GetCurrentProcess,0);

[英]Mac OS X equivalent for TerminateProcess(GetCurrentProcess,0);

I am looking for a simple and uncatchable way to terminate the Mac port of my C++ application. 我正在寻找一种简单且无法捕获的方法来终止我的C ++应用程序的Mac端口。 In Windows I was using 在我使用的Windows中

TerminateProcess(GetCurrentProcess, 0);

What's the equivalent command I can use with Mac OS X / XCode / GCC? 我可以在Mac OS X / XCode / GCC上使用的等效命令是什么?

Actually you want _exit if you want to have the same semantics as TerminateProcess . 实际上,如果你想拥有与TerminateProcess相同的语义,你想要_exit exit semantics are more closely aligned with ExitProcess . exit语义与ExitProcess更紧密地对齐。

A closer to ProcessTerminate will be to send a SIGKILL with kill , both terminate the current process immediately and can't be trapped. 更接近ProcessTerminate将发送一个带有killSIGKILL ,它们都会立即终止当前进程而不能被捕获。 This is the same as _exit 这与_exit相同

kill(getpid(), SIGKILL);

Actually, both exit() and _exit() involve the CRT, which means various actions are still taken. 实际上,exit()和_exit()都涉及CRT,这意味着仍然会采取各种操作。 (Not sure about atexit, I haven't checked) (不确定atexit,我还没检查)

TerminateProcess on Windows is on the OS level, so it sidesteps all of the CRT. Windows上的TerminateProcess是在操作系统级别上,因此它会回避所有CRT。 If you want to do the same thing on the Mac, your best bet is getting your hands dirty with mach functions. 如果你想在Mac上做同样的事情,你最好的选择是用马赫功能弄脏你的手。 In this case: 在这种情况下:

#include <mach/mach.h>

... // lots of your code here

task_terminate(mach_task_self());

That's about as uncatchable as you can get. 那就像你能得到的一样无法捕捉。

出口(0);


请记住,如果您调用exit()或TerminateProcess(),您将立即终止应用程序,即没有析构函数调用,您可能希望完成的清理工作(当然操作系统会清理它可以做的一切) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM