双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

父进程与子进程之间的 困惑!

jqx_ah jqx_ah 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

int main(void)
{
pid_t pid2;
pid_t pid=fork();
if(pid==0)
{
int j ;
for(j=0;j<5;j++)
{
printf("child: %d\n",j);
sleep(1);
}
}
else if (pid>0)
{
int i;
for(i=0;i<5;i++)
{
printf("parent: %d\n",i);
sleep(1);
}
}
else
{
fprintf(stderr,"can't fork ,error %d\n",errno);
exit(1);
}
pid2=fork();
if(pid2==0){printf(" pid2 \n");}
printf("This is the end !\n");
return 0;
}

-------------------------------------------------------------------
./a.out
结果是:jqx2008@jqx55ah:~$ ./a.out
child: 0
parent: 0
child: 1
parent: 1
child: 2
parent: 2
child: 3
parent: 3
child: 4
parent: 4
pid2
This is the end !
pid2
This is the end !
This is the end !
jqx2008@jqx55ah:~$ This is the end !


问题是:一共会产生几个子进程?
输出结果 最后,面那5行怎么解释啊 ?
-------------------------------------------------------
同样这个题目:
./a.out > ty.txt
输出结果却是:

child: 0
child: 1
child: 2
child: 3
child: 4
pid2
This is the end !
parent: 0
parent: 1
parent: 2
parent: 3
parent: 4
pid2
This is the end !
parent: 0
parent: 1
parent: 2
parent: 3
parent: 4
This is the end !
child: 0
child: 1
child: 2
child: 3
child: 4
This is the end !

为什么将结果写进文件 就是这样一个形式了?

大家抽个时间 解释撒!
谢谢哈!
...全文

相关推荐

评论 15
收藏 已收藏
分享
返回顶部
jqx_ah
C语言 70,039 成员 243,237 内容