Mac OS X: the fastest way to kill / close the entire process tree from a Cocoa application

I know that there are many questions and answers about this, but I am looking for an effective and reliable solution. I need to kill a process and all its child processes from a Cocoa application. I got the process id and what I'm going to execute the code is to execute the kill command

kill -- -<parent PID> 

from my application ... but it seems terribly boorish and cruel to me. Isn't there a better solution? Carbon KillProcess () and his process manager friends do not seem very useful unless I myself create a process tree view. Did I miss something?

I also have code to send an Apple Quit event based on PID. It would be even nicer to send this to each process in the tree defined by the parent process, from the bottom up. But it is only nice. The answer to the first question is the "point".

+4
source share
2 answers

You can simply use killpg to complete the process and everything in your group:

 #include <signal.h> #include <unistd.h> /* ... */ killpg(getpgid(pid), SIGTERM); 

Correct error checking should be done, of course, but you should get the gist. See the killpg(2) pages for kill(2) and killpg(2) for more information.

+8
source

The last time I looked at this (it was a few years ago, but I don’t think much has changed), the best solution I found was to simply call the kill command.

 system( "ps axwww | grep -i CoreServices/Dock.app/Contents/MacOS/Dock | grep -v grep | awk '{print $1}' | xargs kill -3" ); 
0
source

Source: https://habr.com/ru/post/1286208/


All Articles