问题分叉fork()的多个进程的Unix [英] Problem forking fork() multiple processes Unix

查看:79
本文介绍了问题分叉fork()的多个进程的Unix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个功能是派生的子进程的N多。然而,它似乎比分叉指定了。你能告诉我,我做错了什么?
谢谢

 无效forkChildren(INT nChildren){
    INT I;
    对于(i = 1; I< = nChildren;我++){
        PID =叉();
        如果(PID == 0)
            的printf(我是一个小孩:%d个PID:%d个\\ N,我,GETPID());
    }}

在主我打电话:

  forkChildren(5);

我期待下面的输出:

 我是一个小孩:1 PID:2990
我是一个孩子:2 PID:2991
我是个孩子:3 PID:2992
我是个孩子:4 PID:2993
我是个孩子:5 PID:2994

而是我得到如下:

 我是一个小孩:1 PID:2990
我是一个孩子:2 PID:2991
我是个孩子:3 PID:2992
我是个孩子:4 PID:2993
我是个孩子:5 PID:2994
用户@电脑:〜/目录/ $我是一个孩子:2 PID:2999
我是个孩子:3 PID:3000
我是个孩子:3 PID:3001
我是个孩子:4 PID:3002
我是个孩子:5 PID:3003
我是个孩子:5 PID:3004
我是个孩子:4 PID:3005
我是个孩子:5 PID:3006
我是个孩子:4 PID:3007
我是个孩子:5 PID:3008
我是个孩子:3 PID:3011
我是个孩子:4 PID:3012
我是个孩子:4 PID:3010
我是个孩子:5 PID:3013
我是个孩子:5 PID:3014
我是个孩子:5 PID:3015
我是个孩子:4 PID:3018
我是个孩子:5 PID:3019
我是个孩子:5 PID:3020
我是个孩子:5 PID:3021
我是个孩子:5 PID:3023
我是个孩子:5 PID:3025
我是个孩子:5 PID:3024
我是个孩子:4 PID:3022
我是个孩子:5 PID:3026
我是个孩子:5 PID:3027


解决方案

叉()调用产生一个新的过程,在完全相同的点的叉发生的位置开始其执行的。因此,它看起来像叉子两次返回

这里发生的事情是,你的fork()的调用返回两次,所以无论是家长和孩子过程中不断循环并产生新的进程。每个孩子(包括原来的父母和孩子的),然后再叉,反复加倍的进程数...

So I have this function that forks N number of child processes. However it seems to be forking more than specified. Can you tell me what I'm doing wrong? Thanks

void forkChildren(int nChildren){
    int i;
    for(i = 1; i <= nChildren; i++){
        pid = fork();
        if(pid == 0)    	  
            printf("I'm a child: %d PID: %d\n",i, getpid());
    }

}

In main I call:

forkChildren(5);

I am expecting the following output:

I'm a child: 1 PID: 2990
I'm a child: 2 PID: 2991
I'm a child: 3 PID: 2992
I'm a child: 4 PID: 2993
I'm a child: 5 PID: 2994

But instead I get the following:

I'm a child: 1 PID: 2990
I'm a child: 2 PID: 2991
I'm a child: 3 PID: 2992
I'm a child: 4 PID: 2993
I'm a child: 5 PID: 2994
user@computer:~/directory/$ I'm a child: 2 PID: 2999
I'm a child: 3 PID: 3000
I'm a child: 3 PID: 3001
I'm a child: 4 PID: 3002
I'm a child: 5 PID: 3003
I'm a child: 5 PID: 3004
I'm a child: 4 PID: 3005
I'm a child: 5 PID: 3006
I'm a child: 4 PID: 3007
I'm a child: 5 PID: 3008
I'm a child: 3 PID: 3011
I'm a child: 4 PID: 3012
I'm a child: 4 PID: 3010
I'm a child: 5 PID: 3013
I'm a child: 5 PID: 3014
I'm a child: 5 PID: 3015
I'm a child: 4 PID: 3018
I'm a child: 5 PID: 3019
I'm a child: 5 PID: 3020
I'm a child: 5 PID: 3021
I'm a child: 5 PID: 3023
I'm a child: 5 PID: 3025
I'm a child: 5 PID: 3024
I'm a child: 4 PID: 3022
I'm a child: 5 PID: 3026
I'm a child: 5 PID: 3027

解决方案

The fork() call spawns a new process which begins its execution at the exact same point where the fork occurred. So, it looks like fork "returns twice"

What's happening here is that your fork() call returns twice, so both the parent and child process continue looping and spawning new processes. Each child (of both the original parent and child) then forks again, repeatedly doubling the number of processes...

这篇关于问题分叉fork()的多个进程的Unix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