Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fork(), exec and waitpid()

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?

like image 473
user1644340 Avatar asked Sep 04 '12 18:09

user1644340


People also ask

What is the difference between fork () and exec () system calls?

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.

What is functionality of fork () and exec ()?

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().

What is the difference between fork () vfork (() and clone ()?

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.

What is the use of fork () and Getpid () system call?

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.


3 Answers

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).

like image 157
Jonathon Reinhart Avatar answered Oct 31 '22 22:10

Jonathon Reinhart


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.

like image 33
pb2q Avatar answered Nov 01 '22 00:11

pb2q


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

like image 21
P.P Avatar answered Oct 31 '22 23:10

P.P