怎么算叉进程位置 [英] How to count the fork processes position

查看:234
本文介绍了怎么算叉进程位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何添加计数器在我的code,以便能够计算每个进程的立场。现在,我得到以下的输出:

 进程的PID:6179 PPID:4378(位置:)。
进程的PID:6181 PPID:6179(位置:)。
进程的PID:6180 PPID:6179(位置:)。
进程的PID:6185 PPID:6180(位置:)。
进程的PID:6183 PPID:6181(位置:)。
进程的PID:6182 PPID:6181(位置:)。
进程的PID:6184 PPID:6180(位置:)。

所有我想要的是能够输出树PID位置:

 过程PID:6179 PPID:4378(位置:1)。
进程的PID:6181 PPID:6179(位置:3)。
进程的PID:6180 PPID:6179(位置:2)。
进程的PID:6185 PPID:6180(位置:7)。
进程的PID:6183 PPID:6181(位置:5)。
进程的PID:6182 PPID:6181(位置:4)。
进程的PID:6184 PPID:6180(位置:6)。

感谢您

 的#include<&unistd.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&; SYS / wait.h>
#包括LT&;&errno.h中GT;
#包括LT&;&math.h中GT;/ *要创建童车* /
#定义NUM_CHILDS 2/ *儿童发射* /
无效launchChild(INT NIVEL,INT N){的printf(过程PID数:%d PPID:%D(位置:?)\\ n,GETPID(),getppid(),N)如果(NIVEL< NUM_CHILDS){
    INT进程;
    过程=叉(); / *子女或父母?* /    如果(处理!= 0){/ *我们是母公司* /
        过程=叉();        如果(进程== 0){/ *我们的孩子* /
            launchChild(NIVEL + 1,n)的;
        }
    }
    其他{/ *我们是母公司* /
        launchChild(NIVEL + 1,n)的;
    }
}
等待(NULL);
}诠释主要(无效){
launchChild(0,1);
}


解决方案

该分叉过程有其父的所有资料至其产卵。这意味着两岔进程知道父的位置。而且你,程序员,知道产卵机制。说,你总是叉两个孩子,到深度为3:

  0
   1 2
 3 4 5 6
7 8 9 A B C D E

儿童为0xA = 10知道这是一个正确的孩子,具有深度为3,其母公司是4:

 <$ C C> own_position =(i_am_right_child 1:0)+ //下父母n个子
               2 *(PARENT_ID - (2 **(own_depth - 1) - 1))+ // 2 *在其行的父的偏移
               2 ** own_depth - 1 //首先自己行的索引10 = 1 + 2 *(4 - 2 **(3 - 1)+ 1)+ 2 ** 3 - 1

如果您叉两个以上的孩子,然后尝试改变2公式中了。

不同的编号系统是可能的。这在这里被称为广度优先搜索

How can I add a counter in my code in order to be able to count the position of each process. Right now I'm getting the following output:

Process Pid: 6179 PPid: 4378 (position: ?).
Process Pid: 6181 PPid: 6179 (position: ?).
Process Pid: 6180 PPid: 6179 (position: ?).
Process Pid: 6185 PPid: 6180 (position: ?).
Process Pid: 6183 PPid: 6181 (position: ?).
Process Pid: 6182 PPid: 6181 (position: ?).
Process Pid: 6184 PPid: 6180 (position: ?).

All I want is to be able to output the Pid position in the tree:

Process Pid: 6179 PPid: 4378 (position: 1).
Process Pid: 6181 PPid: 6179 (position: 3).
Process Pid: 6180 PPid: 6179 (position: 2).
Process Pid: 6185 PPid: 6180 (position: 7).
Process Pid: 6183 PPid: 6181 (position: 5).
Process Pid: 6182 PPid: 6181 (position: 4).
Process Pid: 6184 PPid: 6180 (position: 6).

Thank you

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <math.h>

/*Childs to be created*/
#define NUM_CHILDS 2  

/*Child launcher*/
void launchChild(int nivel,int n){

printf("Process Pid: %d PPid: %d (position: ?).\n",getpid(),getppid(),n); 

if(nivel<NUM_CHILDS){
    int process;    
    process = fork(); /*Child or parent?*/

    if(process!=0){ /*We are parent*/
        process=fork();

        if(process==0){ /*We are child*/
            launchChild(nivel+1,n);
        }
    }
    else{ /*We are parent*/
        launchChild(nivel+1,n);
    }
}
wait(NULL);
}

int main(void){
launchChild(0,1);
}

解决方案

The forked process has all the information of its parent up to its spawning. This means that the forked process knows the "position" of the parent. Further you, the programmer, know the spawn mechanism. Say, you always fork two children, up to a depth of 3:

       0
   1       2
 3   4   5   6
7 8 9 A B C D E

Child 0xA=10 knows it is the right child, has a depth of 3, its parent is 4:

own_position = (i_am_right_child ? 1 : 0) +                  // nth child under parent
               2 * (parent_id - (2**(own_depth - 1) - 1)) +  // 2 * offset of parent in its line
               2**own_depth - 1                              // first index of own line

10 = 1 + 2 * (4 - 2**(3 - 1) + 1) + 2**3 - 1

If you fork more than two children, then try change the 2 in the formula, too.

Different numbering systems are possible. This here is called breadth-first search.

这篇关于怎么算叉进程位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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