Subject: system() function

From: "Siders, Keith" <[email-masked]>
Date: Thu, 30 May 2002 11:01:30 -0500

I was planning to use system() to invoke a shell and launch a script.
However it appears that this causes the parent process to terminate. A note
in Linux Programming Bible (Goerzen, 2000) says to never invoke a shell or
use the system() function. Having looked at fork() and exec(), these will
require obscene amounts of memory and overhead (for an embedded box). I've
also looked at vfork() and execve(), which looks like it will do what I
want. So do I do the vfork()/execve() pair, or is there a better way? And
would sigaction() handling be the way to pass progress information from the
child back to the parent process?

Keith Siders
Software Engineer
 Toshiba America Consumer Products, Inc.
Advanced Television Technology Center
801 Royal Parkway, Suite 100
Nashville, Tennessee 37214
Phone: (615) 257-4050
Fax:   (615) 453-7880

---

From: "Justin Carlson" <[email-masked]>
Date: 30 May 2002 11:42:01 -0700

On Thu, 2002-05-30 at 09:01, Siders, Keith wrote:
> require obscene amounts of memory and overhead (for an embedded box).

I'm not sure that fork() qualifies as an "obscene amount of overhead". 
That may have been true before copy-on-write, but the only thing vfork()
saves you with respect to fork() is page table construction, which is
not particularly memory or processor intensive.

> I've

As a personal preference, I dislike vfork() since it's pretty
nonstandard in its behaviour on linux.  Generally, though, it sounds
like maybe you'd benefit from a copy  of Kernighan & Pike's _The Unix
Programming Environment_.  There are more ways to do interprocess
communication than you can shake a stick at, and which one is "right"
for you depends largely on your specific application.  Signals is a
reasonable way (though if you vfork() you won't be able to handle
signals in your parent concurrent with the child running...).  There are
also named pipes, shared memory, SysV messaging...

-Justin

---

From: Alan Cox <[email-masked]>
Date: 31 May 2002 00:02:45 +0100

> I was planning to use system() to invoke a shell and launch a script.

You can do that yes

> However it appears that this causes the parent process to terminate. A note

No it doesn't

> require obscene amounts of memory and overhead (for an embedded box). I've

Nope.

> child back to the parent process?

system is actually implemented using either vfork/execve or fork/execve
to run your command through the shell. It works great but you do need to
remember its going via the shell so things like "*" will be expanded.

fork creates a copy on write clone of the process (ie the program data
is not actually copied unless either task writes to it), so it generally
uses very little ram indeed, especially when one of them exec's
something