Fork (), exec and waitpid ()

I read the previous question Differences between fork and exec , but that left me with some doubts.

When using fork()and you call exec for a child, is the new process created with the help execof the child's rights still?

Killing the parent process also kills the child?

In the drawing / example shown in the top answer , it calls wait/ waitpid, because if the parent process ends first, the child process dies and then you get partial or no output for the command ls, is that right?

+5
source share
4 answers

exec . , , - ( .)

, ( ).

+3

. exec, .

- , . , - - . .

. .

+2

fork() exec , exec, - ?

exec . exec.

?

, , ( , Linux), , .

wait/waitpid, , , ls, ?

( , ), : ) , ) ... , , , , , , .

: :

mike@linux-4puc:~> ps
PID TTY          TIME CMD
18577 pts/2    00:00:00 bash
18643 pts/2    00:00:00 ps

:

void main() 
{ 
    if(fork()){
      printf("parent print");
    }
    else
      while(1);
    printf("done");
}

: " ", "". "ps", , :

mike@linux-4puc:~> ps
PID TTY          TIME CMD
18577 pts/2    00:00:00 bash
18673 pts/2    00:00:02 a.out
18678 pts/2    00:00:00 ps

, a.out , while, .

+2
source
.. the new process created by exec is still a child right?

Yes, this is still a child.

Does killing the parent process kills the child too?

No. If the parent dies for any reason and the child is still performing, then the child will be adopted by the init process (process ID = 1), 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 a parent status notification. Note that if the parent process has many children, it usually waits for any child unless you provide a process identifier for a specific child.

+2
source

All Articles