【问题标题】:How should I made this process tree with fork in C?我应该如何在 C 中使用 fork 制作这个进程树?
【发布时间】:2022-01-13 23:29:47
【问题描述】:

每个人。

我正在学习操作系统课程,我的任务是使用 fork 用 C 语言编写以下进程树。程序必须接收 2 个参数,即高度和宽度(分别为 3 和 2)并创建此图像的树:

在图像上,P.I = Parent Process ,Hijo N= N Child Process,Nieto N = N Grandson 和 Bisnieto N = N Great Grandson。

代码必须使用以下模板:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
 int i, height, width;
 if (argc!= 3) exit(0);
 height = atoi(argv[1]); /* height */
 width = atoi(argv[2]); /* width */
 /* Your code goes here */
 printf("I am the process %d and my father is %d\n", getpid(), 
getppid());
 sleep(1);
 return 0;
}

我做了几个代码,但我找不到解决方案,我最接近的是下一个代码:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int
main (int argc, char *argv[]) 
{  
int i,x, height, width;  
pid_t pid,pid2; 
if (argc != 3)
    exit (0);  
height = atoi (argv[1]);    /* height */
width = atoi (argv[2]); /* width */  
    /* Your code goes here */ 
for (i = 1; i < height; i++)    
{
    pid=fork();
    if(pid<0){pid2 = fork();}
} 
printf ("I am the process %d and my father is %d\n", getpid (), getppid ());  
sleep (1);
return 0;

}

但是唯一缺少的就是创建第一个进程(P.I)的两个子进程,hijo 2=child 2 of the diagram is not created.

我还尝试创建一个代码,一个接一个地创建一个进程链,另一个代码从一个父进程创建两个进程,但我不知道如何加入代码来创建这棵树。有人可以帮我吗?

这是我制作的链的代码:

#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
 int i, height, width;
 pid_t pid,pid2;
 if (argc!= 3) exit(0);
 height = atoi(argv[1]); /* height */
 width = atoi(argv[2]);
    for (int i=0; i<height; i++)     
    {
        pid = fork();
        if (pid == -1) { /* handle error*/ }
        else if (pid == 0) {
            printf("Soc el process %d y meu pare es %d\n", getpid(),getppid());
        }
        else { // parent process
            wait(NULL);
            exit(0);
        }
    }
  printf("Soc el process %d y meu pare es %d\n", getpid(),getppid());
}

我创建的创建一个父母的两个孩子的代码是这个:

#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
 int i, height, width;
 pid_t pid,pid2;
 if (argc!= 3) exit(0);
 height = atoi(argv[1]); /* height */
 width = atoi(argv[2]); /* width */
 /* tu codigo aqui */
 for(int i=0;i<width;i++) // loop will run n times (n=5)
    {
        if(fork() == 0)
        {
            printf("[son] pid %d from [parent] pid %d\n",getpid(),getppid());
            exit(0);
        
        }
    }
  printf("Soc el process %d y meu pare es %d\n", getpid(),getppid());
  for(int i=0;i<height;i++) // loop will run n times (n=5)
  wait(NULL);
}

我想知道加入两个代码或修改第一个代码是否更好,以及如何做到:C

【问题讨论】:

  • 我投票结束这个问题,因为在家庭作业上寻求帮助需要帖子就特定问题提出特定问题。

标签: c for-loop process fork binary-tree


【解决方案1】:

您在问题中显示的图表描述了高度为 3 且宽度为 2 时的行为,并且行为是:
每个进程都应该有2(等于width)子进程,其中一个子进程将退出,一个将继续分叉其2(等于width)子进程......等等.当到达height 时,fork 1 子进程(并让它退出)。

当宽度为3(或更准确地说,宽度为&gt; 2)时会发生什么?

