Re: Small memory penalty on forking and command-execution?

From: Joe Pfeiffer (pfeiffer_at_cs.nmsu.edu)
Date: 04/01/05


Date: 31 Mar 2005 22:44:41 -0700

Anders Christensen <andersc1@hotmail.com> writes:

> I'm making a program for a device with limited memory. My program
> allocates memory using malloc() during runtime (there is enough memory
> to the intended use of the program). Now I need to execute a command from
> within my program, so I'm thinking about forking immediately followed by
> executing the command in the child-process...

That's the standard approach.

> I do not (always) have enough free memory to store an exact copy of the
> parent. I've read that fork() uses copy-on-write, but I can't figure out,
> if the exec-call will cause the parent's memory to be copied. Can someone
> help me on that?

No, it won't. At worst it would only copy those pages that the new
command needs to write over, which are pages the new command needs to
allocate anyway; this would be a time and not a space penalty. I
expect it's actually bright enough to figure out that there's no
point in making the copy at all.

> Which of fork() and vfork() will give the smallest memory penalty? And
> which exec-family function will give the smallest memory penalty?

man vfork

vfork is the winner, by a very small margin (huh. Until I checked
myself, I thought they had become synonyms).

I can't imagine that which flavor of exec() you use could make a
significant difference.

-- 
Joseph J. Pfeiffer, Jr., Ph.D.       Phone -- (505) 646-1605
Department of Computer Science       FAX   -- (505) 646-1002
New Mexico State University          http://www.cs.nmsu.edu/~pfeiffer


Relevant Pages

  • Re: Superpages on amd64 FreeBSD 7.2-STABLE
    ... Especially because vfork is often called a solution. ... Parent with super page ... the child throws away his pages because of the exec!? ... parents memory and then starts with a completely new map. ...
    (freebsd-hackers)
  • Re: Superpages on amd64 FreeBSD 7.2-STABLE
    ... or if squid does something particularly horrible to ... Especially because vfork is often called a solution. ... Parent with super page ... parents memory and then starts with a completely new map. ...
    (freebsd-hackers)
  • Re: [PATCH 4/6] PM/Hibernate: Rework shrinking of memory
    ... just once to make some room for the image and then allocates memory ... We also need a number of page frames to be free during ... which corresponds to the maximum size of a hibernation image. ...
    (Linux-Kernel)
  • Re: [PATCH 0/7] Fixups for duplicate slot names
    ... memory region for slot name will be allocated ... by hotplug *core* driver (not hotplug controller driver). ... in the same component (maybe hotplug core driver in this case). ... hotplug controller driver allocates memory for slot name using ...
    (Linux-Kernel)
  • Re: uninterruptible CLONE_VFORK (Was: oom: Make coredump interruptible)
    ... that the user can hide the memory hog from oom-killer. ... So if the vfork parent is really the culprit, ... vfork child is chosen for oom_kill and killed, ... I've long thought that we should make a vfork parent SIGKILL-able. ...
    (Linux-Kernel)