关于叉和printf /写 [英] About fork and printf/write
本文介绍了关于叉和printf /写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:结果
fork()和输出
运行:
#include<stdio.h>
int main()
{
fork();
printf("b");
if (fork() == 0) {
write(1, "a", 1);
}else{
write(1, "c", 1);
}
return 0;
}
我 cbcabbab
,可能有人解释输出给我吗?如果可能的话,有没有看一步运行的过程步骤的工具吗?
I got cbcabbab
, could someone explain the output to me? And if possible, is there a tool to see the running procedure step by step?
推荐答案
尝试再次运行它,你可能会得到不同的结果。
Try running it again, you'll probably get a different output.
作为一个工具,看一步的步骤一步,我觉得 strace的-f
可能会有点帮助:
As for a tool to see the procedure step by step, I think strace -f
might help a bit:
$ 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
这篇关于关于叉和printf /写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文