我假设,在这种情况下,当宽度为&gt; 2 时,一个进程应该派生等于width 的子进程数,并且所有子进程都应该退出,除了将进一步派生子进程的子进程之一等于给定数量的width 并且一直持续到达到树的高度。最后一个父进程将派生width - 1 数量的子进程,并且这些子进程都不会进一步派生任何其他子进程。有了这个假设,width 应该是&gt;= 1

实施:

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

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

    int height, width;

    if (argc != 3) {
        printf ("Invalid number of arguments, exiting..\n");
        exit (0);
    }

    height = atoi (argv[1]);
    width = atoi (argv[2]);

    if ((height <= 0) || (width <= 0)) {
        printf ("Invalid input.\n"); // error handling can be better
        exit (0);
    }

    printf ("Parent process, my pid = %d, height = %d, width = %d\n", getpid(), height, width);

    for (int i = 0; i < height; ++i) {
        printf ("\nMy pid : %d, current height of tree : %d, forking..\n", getpid(), i);
        pid_t pid;

        for (int j = 0; j < width; ++j) {
            if ((i + 1 == height) && (j == 0)) {
                continue;
            }

            pid = fork();

            if (pid == -1) { 
                printf ("Fork failed\n");
            } else if (pid == 0) {
                if (j) {
                    printf ("My pid = %d, [my parent : %d], I am EXITING..\n", getpid(), getppid());
                    exit(0);
                } else {
                    printf ("My pid = %d, [my parent : %d], I will fork other child processes..\n", getpid(), getppid());
                    break;
                }
            }
        }

        if (pid) {
            break;
        } else {
            // added sleep for sequenced output, otherwise it's not needed
            sleep (1);
        }
    }

    while (wait(NULL) > 0);
    printf ("pid %d : I am EXITING\n", getpid());

    // added sleep for sequenced output, otherwise it's not needed
    sleep (1);
    return 0;
}

输出:

# ./a.out 3 2
Parent process, my pid = 3827, height = 3, width = 2

My pid : 3827, current height of tree : 0, forking..
My pid = 3828, [my parent : 3827], I will fork other child processes..
My pid = 3829, [my parent : 3827], I am EXITING..

My pid : 3828, current height of tree : 1, forking..
My pid = 3830, [my parent : 3828], I will fork other child processes..
My pid = 3831, [my parent : 3828], I am EXITING..

My pid : 3830, current height of tree : 2, forking..
My pid = 3832, [my parent : 3830], I am EXITING..
pid 3830 : I am EXITING
pid 3828 : I am EXITING
pid 3827 : I am EXITING

图解表示:

     --------
     | 3827 |
     --------
       |    \
       |     \
       |      \
       |       \
       |        --------
       |        | 3829 |
     --------   --------
     | 3828 | 
     -------- 
       |    \
       |     \
       |      \
       |       \
       |        --------
       |        | 3831 |
     --------   --------
     | 3830 | 
     -------- 
            \
             \
              \
               \
                --------
                | 3832 |
                --------

height = 3width = 4 的输出:

# ./a.out 3 4
Parent process, my pid = 4052, height = 3, width = 4

My pid : 4052, current height of tree : 0, forking..
My pid = 4053, [my parent : 4052], I will fork other child processes..
My pid = 4054, [my parent : 4052], I am EXITING..
My pid = 4055, [my parent : 4052], I am EXITING..
My pid = 4056, [my parent : 4052], I am EXITING..

My pid : 4053, current height of tree : 1, forking..
My pid = 4058, [my parent : 4053], I will fork other child processes..
My pid = 4059, [my parent : 4053], I am EXITING..
My pid = 4060, [my parent : 4053], I am EXITING..
My pid = 4061, [my parent : 4053], I am EXITING..

My pid : 4058, current height of tree : 2, forking..
My pid = 4062, [my parent : 4058], I am EXITING..
My pid = 4063, [my parent : 4058], I am EXITING..
My pid = 4064, [my parent : 4058], I am EXITING..
pid 4058 : I am EXITING
pid 4053 : I am EXITING
pid 4052 : I am EXITING

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 2014-07-27
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多