mikeash.com pyblog/friday-qa-2012-01-20-fork-safety.html commentshttp://www.mikeash.com/?page=pyblog/friday-qa-2012-01-20-fork-safety.html#commentsmikeash.com Recent CommentsThu, 28 Mar 2024 22:43:03 GMTPyRSS2Gen-1.0.0http://blogs.law.harvard.edu/tech/rssmikeash - 2012-01-22 19:14:23http://www.mikeash.com/?page=pyblog/friday-qa-2012-01-20-fork-safety.html#commentsCopy on write is an optimization and an implementation detail. Conceptually, the two processes have separate copies of everything from the beginning. Thus a copy gets created if either side modifies memory.6a4df6981fc483df45059d7fcc485df1Sun, 22 Jan 2012 19:14:23 GMTHeath Borders - 2012-01-22 17:35:28http://www.mikeash.com/?page=pyblog/friday-qa-2012-01-20-fork-safety.html#commentsDoes the child's copy-on-write view of the parent change when the parent changes memory? Does the memory only copy if the child modifies it, or if either the child or parent modify it?95bfa5d04a81e1c78c24d51cf0c0a2b7Sun, 22 Jan 2012 17:35:28 GMTGwynne Raskind - 2012-01-21 04:26:04http://www.mikeash.com/?page=pyblog/friday-qa-2012-01-20-fork-safety.html#comments<b>Russell F.:</b> The article actually answers that question, with a little inference: Almost nothing is safe to do between <code>fork()</code> and <code>exec()</code>. CoreFoundation detects the case where you try to do something with it at such a time and blows up on that symbol because that's the easiest way to tell you what went wrong without trying even more things that potentially aren't safe.ac3831292bea2904a562e7dbdc37ba81Sat, 21 Jan 2012 04:26:04 GMTRussell F. - 2012-01-21 03:20:16http://www.mikeash.com/?page=pyblog/friday-qa-2012-01-20-fork-safety.html#commentsThis is a great discussion of fork()/exec() from a general Unix viewpoint, but I was hoping to see additional commentary on Mac-specific issues, such as the reason why there is a function called __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() :-).ef52e53eb41356addd097a3669ede541Sat, 21 Jan 2012 03:20:16 GMTBen G. - 2012-01-20 17:08:48http://www.mikeash.com/?page=pyblog/friday-qa-2012-01-20-fork-safety.html#commentsAnother important thing to note is that, unlike file descriptors, Mach ports are *not* cloned after a fork. <br /> <br />Even if you're careful to only use a single thread in your process, and set up all your Mach ports before fork()ing, all Mach ports in your process are invalidated when you fork. (More specifically, the kernel just doesn't duplicate the ports into the new process.)31fca7c2a98f8019a7b136adf6797a9bFri, 20 Jan 2012 17:08:48 GMT