我在代码中创建子进程。当我调用fork()时,子进程应该从下一个语句开始执行,但在我的代码中,子进程在fork调用之前执行语句。子进程执行fork调用之前写入的语句
#include<stdio.h>
int main()
{
int pid;
FILE *fp;
fp = fopen("oh.txt","w");
fprintf(fp,"i am before fork\n");
pid = fork();
if(pid == 0)
{
fprintf(fp,"i am inside child block\n");
}
else{
fprintf(fp,"i inside parent block\n");
}
fprintf(fp,"i am inside the common block to both parent and child\n");
fclose(fp);
return 0;
}
这是它的输出我得到
OUTPUT:
i am before fork
i inside parent block
i am inside the common block to both parent and child
i am before fork
i am inside child block
i am inside the common block to both parent and child
行“我是叉之前”应一次在文件中写的,但它是由孩子写两次,家长。 为什么是这样?
谢谢。
添加'fflush(FP)'你叉掉了。 – 2013-02-20 16:17:17