- 论坛徽章:
- 0
|
我主要想知道代码中父子进程分别如何执行的,我自己写了一段小代码
#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
int main()
{ -----------------------------------1
FILE * fp=NULL;
int fd,t,i=10;
char buf[50]="i love you";
fp=fopen("./b.c","a+");
if(fp == NULL)
printf("open wrong\n");
if (fwrite(buf,1,15,fp) == 0)
printf("write error\n");
printf("ok\n");
---------------------------------------2
if((t=fork()) >0 )
printf("farther\n");
--------------------------------------3
else if (t == 0)
{
printf("son\n");
fwrite(buf,1,15,fp);
-------------------------------------4
}
return 0;
}
输出:
ok
son
farther
打开b.c
i love you i love you i love you
按照输出的结果推算1~2段执行一次,写了一次 在子进程3~4段也写了一次 可是结果写了三次,这个执行顺序是什么样的啊?????
请指教????
|
|