简体   繁体   English

叉树C,子进程PID

[英]fork tree C, child processes PID

I have to implement fork tree as in this picture . 我必须实现这张图中的 fork树。

               +----+
      +--------| P1 |---------+
     /         +----+          \
    /         /     \           \
 +----+   +----+     +----+   +----+
 | P2 |   | P3 |     | P4 |   | P5 |
 +----+   +----+     +----+   +----+
           /  \                /   \
      +----+ +----+        +----+ +----+
      | P6 | | P7 |        | P8 | | P9 |
      +----+ +----+        +----+ +----+
               |
             +-----+
             | P10 |
             +-----+

Output must return something like: 输出必须返回如下内容:

   P1: pid: 1337 ppid: 1336 child processes pids: 1338, 1339, 1340, 1341
   P2: pid: 1338 ppid: 1337 child processes pids: 1342, 1342... etc.

where the child processes pids are pids of every child process of PX, and I don't know how to go about it. 子进程pid是每个PX子进程的pid,我不知道该如何处理。 Can you give me some advie? 你能给我一些建议吗? My code so far is below, it creates the tree correctly imo, the child processes are a problem. 到目前为止,我的代码在下面,它正确地创建了imo树,子进程是一个问题。

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

    int main(int argc, char *argv[])
   {
       int pidp1;
      int pidp2;



switch (pidp1 = fork())
{
    case 0:
        printf("P2: pid: %d ppid: %d\n", getpid(), getppid());
        exit (0);
        break;
    case -1:
        printf("Blad funkcji\n");
        exit (1);
    default:
        printf("P1: pid: %d ppid: %d child processes pids: %d\n", getpid(), getppid(), pidp1);
        wait(NULL);
        switch (pidp1 = fork())
        {
            case 0:
                printf("P3: pid: %d ppid: %d\n", getpid(), getppid());
                switch (pidp1 = fork())
                {
                    case 0:
                        printf("P6: pid: %d ppid: %d \n", getpid(), getppid());
                        exit (0);
                        break;
                    case -1:
                        printf("Blad funkcji\n");
                        exit (1);
                    default:
                        wait(NULL);
                        switch (pidp1 = fork())
                        {
                            case 0:
                                printf("P7: pid: %d ppid: %d\n", getpid(), getppid());
                                switch (pidp1 = fork())
                                {
                                    case 0:
                                        printf("P10: pid: %d ppid: %d\n", getpid(), getppid());
                                        exit (0);
                                        break;
                                    case -1:
                                        printf("Blad funkcji\n");
                                        exit (1);
                                    default:
                                        wait(NULL);
                                        exit (0);
                                }
                                exit (0);
                                break;
                            case -1:
                                printf("Blad funkcji\n");
                                exit (1);
                            default:
                                wait(NULL);
                                exit (0);
                        }
                        exit (0);
                }
                exit (0);
                break;
            case -1:
                printf("Blad funkcji\n");
                exit (1);
            default:
                wait(NULL);
                switch (pidp1 = fork())
                {
                    case 0:
                        printf("P4: pid: %d ppid: %d\n", getpid(), getppid());
                        exit (0);
                        break;
                    case -1:
                        printf("Blad funkcji\n");
                        exit (1);
                    default:
                        wait(NULL);
                        switch (pidp1 = fork())
                        {
                            case 0:
                                printf("P5: pid: %d ppid: %d\n", getpid(), getppid());
                                switch (pidp1 = fork())
                                {
                                    case 0:
                                        printf("P8: pid: %d ppid: %d\n", getpid(), getppid());
                                        exit (0);
                                        break;
                                    case -1:
                                        printf("Blad funkcji\n");
                                        exit (1);
                                    default:
                                        wait(NULL);
                                        switch (pidp1 = fork())
                                        {
                                            case 0:
                                                printf("P9: pid: %d ppid: %d\n", getpid(), getppid());
                                                exit (0);
                                                break;
                                            case -1:
                                                printf("Blad funkcji\n");
                                                exit (1);
                                            default:
                                                wait(NULL);
                                                exit (0);
                                        }
                                        exit (0);
                                }
                                exit (0);
                                break;
                            case -1:
                                printf("Blad funkcji\n");
                                exit (1);
                            default:
                                wait(NULL);
                                exit (0);
                        }
                        exit (0);
                }
                exit (0);
        }
        exit (0);
}
}

