fork Cocoa process and re-init Cocoa. how?

2019-06-06 17:42发布

问题:

I tried to fork() a Cocoa process and setup a new Cocoa/ObjC environment but I get the error:

The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.

Is there a way to re-initialize Cocoa/ObjC in the forked process?


I know that the documentation about fork() does say it is limited, however I wonder if there is still a way to do it, not just what the documentation says.

I tried to read the source code of the XNU kernel which handles the execve syscall (bsd/kern/kern_exec.c) but I am not that used to the code that I cannot easily say wether it is possible or not to do something similar in userspace or not.

回答1:

Mac OS X severely limits what you can do after a fork without execing. See the CAVEATS section of the fork man page. The allowed actions do not include using Cocoa or CoreFoundation, so you must call exec to start a new program.