c - 只在一个 child 上 fork 的 Unix fork 树

标签 c unix process fork

显然是家庭作业,但我并不是要任何人为我做,而是我只是想要方向。到目前为止,我已经把它写成一个 fork 进程树(这是一个很难弄清楚的问题)

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

int main()
{   
    pid_t leftchild;
    pid_t rightchild;
    pid_t pid;
    int level=0;
    int max;


    printf("Enter in max level for process tree: ");
    scanf(" %d", &max);




    pid=getpid();
        fprintf(stderr,"I am the parent process with and id of: %ld\n", (long)getpid());
    for(; level<max; level++){

        leftchild=fork();
          if (leftchild == -1)
           {   

              fprintf(stderr, "can't fork, error %d\n", errno);
              exit(EXIT_FAILURE);
           }

        if(leftchild==0){
            fprintf(stderr,"Level is %d, i am %ld , my parent is %ld\n",level, (long)getpid(), (long)getppid());
            continue;
        }



        else{
            rightchild=fork();

            if(rightchild==0){
                fprintf(stderr,"Level is %d, i am %ld , my parent is %ld\n",level, (long)getpid(), (long)getppid());

                continue;
            }
        }
    wait(NULL);
    wait(NULL);
    break;

    }   

}

这个程序创建了这棵树

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

我需要创建另一个具有如下树的叉树:

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

我应该考虑对我的程序进行哪些修改?

我曾尝试在 rightchild if 语句中创建另一个分支,但没有成功。我什至尝试将所有东西分开,但失败得很惨。我只是没有从逻辑上看到解决方案。有什么提示或建议吗?

我试过递归:

     /* Includes */
#include <unistd.h>     /* Symbolic Constants */
#include <stdio.h>      /* Input/Output */
#include <stdlib.h>     /* General Utilities */
#include<errno.h>
pid_t leftchild;
pid_t rightchild;
pid_t pid;
int level=0;
int max;



void recurse(){
if(level<2){
    leftchild= fork();
        if (leftchild == -1)
           {   

              fprintf(stderr, "can't fork, error %d\n", errno);
              exit(EXIT_FAILURE);
           }

        if(leftchild==0){
            fprintf(stderr,"Level is %d, i am %ld , my parent is %ld\n",level, (long)getpid(), (long)getppid());

        }   

    rightchild=fork();
        if (rightchild == -1)
           {   

              fprintf(stderr, "can't fork, error %d\n", errno);
              exit(EXIT_FAILURE);
           }

        if(rightchild==0){
            fprintf(stderr,"Level is %d, i am %ld , my parent is %ld\n",level, (long)getpid(), (long)getppid());

        }
        level++;
        recurse();
        }

    }




int main()
{   



    printf("Enter in max level for process tree: ");
    scanf(" %d", &max);


    pid=getpid();
        fprintf(stderr,"I am the parent process with and id of: %ld\n", (long)getpid());

    recurse();



}

这实际上不会返回任何东西的 pid,有什么特别的原因吗?

最佳答案

你的左 child 不应该继续循环;它应该跳出循环,或者退出。它没有子项,但这仅仅意味着 wait() 调用每次都会返回一个错误。

你的右 child 应该继续循环以继续这个过程直到最后一层。

每一级的父进程应该在启动第二个(右边的)子进程后跳出循环,等待它的子进程结束。


例子

这似乎对我有用;这是对 SO 7624325 答案的简单改编,你之前的相关问题,尽管我从上面的示例代码中提取出来。

示例输出

I am the parent process with and id of: 13277
Level is 0, I am Left  child 13278, my parent is 13277
Exiting: 13278
Level is 0, I am Right child 13279, my parent is 13277
Level is 1, I am Left  child 13280, my parent is 13279
Level is 1, I am Right child 13281, my parent is 13279
Exiting: 13280
Level is 2, I am Right child 13283, my parent is 13281
Level is 2, I am Left  child 13282, my parent is 13281
Exiting: 13282
Level is 3, I am Left  child 13284, my parent is 13283
Level is 3, I am Right child 13285, my parent is 13283
Exiting: 13284
Level is 4, I am Left  child 13286, my parent is 13285
Exiting: 13286
Level is 4, I am Right child 13287, my parent is 13285
Level is 5, I am Left  child 13288, my parent is 13287
Exiting: 13288
Level is 5, I am Right child 13289, my parent is 13287
Level is 6, I am Right child 13291, my parent is 13289
Level is 6, I am Left  child 13290, my parent is 13289
Exiting: 13290
Exiting: 13291
Exiting: 13289
Exiting: 13287
Exiting: 13285
Exiting: 13283
Exiting: 13281
Exiting: 13279
Exiting: 13277

示例代码

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

static void run_process(const char *child, int level)
{
    fprintf(stderr, "Level is %d, I am %-5s child %ld, my parent is %ld\n",
            level, child, (long)getpid(), (long)getppid());
}

int main(void)
{   
    int max = 7;

    fprintf(stderr, "I am the parent process with and id of: %ld\n", (long)getpid());

    for (int level = 0; level < max; level++)
    {
        pid_t leftchild;
        pid_t rightchild;
        if ((leftchild = fork()) < 0)
        {   
            fprintf(stderr, "can't fork, error %d\n", errno);
            exit(EXIT_FAILURE);
        }
        else if (leftchild == 0)
        {
            run_process("Left", level);
            break;
        }
        else if ((rightchild = fork()) < 0)
        {   
            fprintf(stderr, "can't fork, error %d\n", errno);
            exit(EXIT_FAILURE);
        }
        else if (rightchild == 0)
        {
            run_process("Right", level);
            continue;
        }
        else
            break;
    }

    wait(NULL);
    wait(NULL);
    fprintf(stderr, "Exiting: %ld\n", (long)getpid());

    return(0);
}

关于c - 只在一个 child 上 fork 的 Unix fork 树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7682988/

相关文章:

c - 从产生奇怪输出的字符串中删除字符

c - 为什么 x86 的 INC 指令不是原子的?

json - JQ库: Overriding existing keys of an JSON object with another object keys

linux - 想要从具有页眉页脚的文件中获取分隔数据,以便进行数据处理以进行性能分析

c# - 图像文件副本,正在被另一个进程使用

c++ - 为什么我的 strcmp 实现没有返回正确的值?

c++ - 一个类似 DSL 的小型 lisp,可以编译成 C/C++ 代码——Antlr 是一个不错的选择吗?

unix - 用于屏蔽最多 12 位信用卡的 Sed 命令

c - popen 是标准 C 函数吗?

php - 检查 php 文件命令是否已经在 cron 上运行