Re: Small memory penalty on forking and command-execution?
From: Joe Pfeiffer (pfeiffer_at_cs.nmsu.edu)
Date: 31 Mar 2005 22:44:41 -0700
Anders Christensen <email@example.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?
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
-- 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