Subject: Re: popen reentrant (was Re: SA/pthread and vfork)
To: Greywolf <greywolf@starwolf.com>
From: Nathan J. Williams <nathanw@wasabisystems.com>
List: tech-kern
Date: 09/14/2003 16:57:50
Greywolf <greywolf@starwolf.com> writes:

> [parroting what I've just learned about vfork():]
> If you fork(), even if you don't touch any of the pages before calling
> exec(), you still have to copy all the parent's pages into the child.
> This could potentially run you out of (virtual and physical) memory.

Still a little bit off. The fork() call doesn't copy the pages (in any
"modern" MMU-using OS), but copies the pointers in such a way that if
the child writes to the pages, copies will be made for the child as
needed. The two problems with this are:

1) Copying pointers, while faster than copying pages, still isn't
   free, and slows down with the size of the process.
2) The memory *commitment* doubles, though not the actual memory
   consumption, since the child could touch all the pages before
   calling exec(). For the common fork/exec case, this isn't as big of
   a deal as (1).

In contrast, vfork() doesn't even set up the pointers for
copy-on-write operation; the child shares the same VM pointers and can
mess it up for the parent.

        - Nathan