免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2404 | 回复: 7
打印 上一主题 下一主题

关于fork的问题(也关于输出重定向) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-12-07 14:38 |只看该作者 |倒序浏览
为什么两个结果差异这样大呢?哪位高手给个指点嘛?

源程序MyFork.c

#include "test.h"

main(void)
{
    pid_t pid;
    /*static*/ int iCount = 0;

    printf("Before fork: pid=%d \n", getpid());

    /* Fork MAXP precesses */
    for (iCount=0; iCount<MAXP; iCount++)
    {
        pid = fork();

        /* Something wrong when fork a precess */
        if (pid < 0)
        {
            printf("ERROR\n";
        }
        else if (0 == pid) /* Child precess */
        {
            printf("Child:  \tiCount=%d, pid=%d, \tParent: pid=%d\n",
                           iCount, getpid(), getppid());
/*            break;
*/
        }
        else /* Parent precess */
        {
            sleep(1);
            wait();
            printf("arent: \tiCount=%d, pid=%d, \tParent: pid=%d\n",
                           iCount, getpid(), getppid());
/*            break;
*/
        }
    }

    printf("The end of precess: pid=%d \n\n", getpid());
}


头文件test.h
#define MAXP = 3

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


编译该程序:cc -s MyFork.c -o fork_test


运行fork_test,输出如下:

Before fork: pid=7402
Child:          iCount=0, pid=7403,     Parent: pid=7402
Child:          iCount=1, pid=7404,     Parent: pid=7403
Child:          iCount=2, pid=7405,     Parent: pid=7404
Child:          iCount=3, pid=7406,     Parent: pid=7405
The end of precess: pid=7406

Parent:         iCount=3, pid=7405,     Parent: pid=7404
The end of precess: pid=7405

Parent:         iCount=2, pid=7404,     Parent: pid=7403
Child:          iCount=3, pid=7407,     Parent: pid=7404
The end of precess: pid=7407

Parent:         iCount=3, pid=7404,     Parent: pid=7403
The end of precess: pid=7404

Parent:         iCount=1, pid=7403,     Parent: pid=7402
Child:          iCount=2, pid=7408,     Parent: pid=7403
Child:          iCount=3, pid=7409,     Parent: pid=7408
The end of precess: pid=7409

Parent:         iCount=3, pid=7408,     Parent: pid=7403
The end of precess: pid=7408

Parent:         iCount=2, pid=7403,     Parent: pid=7402
Child:          iCount=3, pid=7411,     Parent: pid=7403
The end of precess: pid=7411

Parent:         iCount=3, pid=7403,     Parent: pid=7402
The end of precess: pid=7403

Parent:         iCount=0, pid=7402,     Parent: pid=6214
Child:          iCount=1, pid=7412,     Parent: pid=7402
Child:          iCount=2, pid=7413,     Parent: pid=7412
Child:          iCount=3, pid=7414,     Parent: pid=7413
The end of precess: pid=7414

Parent:         iCount=3, pid=7413,     Parent: pid=7412
The end of precess: pid=7413

Parent:         iCount=2, pid=7412,     Parent: pid=7402
Child:          iCount=3, pid=7415,     Parent: pid=7412
The end of precess: pid=7415

Parent:         iCount=3, pid=7412,     Parent: pid=7402
The end of precess: pid=7412

Parent:         iCount=1, pid=7402,     Parent: pid=6214
Child:          iCount=2, pid=7417,     Parent: pid=7402
Child:          iCount=3, pid=7418,     Parent: pid=7417
The end of precess: pid=7418

Parent:         iCount=3, pid=7417,     Parent: pid=7402
The end of precess: pid=7417

Parent:         iCount=2, pid=7402,     Parent: pid=6214
Child:          iCount=3, pid=7422,     Parent: pid=7402
The end of precess: pid=7422

Parent:         iCount=3, pid=7402,     Parent: pid=6214
The end of precess: pid=7402


运行fork_test>result.log
vi result.log,显示如下:

Before fork: pid=6864
Child:          iCount=0, pid=6865,     Parent: pid=6864
Child:          iCount=1, pid=6866,     Parent: pid=6865
Child:          iCount=2, pid=6867,     Parent: pid=6866
Child:          iCount=3, pid=6868,     Parent: pid=6867
The end of precess: pid=6868

Before fork: pid=6864
Child:          iCount=0, pid=6865,     Parent: pid=6864
Child:          iCount=1, pid=6866,     Parent: pid=6865
Child:          iCount=2, pid=6867,     Parent: pid=6866
Parent:         iCount=3, pid=6867,     Parent: pid=6866
The end of precess: pid=6867

Before fork: pid=6864
Child:          iCount=0, pid=6865,     Parent: pid=6864
Child:          iCount=1, pid=6866,     Parent: pid=6865
Parent:         iCount=2, pid=6866,     Parent: pid=6865
Child:          iCount=3, pid=6869,     Parent: pid=6866
The end of precess: pid=6869

Before fork: pid=6864
Child:          iCount=0, pid=6865,     Parent: pid=6864
Child:          iCount=1, pid=6866,     Parent: pid=6865
Parent:         iCount=2, pid=6866,     Parent: pid=6865
Parent:         iCount=3, pid=6866,     Parent: pid=6865
The end of precess: pid=6866

Before fork: pid=6864
Child:          iCount=0, pid=6865,     Parent: pid=6864
Parent:         iCount=1, pid=6865,     Parent: pid=6864
Child:          iCount=2, pid=6870,     Parent: pid=6865
Child:          iCount=3, pid=6871,     Parent: pid=6870
The end of precess: pid=6871

Before fork: pid=6864
Child:          iCount=0, pid=6865,     Parent: pid=6864
Parent:         iCount=1, pid=6865,     Parent: pid=6864
Child:          iCount=2, pid=6870,     Parent: pid=6865
Parent:         iCount=3, pid=6870,     Parent: pid=6865
The end of precess: pid=6870

Before fork: pid=6864
Child:          iCount=0, pid=6865,     Parent: pid=6864
Parent:         iCount=1, pid=6865,     Parent: pid=6864
Parent:         iCount=2, pid=6865,     Parent: pid=6864
Child:          iCount=3, pid=6872,     Parent: pid=6865
The end of precess: pid=6872

Before fork: pid=6864
Child:          iCount=0, pid=6865,     Parent: pid=6864
Parent:         iCount=1, pid=6865,     Parent: pid=6864
Parent:         iCount=2, pid=6865,     Parent: pid=6864
Parent:         iCount=3, pid=6865,     Parent: pid=6864
The end of precess: pid=6865

Before fork: pid=6864
Parent:         iCount=0, pid=6864,     Parent: pid=6214
Child:          iCount=1, pid=6873,     Parent: pid=6864
Child:          iCount=2, pid=6874,     Parent: pid=6873
Child:          iCount=3, pid=6875,     Parent: pid=6874
The end of precess: pid=6875

Before fork: pid=6864
Parent:         iCount=0, pid=6864,     Parent: pid=6214
Child:          iCount=1, pid=6873,     Parent: pid=6864
Child:          iCount=2, pid=6874,     Parent: pid=6873
Parent:         iCount=3, pid=6874,     Parent: pid=6873
The end of precess: pid=6874

Before fork: pid=6864
Parent:         iCount=0, pid=6864,     Parent: pid=6214
Child:          iCount=1, pid=6873,     Parent: pid=6864
Parent:         iCount=2, pid=6873,     Parent: pid=6864
Child:          iCount=3, pid=6876,     Parent: pid=6873
The end of precess: pid=6876

Before fork: pid=6864
Parent:         iCount=0, pid=6864,     Parent: pid=6214
Child:          iCount=1, pid=6873,     Parent: pid=6864
Parent:         iCount=2, pid=6873,     Parent: pid=6864
Parent:         iCount=3, pid=6873,     Parent: pid=6864
The end of precess: pid=6873

Before fork: pid=6864
Parent:         iCount=0, pid=6864,     Parent: pid=6214
Parent:         iCount=1, pid=6864,     Parent: pid=6214
Child:          iCount=2, pid=6877,     Parent: pid=6864
Child:          iCount=3, pid=6878,     Parent: pid=6877
The end of precess: pid=6878

Before fork: pid=6864
Parent:         iCount=0, pid=6864,     Parent: pid=6214
Parent:         iCount=1, pid=6864,     Parent: pid=6214
Child:          iCount=2, pid=6877,     Parent: pid=6864
Parent:         iCount=3, pid=6877,     Parent: pid=6864
The end of precess: pid=6877

Before fork: pid=6864
Parent:         iCount=0, pid=6864,     Parent: pid=6214
Parent:         iCount=1, pid=6864,     Parent: pid=6214
Parent:         iCount=2, pid=6864,     Parent: pid=6214
Child:          iCount=3, pid=6879,     Parent: pid=6864
The end of precess: pid=6879

Before fork: pid=6864
Parent:         iCount=0, pid=6864,     Parent: pid=6214
Parent:         iCount=1, pid=6864,     Parent: pid=6214
Parent:         iCount=2, pid=6864,     Parent: pid=6214
Parent:         iCount=3, pid=6864,     Parent: pid=6214
The end of precess: pid=6864

论坛徽章:
0
2 [报告]
发表于 2005-12-07 14:54 |只看该作者
加fflush(stdout)再比较结果.
或者用write来输出.

论坛徽章:
0
3 [报告]
发表于 2005-12-07 16:39 |只看该作者
不对吧,你不是break出去了吗?怎么还会有运行这么多次呢
照你给的程序主子进程都应该只执行一次

论坛徽章:
0
4 [报告]
发表于 2005-12-07 16:43 |只看该作者

break那句话被我注释了。

原帖由 bugzilla_zhu 于 2005-12-7 16:39 发表
不对吧,你不是break出去了吗?怎么还会有运行这么多次呢
照你给的程序主子进程都应该只执行一次


break那句话被我注释了。

论坛徽章:
0
5 [报告]
发表于 2005-12-07 17:22 |只看该作者

那是什么原因造成的呢

原帖由 renstone921 于 2005-12-7 14:54 发表
加fflush(stdout)再比较结果.
或者用write来输出.


那是什么原因造成的呢?是不是因为父进程和子进程共享输出缓存什么的问题?糊涂啊

论坛徽章:
0
6 [报告]
发表于 2005-12-07 18:39 |只看该作者

那是什么原因造成的呢

标准输出连接到终端设备是行缓冲的,其它情况是全缓冲的
直接运行程序 很快就会被新的一行冲掉

论坛徽章:
0
7 [报告]
发表于 2005-12-07 20:48 |只看该作者
setvbuf
lzcxh 该用户已被删除
8 [报告]
发表于 2005-12-07 22:33 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP