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

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

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

Post 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?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

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

Post 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)
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

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

Post 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)?
mystran

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

Post 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.
DevL

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

Post 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.
Post Reply