再次尝试运行它,您可能会得到不同的输出。
至于逐步查看过程的工具,我认为strace -f
可能会有所帮助:
$ strace -f ./weirdfork
execve("./weirdfork", ["./weirdfork"], [/* 35 vars */]) = 0
... uninteresting boiler plate removed ...
clone(Process 8581 attached
child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7fe1c7d0b9d0) = 8581
[pid 8580] fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
[pid 8581] fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
[pid 8580] mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 <unfinished ...>
[pid 8581] mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 <unfinished ...>
[pid 8580] <... mmap resumed> ) = 0x7fe1c7d22000
[pid 8581] <... mmap resumed> ) = 0x7fe1c7d22000
[pid 8581] clone( <unfinished ...>
[pid 8580] clone(Process 8582 attached
<unfinished ...>
[pid 8581] <... clone resumed> child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7fe1c7d0b9d0) = 8582
Process 8583 attached
[pid 8580] <... clone resumed> child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7fe1c7d0b9d0) = 8583
[pid 8580] write(1, "c", 1 <unfinished ...>
[pid 8581] write(1, "c", 1cc) = 1
[pid 8580] <... write resumed> ) = 1
[pid 8581] write(1, "b", 1b <unfinished ...>
[pid 8580] write(1, "b", 1 <unfinished ...>
[pid 8581] <... write resumed> ) = 1
b[pid 8581] exit_group(0) = ?
Process 8581 detached
[pid 8580] <... write resumed> ) = 1
[pid 8580] exit_group(0) = ?
[pid 8583] write(1, "a", 1 <unfinished ...>
[pid 8582] write(1, "a", 1a) = 1
a[pid 8582] write(1, "b", 1 <unfinished ...>
[pid 8583] <... write resumed> ) = 1
[pid 8583] write(1, "b", 1b) = 1
[pid 8583] exit_group(0) = ?
Process 8583 detached
b<... write resumed> ) = 1
exit_group(0) = ?
Process 8582 detached