开发者

Do work out of process in a cocoa app?

开发者 https://www.devze.com 2023-01-18 02:43 出处:网络
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

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.


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.

fork and exec work the same on Mac OS X as on Linux and other POSIX environments, with one catch: In a Cocoa app, you can't just fork and not exec, because Core Foundation will not allow you to use any CF- or Cocoa-based APIs in the new process. If you fork in a Cocoa app, you must exec pretty much immediately afterward.

You can exec the same binary by using your own argv[0] in the [0] of the argv that you pass to exec.

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 to fork and not exec with NSTask, for the above reason.

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).

pipe also works as you would expect.

If you use NSTask, the Cocoa version of pipe is [NSPipe pipe].

0

精彩评论

暂无评论...
验证码 换一张
取 消