I am looking for a simple and uncatchable way to terminate the Mac port of my C++ application. In Windows I was using
TerminateProcess(GetCurrentProcess, 0);
What's the equivalent command I can use with Mac OS X / XCode / GCC?
-
exit(0);
Ismael : exit is more like ExitProcess which tries to shutdown the application cleanly, but it might fail. TerminateProces is unconditional and can't be trapped. -
Actually you want
_exit
if you want to have the same semantics asTerminateProcess
.exit
semantics are more closely aligned withExitProcess
. -
Keep in mind that if either you call exit() or TerminateProcess(), you'll get you application terminated immediately, i.e. no destructor calls, no cleanup you may expect to be done is done (of course OS cleans up everything it can).
Ismael : exit will call the functions registered with atexit, so it is not the same. -
A closer to ProcessTerminate will be to send a SIGKILL with kill, both terminate the current process immediately and can't be trapped. This is the same as _exit
kill(getpid(), SIGKILL);
Peter Hosey : Or, a bit easier: `raise(SIGKILL)`. -
Actually, both exit() and _exit() involve the CRT, which means various actions are still taken. (Not sure about atexit, I haven't checked)
TerminateProcess on Windows is on the OS level, so it sidesteps all of the CRT. If you want to do the same thing on the Mac, your best bet is getting your hands dirty with mach functions. 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 comments:
Post a Comment