Using vfork() in do_system() instead of fork()

David Wuertele
Tue Jun 23 18:16:00 GMT 2009

Hi Carlos, thanks for the help!

> Linux implements copy-on-write, therefore after the fork you shouldn't
> be using any more memory (modulo the new structures allocated by the
> kernel). Are you actually seeing memory usage double after the fork? I
> think your problem is elsewhere.

I don't have direct evidence that the memory usage doubles, but system()
only fails if my app has consumed more than half of physical memory.

One test I did was to write a program that allocates and touches more and
more memory, while running system() and my replacement of glibc's
system() that uses vfork() instead of fork().  After the test program
allocates about half of remaining memory, the standard system() starts
returning -1, but my vfork() implementation continues to work right up
until there's really nothing free.

> Using vfork() to implement system() violates POSIX which requires that
> it operate as if fork() were called followed by execl(). It likely
> needs to use fork() to setup blocked signals etc.

It looks like the signals are all set up in do_system() before the fork() is
executed, so maybe that particular requirement is not an issue.

I just read the 2004 version which I found on-line, and it says this:

  "The environment of the executed command shall be as if a child
process were created using fork()"

So they are specifically talking about the environment, which I believe
is identical whether I use fork() or vfork().


More information about the Libc-help mailing list