使用 fork 创建进程树

标签 c linux ubuntu fork

我正在尝试创建图中所示的流程树。基本上,如果级别是偶数,我想创建一个子进程并终止父进程。如果级别很奇怪,我想创建两个子进程,然后终止父进程。我现在已经编写了一个程序,但我认为很难想象我的程序实际创建的进程树是什么。我已经对代码写了一些注释来解释我的想法。我还想输出我的代码无法正确执行的树底部子节点的 PID。

enter image description here

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

int main(int argc, char *argv[]){ 
    pid_t pid, ppid; 
    int n, i;
    int childstate;
    int count = 0; 

    if(argc != 2){ 
        printf("Wrong number of arguments"); 
        exit(-1); 
    } 
    n = atoi(argv[1]); 

    fork(); //start process 0
    for(i = 1; i < n + 1; i++){ 
        if(i % 2 != 0){ 
            fork(); //if odd level start 1 child process
             if(getpid() == 0){
                kill (getppid(), 9); //terminate parent process
            }
        } else { 
            if(fork() > 0){  //start new process
                fork(); //if new process is not a child start another process
                if(getpid() == 0){
                    kill (getppid(), 9); //terminate parent process
                }
            } 
        } 
        if(i == n){ //print pid of leaves (not working correctly)
            printf("Process: %d \n", getpid()); 
        } 
    }
    return 0; 
}

最佳答案

I also want to output the PID of the bottom children of the tree which my code doesn't do correctly.

让您的进程以 Dot 语言输出树,并使用 Graphviz 输出树。

例如,如果您将以下内容保存为 tree.c:

#define  _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>

int process(const unsigned int level, const unsigned int maxlevel, FILE *dot)
{
    int           status = EXIT_SUCCESS, childstatus;
    unsigned int  children, i;
    pid_t         p, child[2];

    if (dot) {
        /* Output a node for this child, */
        fprintf(dot, "    \"%ld\" [ label=\"Process %ld\" ];\n", (long)getpid(), (long)getpid());

        /* and if not at the top level (0), an edge from our parent. */
        if (level)
            fprintf(dot, "    \"%ld\" -> \"%ld\";\n", (long)getppid(), (long)getpid());

        fflush(dot);
    }

    /* No more forking? */
    if (level >= maxlevel) {
        if (level)
            exit(status);
        else
            return status;
    }

    /* Odd levels create two child processes, even one. */
    if (level & 1)
        children = 2;
    else
        children = 1;

    /* Fork the child processes, */
    for (i = 0; i < children; i++) {
        child[i] = fork();
        if (child[i] == -1) {
            fprintf(stderr, "Cannot fork: %s.\n", strerror(errno));
            exit(EXIT_FAILURE);
        } else
        if (!child[i]) {
            /* have each child run process() and nothing else, */
            exit(process(level + 1, maxlevel, dot));
        }
        /* This line is run in parent only. */
    }

    /* and wait for them. */
    for (i = 0; i < children; i++) {
        if (child[i] != -1) {
            do {
                p = waitpid(child[i], &childstatus, 0);
            } while (p == -1 && errno == EINTR);
            if (p != child[i])
                status = EXIT_FAILURE;
        } else
            status = EXIT_FAILURE;
    }

    if (level)
        exit(status);
    else
        return status;
}

int dot_process_tree(const int levels, FILE *out)
{
    int  retval = EXIT_SUCCESS;

    if (out) {
        fprintf(out, "digraph {\n");
        fflush(out);
    }

    if (levels > 0)
        retval = process(0, levels - 1, out);

    if (out) {
        fprintf(out, "}\n");
        fflush(out);
    }

    return retval;
}

int main(void)
{
    return dot_process_tree(5, stdout);
}

并编译运行

reset ; gcc -Wall -Wextra -O2 tree.c -o tree && ./tree | dot -Tx11

您将获得一个漂亮的图形化流程树。 (使用 dot -Tsvg > out.svgdot -Tpng > out.png 将其保存为 SVG 或 PNG 图像。)在我的系统上:

example process tree

请注意,进程 ID 没有理由按树顺序排列。虽然例如Linux 以相当有序的方式传递它们,它们可以以任何顺序,甚至完全随机。所以不要对 PID 做任何假设。

Dot 语言本身很简单。上面程序的输出是这样的

digraph {
    "12375" [ label="Process 12375" ];
    "12377" [ label="Process 12377" ];
    "12375" -> "12377";
    "12378" [ label="Process 12378" ];
    "12377" -> "12378";
    "12379" [ label="Process 12379" ];
    "12377" -> "12379";
    "12380" [ label="Process 12380" ];
    "12378" -> "12380";
    "12381" [ label="Process 12381" ];
    "12379" -> "12381";
    "12382" [ label="Process 12382" ];
    "12380" -> "12382";
    "12384" [ label="Process 12384" ];
    "12381" -> "12384";
    "12383" [ label="Process 12383" ];
    "12380" -> "12383";
    "12385" [ label="Process 12385" ];
    "12381" -> "12385";
}

这应该是显而易见的;节点由进程 ID 命名,[ label="Title"] 设置节点中的文本。它与上图不同,因此进程 ID 不同。

在 Dot 中,如果用作名称,数字确实需要用引号引起来,但如果名称以字母开头,则不需要用引号引起来。参见 Graphviz documentation了解更多详情。 (Node, Edge and Graph Attributes 页面是您通常需要的页面。)

如果要在每个节点显示层级,使用

        fprintf(dot, "    \"%ld\" [ label=\"Process %ld, level %u\" ];\n", (long)getpid(), (long)getpid(), level + 1);

process() 中。 (它使用 0 级转发,所有非零级别都是子进程,0 级是原始进程。这就是为什么 0 级返回,所有其他级别返回 exit()。)

关于使用 fork 创建进程树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53024715/

相关文章:

c++ - #include <CEGUI/RendererModules/Ogre/CEGUIOgreRenderer.h> 没有正确包含 ogre 头文件

ubuntu - 在 native 模式下运行 erlang 代码

c - 编辑距离矩阵

c++ - 我的程序在 openSUSE 中出现段错误

linux - 找不到为 makefile 指定的目标

php - 有什么办法可以将屏幕内容打印到网页上吗?

docker - Google Cloud Run 最多只能同时处理 100 个请求

c - 单向链表C反向数据读取

c - 如何在 C 中的函数调用之间保留 union 成员的值?

c - C 程序输出有问题