Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what will be the PID after fork()?

Tags:

fork

pid

I am doing 3 consecutive forks in a C program.
1. Is it going to execute in the same order ? ( My guess is yes ).
2. If I do a pgrep myexecutable from shell, would it give the process ids in the same order as they were started ? ( my guess is no, because you cant guarantee what pid the system gives the child, right ? )

like image 644
Shrinath Avatar asked Feb 25 '23 02:02

Shrinath


1 Answers

There would be a total of 8 processes running after 3 forks are executed

enter image description here

so now the pid would be depend on which order the child process are created by and also in which order the children of children are created.

could be like

main - 12345

child1_of_main_after_fork1  12346

child2_of_child1_after_fork2  12347

child3_of_main_after_fork2 12348

child4_of_main_after_fork3 12349

child5_of_child1_after_fork3 12350

child6_of_child2_after_fork3 12351

child7_of_child3_after_fork3 12352
like image 157
ayush Avatar answered Mar 03 '23 12:03

ayush