6 ms·
fork() and exec() work well as separate system calls for the common situation where the child (but not the parent) needs to adjust something before executing th
by rtm 17y ago
fork() and exec() work well as separate system calls
for the common situation where the child (but not the
parent) needs to adjust something before executing
the new program. Changing file descriptors to implement
> and < in the shell, for example. It's common to see
sequences like
pid = fork();
if(pid == 0){
close(1);
dup(pipefds[1]);
exec(...);
}
- caf 17y agoAye. This way, we can control a large number of aspects of the child's environment in which execve() is called, without having to have execve() do all that work for us. We can open files, change the session id, reparent the process to init, alter environment variables, lower process limits, change credentials, change the root directory... the possibilities are legion. You wouldn't want to have to design a way to pass all of those things to execve(), would you?