In Unix whenever we want to create a new process, we fork the current process i.e. we create a new child process which is exactly the same as the parent process and then we do exec system call to replace the child process with a new process i.e. we replace all the data for the parent process eith that for the new process.
Why do we create a copy of the parent process in the first place and why don’t we create a new process directly? I am new to Unix please explain in lay-man terms.
3
By separating fork() and execve() you let the parent control the environment that the child inherits.
The most common example is the shell redirecting IO, for example in the following command:
find . -name '*.java' | grep Frob
In this example, the standard output of find
is attached to the standard input of grep
. These are two distinct file descriptors, which are in-memory objects.
In a world where fork
and exec
are separate, the shell contains code that re-assigns file descriptors in the forked child, before exec-ing the new program. In a world where fork and exec are separate, you would need some sort of mechanism to pass this information to the child program, perhaps via some “well-known” shared memory ID.
The former (fork/exec) model is a lot simpler to implement, and allows a default case where the parent and child share many of the same resources.
4