Page 1 of 1

What's the advantage of fork + exec versus combined?

Posted: Thu Aug 14, 2003 6:37 am
by Candy
Thinking about the process starting methodics, I was wondering why unix uses fork / exec to start a new process, and if fork / forkexec wouldn't be just as useful? I cannot see any reason for a blank exec without fork before it (unless you want to make a "trainer"-something...)

Can anybody explain to me why exec in itself would be a good thing?

Re:What's the advantage of fork + exec versus combined?

Posted: Thu Aug 14, 2003 7:13 am
by Pype.Clicker
it is used by some small program that just prepare a few file descriptors and environment variable for the "main" program before they launch it. If you had to fork() that one, it would have an useless extra cost (as you'll exit() the launcher just after fork_n_exec() ...)

This is used in some 'weird' networking programs like TCPserver, inetd and possibly some of qmail parts (though i'm no longer very sure of the last one)

Re:What's the advantage of fork + exec versus combined?

Posted: Thu Aug 14, 2003 7:24 am
by Candy
if you left it out completely, and would add a new function called exec in the libc

Code: Select all

void execchar *what) {
  fork_and_exec(what);
  exit(0);
}
would that make a real difference for those programs?

Is posix (1003.1) compliance possible with this exec function, provided you don't switch pid (using some weird function)?

Re:What's the advantage of fork + exec versus combined?

Posted: Thu Aug 14, 2003 8:06 am
by mystran
There is also the fact that just using exec() reuses the PID of the process.. which POSIX requires AFAIK, and is sometimes useful as it allows for using wrappers and still being able to tell if the task is still alive.

I think it also preserves some other things like parent and such.

Re:What's the advantage of fork + exec versus combined?

Posted: Sun Aug 17, 2003 4:51 am
by DevL
QNX for example support both exec() and fork() as required by POSIX and a function combining the two. Unfortunately I can't remember what it's called by probably something along the lines of run() or so.

The interesting part is actually asking whether you're interested in supporting POSIX at all.