I’ve got a cocoa app that needs to do some work in a second process (because it might crash due to buggy libraries). I’d like to keep my project as simple as possible, so ideally I would use the same binary as the parent process and just control the child with command line parameters. It would also be nice if the parent could get handles to the new process’s stdin & stdout so that they can communicate (although something I create with pipe() would work too). Has anyone solved this problem before? What did you learn? I come from a Win32/Linux background, so I’m not sure if there are any special capabilities I get with Cocoa/OS X that I should be using.
Share
forkandexecwork the same on Mac OS X as on Linux and other POSIX environments, with one catch: In a Cocoa app, you can’t justforkand notexec, because Core Foundation will not allow you to use any CF- or Cocoa-based APIs in the new process. If youforkin a Cocoa app, you mustexecpretty much immediately afterward.You can
execthe same binary by using your ownargv[0]in the[0]of theargvthat you pass toexec.There is a Cocoa version of
fork+exec: Create an NSTask and set its launch path to your own[[[NSProcessInfo processInfo] arguments] objectAtIndex:0]. There is no way toforkand notexecwith NSTask, for the above reason.pipealso works as you would expect.If you use NSTask, the Cocoa version of
pipeis[NSPipe pipe].