如何创建unix进程二进制树?

时间:2011-10-02 02:55:06

标签: c unix process fork

有人可以帮助我,不一定完成我的作业。

我需要使用fork()创建一个进程树;在Unix / C中,到目前为止,我能够达到第4级的最好是我的代码:

/* Includes */
#include <unistd.h> /* Symbolic Constants */
#include <stdio.h> /* Input/Output */
#include <stdlib.h> /* General Utilities */

int main()
{
    pid_t childpid;
    pid_t pid;
    int i;
    childpid=fork();
    waitpid();
    i=0;
    for (; i<1; i++) {
        int b=0;
        for (; b<1;b++) {
            childpid=fork();
            fprintf(stderr,"the value of i is %d, i am %ld , my parent is %ld\n",
                            i, (long)getpid(), (long)getppid());

            if (childpid==0) {
                break;
            }

        }
        if (childpid==0) {
            break;
        }
    }

    sleep(1);
}

我对C编程非常陌生,总体来说编程很好,所以我想向公众询问我缺少哪些步骤。树深度需要由用户指定,所以我想我会使用n a for循环,但只能在一个级别上相乘而不是创建新级别。

有人可以帮助我,并指出我正确的方向:) 我需要它像这样分叉

           i
          / \
         /   \
        /     \
       /       \
      /         \
     i           i
    / \         / \
   /   \       /   \
  i     i     i     i
 / \   / \   / \   / \
i   i i   i i   i i   i

1 个答案:

答案 0 :(得分:1)

waitpid()函数需要参数。

初始childpid = fork();是意料之外的。

你可以明智地写道:

for (int i = 0; i < 1; i++)
除非你坚持使用C89而不是C99语法,否则

用于循环。

尝试2

示例输出

Start 46480
i am 46480, my parent is 30775
i am 46482, my parent is 46480
i am 46481, my parent is 46480
i am 46483, my parent is 46481
i am 46484, my parent is 46482
i am 46486, my parent is 46482
i am 46487, my parent is 46483
Exit 46487
i am 46488, my parent is 46484
Exit 46488
i am 46485, my parent is 46481
i am 46491, my parent is 46484
Exit 46491
i am 46489, my parent is 46483
Exit 46489
Exit 46483
i am 46492, my parent is 46486
Exit 46492
Exit 46484
i am 46490, my parent is 46486
Exit 46490
i am 46493, my parent is 46485
Exit 46493
Exit 46486
Exit 46482
i am 46494, my parent is 46485
Exit 46494
Exit 46485
Exit 46481
Exit 46480

示例来源

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

static pid_t fork_ok(void)
{
    pid_t pid;
    if ((pid = fork()) < 0)
    {
        fprintf(stderr, "Fork failure in pid %d\n", (int)getpid());
        exit(1);
    }
    return pid;
}

int main(void)
{
    fprintf(stderr, "Start %d\n", (int)getpid());

    for (int level = 0; level < 3; level++)
    {
        if (fork_ok() == 0 || fork_ok() == 0)
            continue;
        break;
    }
    fprintf(stderr, "i am %d, my parent is %d\n",
            (int)getpid(), (int)getppid());
    while (wait(0) > 0)
        ;
    fprintf(stderr, "Exit %d\n", (int)getpid());
    return(0);
}

这使用了一个循环,一个有点不寻常的循环。在每个级别,父进程分叉两个孩子;孩子们每个'继续'到下一个级别,而父母完成并退出循环。清理代码与之前相同(见下文)。

递归变体

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

static pid_t fork_ok(void)
{
    pid_t pid;
    if ((pid = fork()) < 0)
    {
        fprintf(stderr, "Fork failure in pid %d\n", (int)getpid());
        exit(1);
    }
    return pid;
}

static void new_level(int level)
{
    if (level > 3)
        return;
    if (fork_ok() == 0 || fork_ok() == 0)
        new_level(level+1);
    else
    {
        printf("i am %d, my parent is %d\n",
                (int)getpid(), (int)getppid());
        while (wait(0) > 0)
            ;
        printf("Exit %d\n", (int)getpid());
    }
}

int main(void)
{
    printf("Start %d\n", (int)getpid());
    fflush(0);
    new_level(0);
    return(0);
}

尝试1

我不相信你需要任何循环。这似乎对我有用:

示例输出

Start 44397
i am 44397, my parent is 30775
i am 44400, my parent is 44397
i am 44401, my parent is 44397
Exit 44401
i am 44399, my parent is 44397
i am 44398, my parent is 44397
i am 44404, my parent is 44400
Exit 44404
i am 44403, my parent is 44399
i am 44402, my parent is 44398
i am 44405, my parent is 44398
i am 44407, my parent is 44398
Exit 44407
Exit 44400
i am 44406, my parent is 44399
Exit 44406
i am 44408, my parent is 44402
i am 44410, my parent is 44402
i am 44411, my parent is 44405
Exit 44410
Exit 44411
i am 44409, my parent is 44403
Exit 44409
Exit 44405
i am 44412, my parent is 44408
Exit 44412
Exit 44403
Exit 44399
Exit 44408
Exit 44402
Exit 44398
Exit 44397

源代码

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

int main(void)
{
    fprintf(stderr, "Start %d\n", (int)getpid());
    if (fork() < 0 || fork() < 0 || fork() < 0 || fork() < 0)
    {
        fprintf(stderr, "Fork failure in pid %d\n", (int)getpid());
        exit(1);
    }
    fprintf(stderr, "i am %d, my parent is %d\n",
            (int)getpid(), (int)getppid());
    while (wait(0) > 0)
        ;
    fprintf(stderr, "Exit %d\n", (int)getpid());
    return(0);
}