fork()之后的PID是什么?

Shr*_*ath 4 fork pid

我在C程序中连续执行3次fork。
1.它会以相同的顺序执行吗?(我的猜测是)。
2.如果我执行pgrep myexecutablefrom shell,是否会按照启动时的顺序给出进程ID?(我的猜测不是,因为您不能保证系统给孩子的pid,对吗?)

ayu*_*ush 5

执行3个fork后,总共将运行8个进程

在此处输入图片说明

因此,现在的pid将取决于创建子进程的顺序以及创建子进程的顺序。

可能像

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
Run Code Online (Sandbox Code Playgroud)