如何创建unix进程二叉树?

发布于 2024-12-07 14:35:37 字数 1174 浏览 1 评论 0原文

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

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

/* 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 编程非常陌生,总体上编程很好,所以我想问一下公众我缺少哪些步骤。树深度需要由用户指定,所以我想我会使用 na for 循环,但这只会在一个级别上相乘,而不是创建新级别。

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

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

Can some one help me out here, not necessarily complete my homework.

I need to create a process tree using fork(); in Unix/C so far the best I can get to to the 4th level here is my code:

/* 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);
}

I am very new to C programming, well programming in general, so I wanted to ask the general public what steps I am missing. The tree depth needs to be user specified so I figured I would use n a for loop but that only multiplies across a level rather than creating new levels.

Can someone help me out and point me in the right direction :)
I need it to fork like this

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别理我 2024-12-14 14:35:37

waitpid() 函数需要参数。

最初的 childpid = fork(); 是意外的。

您可以明智地编写:

for (int i = 0; i < 1; i++)

for 循环,除非您坚持使用 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);
}

The waitpid() function requires arguments.

The initial childpid = fork(); is unexpected.

You can sensibly write:

for (int i = 0; i < 1; i++)

for the loops unless you insist on using C89 instead of C99 syntax.

Attempt 2

Example output

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

Example source

#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);
}

This uses a loop, a somewhat unusual loop. At each level, the parent process forks two children; the children each 'continue' to the next level, while the parent is done and exits the loop. The clean up code is the same as before (see below).

Recursive variant

#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);
}

Attempt 1

I'm not convinced you need any loops. This seemed to do the trick for me:

Example output

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

Source Code

#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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文