Seeing as how this is homework, I'll try to give you some pointers. 看到这是家庭作业,我将尝试为您提供一些指导。

Wow, that's a lot of code! 哇,好多代码! There might be an opportunity here to take some of the code and put it into a function. 这里可能有机会采用一些代码并将其放入函数中。

The tree you've linked is a bit weird-looking (not symmetrical), and it starts counting at 1, which is annoying. 您链接的树看起来有些古怪(不对称),并且从1开始计数,这很烦人。 Nonetheless, we could describe the tree with two arrays: the number of children for each process, and the first number of its leftmost child. 尽管如此,我们仍可以用两个数组来描述树:每个进程的子代数,以及它最左边的子代的第一个数。

/* The number of children for P0, P1, P2, P3, ... */
const int num_children[] = { -1, 4, 0, 2, 0, 2, 0, 1, 0, 0, 0 };

/* The P-number of the first child for P0, P1, P2, P3, ... */
const int first_child[] = { -1, 2, -1, 6, -1, 8, -1, 10, -1, -1, -1 };

You should be able to recreate the tree from your picture using only this information, and without actually looking at the picture. 您应该仅使用此信息就可以从图片中重新创建树,而无需实际查看图片。 (Really, do it - I might have made a mistake.) (真的,这样做-我可能犯了一个错误。)

Then you could write a function that takes a P number, and does the following: 然后,您可以编写一个带有P号的函数,并执行以下操作:

  • Look up the number of children we have to make in num_children . 查找我们必须在num_children多少个孩子。
  • Look up the P number of the first child in first_child . first_child查找第一个孩子的P号。
  • Fork a few times, each time running our function in the child, with the right P number, and storing the child's pid in the parent somewhere. 分叉几次,每次以正确的P号在子级中运行我们的函数,并将子级的pid存储在父级中的某个位置。
  • Output the line you're describing in your question. 输出您在问题中描述的行。 You've stored all the child pids already, so that should be easy. 您已经存储了所有子pid,因此应该很容易。
  • Wait for its own children to complete. 等待自己的孩子完成。

Launch the whole thing with yourfunction(1) . yourfunction(1)启动整个过程。

The leaf functions do not have children, so they would wait forever. 叶子功能没有子代,因此它们将永远等待。 That means your program does not quit anymore. 这意味着您的程序不再退出。 Ever. 曾经 This is actually good news: it means you can use Linux tools like ps , pstree , and htop to make sure your tree has the right shape. 这实际上是个消息:这意味着您可以使用pspstreehtop类的Linux工具来确保树的形状正确。

(The lines of output will probably be output in a random order - I hope that's okay.) (输出行可能会以随机顺序输出-我希望可以。)

Good luck! 祝好运!

Your program is not really readable. 您的程序不是真正可读的。 I think that you should start using structural programming. 我认为您应该开始使用结构化编程。 ie

void p2() {
   exit(0);
}
void p3() {
    if (fork() == 0) p6();
    ...
    exit(0);
}
void p1() {
    if (fork() == 0) p2();
    if (fork() == 0) p3();
    ...
    exit(0);
}

int main() {
   if (fork() == 0) p1();
   wait();
   ...
}

if you wish to having common parent for all child then go for this code instead of writing fork() no of times : 如果您希望所有孩子都有一个共同的父母,那么请选择以下代码,而不要多次编写fork():

int main()
{
    int i, j, k;
    for (i=0; i<10; i++) {
        if (fork()==0) {
            printf("child %d -> pid : [%d] and ppid : [%d]\n",
                   i+1, getpid(), getppid());
            break;
        } else {
            if (i==0) {
                printf("parent   pid : [%d] \n", getpid());
            }
            wait(0);    
        }
    }
    return 0;
}

if you wish to having parent->child1->child2 ie child2 parent is child1 like this sequence then go for this code : 如果您希望让parent-> child1-> child2即child2 parent是child1,例如此序列,请使用以下代码:

int main()
{
    int i, j, k;
    for (i=0; i<10; i++) {
        if (fork()==0) {
            printf("child %d -> pid : [%d] and ppid : [%d]\n",
                   i+1, getpid(), getppid());
        } else {
            if (i==0) {
                printf("parent   pid : [%d] \n", getpid());
            }
            break;
        }
    }
    return 0;
}

Or according to your requirement you can add some condition inside loop. 或者根据您的要求,您可以在循环内添加一些条件。 Hope this will help 希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM