I read the earlier question Differences between fork and exec but it left me with some doubts.
When using fork()
and you call exec on a child, the new process created by exec
is still a child right?
Does killing the parent process kills the child too?
In the drawing/example shown in the top answer, he calls wait
/waitpid
because if the parent process terminates first, the child process dies and then you get partial or no output for the ls
command, is that correct?
fork vs execfork starts a new process which is a copy of the one that calls it, while exec replaces the current process image with another (different) one. Both parent and child processes are executed simultaneously in case of fork() while Control never returns to the original program unless there is an exec() error.
The fork() makes a child's process equal to the parent's process. On the other hand, the exec() makes a child process and replaces it with the parent process. After invoking the fork(), there is a child process and a parent process. In contrast, there is only a child process after invoking the exec().
This is because both processes use the same address space, which contains the stack, stack pointer, and instruction pointer. vfork() acts as a special case of the clone() system call. It creates new processes without copying the address space of the parent process.
System call fork() is used to create processes. It takes no arguments and returns a process ID. The purpose of fork() is to create a new process, which becomes the child process of the caller. After a new child process is created, both processes will execute the next instruction following the fork() system call.
exec
replaces the currently executing process image with a new one. So yes, the child is sill a child process (it is actually the same process.)
No, killing the parent does not kill the child (the child is orphaned).
Killing the parent process does not kill the child. When the child process calls an exec
function, it is still a child process.
In the example from the linked question, the flowchart is roughly describing the process that a shell uses to invoke commands. Unless the command is back-grounded, the shell - the parent process - will wait until the child process terminates before continuing to read commands. Otherwise waiting on the child wouldn't be necessary.
See also this question.
.. the new process created by exec is still a child right?
Yes, it's still the child.
Does killing the parent process kills the child too?
No. If parent dies for whatever reason and child is still executing, then the child will be adopted by the init process (process ID=1) process which will become the new parent of this orphan process.
calls wait/waitpid because if the parent process terminates first, the child...
waitpid/wait is for notifying the status of the child to the parent. Note that, if the parent process has many children then it usually waits for any child unless you specify the process ID of a particular child.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With