免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: ccf
打印 上一主题 下一主题

fork的一个例子,好像人家是讲得很详细了,我还是不明白 [复制链接]

论坛徽章:
0
1 [报告]
发表于 2004-07-14 11:43 |显示全部楼层

fork的一个例子,好像人家是讲得很详细了,我还是不明白

我做如下修改

#include <unistd.h>;
#include <sys/types.h>;

main ()
{
        pid_t pid;
        printf("fork!";    // printf("fork!\n";
        pid=fork();

        if (pid < 0)
                printf("error in fork!";
        else if (pid == 0)
                printf("i am the child process, my process id is %d\n",getpid());
        else
                printf("i am the parent process, my process id is %d\n",getpid());
}[/code]

结果是
[root@localhost c]# ./a.out
fork!i am the child process, my process id is 4286
fork!i am the parent process, my process id is 4285

但我改成printf("fork!\n";后,结果是
[root@localhost c]# ./a.out
fork!
i am the child process, my process id is 4286
i am the parent process, my process id is 4285

为什么只有一个fork!打印出来了?上一个为什么有2个?

论坛徽章:
0
2 [报告]
发表于 2004-07-19 15:19 |显示全部楼层

fork的一个例子,好像人家是讲得很详细了,我还是不明白

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP