我试图在 C 中多次调用 fork

I'm trying to call fork multiple times in C

这是我想要的结构,进程传递一个 int 并添加到它每次打印出结果。

管理多个管道。

|Main|<--------\                    0   1   2   3   
 |              \     Pipes: read  [a] [c] [e] [g]   (each column
 |               |          write  [b] [d] [f] [h]    is a pipe)
 \/              |
|Process 1|      | Process 1 reads from 'a' in pipe 0, and writes to 'd' in pipe 1
 | index: 0      | Process 2 reads from 'c' in pipe 1, and writes to 'f' in pipe 2
 |               | Process 3 reads from 'e' in pipe 2, and writes to 'h' in pipe 3
 \/              |
|Process 2|      |
 | index: 1      |
 |               |
 \/              |
|Process 3|_____ /
   index: 2

我想让它打印这样的东西:

Main process sent 5
(0) Got 5
(0) Sent 6
(1) Got 6
(1) Sent 7
(2) Got 7
(2) Sent 8
The final result is 8

但无论出于何种原因,当我 运行 代码时,它就挂在那里。构建它不会显示任何错误。我检查了它是否有拼写错误,所以我不认为是这样。我猜这是一个逻辑错误,但我只是没有看到它。这是基于 C 手册的基本内容,所以我不知道为什么不起作用。

#define PROCESS_NUM 3
// 3 processes with 4 pipes to connect them all

int
main(int argc, char *argv[])
{
    int pids[PROCESS_NUM];              // array for storing process id'
    int pipes[PROCESS_NUM + 1][2];
    int i;

    for (i = 0; i < PROCESS_NUM + 1; i++) {
        if (pipe(pipes[i]) == -1) {
            printf("Errorwith creating pipe\n");
            return 1;
        }
    }

    for (i = 0; i < PROCESS_NUM; i++) {
        pids[i] = fork();
        if (pids[i] == -1) {
            printf("Error with creating process \n");
            return 2;
        }
        if (pids[i] == 0) {
            // child process
            // i for first process 0 hence goes to pipe 0
            int j;

            for (j = 0; j < PROCESS_NUM + 1; j++) {
                if (i != j) {
                    close(pipes[j][0]);
                }
                if (i + 1 != j) {
                    close(pipes[j][1]);
                }
            }
            int x;

            if (read(pipes[i][0], &x, sizeof(int)) == -1) {
                printf("Error ar reading\n");
                return 3;
            }
            printf("(%d) Got %d\n", i, x);
            x++;

            if (write(pipes[i + 1][1], &x, sizeof(int)) == -1) {
                printf("Error at writing\n");
                return 4;
            }
            printf("(%d) Got %d\n", i, x);
            close(pipes[i][0]);
            close(pipes[i + 1][1]);
            return 0;
            // Once this process compleatss the program stopes so we don't make an infinete number of processes.
        }
        for (i = 0; i < PROCESS_NUM + 1; i++) {
            wait(NULL);
        }
        return 0;
    }
// Main process
    int j;

    for (j = 0; j < PROCESS_NUM + 1; j++) {
        if (j != PROCESS_NUM) {
            close(pipes[j][0]);
        }
        if (j != 0) {
            close(pipes[j][1]);
        }
    }
    int y = 5;

    printf("Main process sent: %d\n", y);
    if (write(pipes[0][1], &y, sizeof(int)) == -1) {
        printf("Error at writing\n");
        return 4;
    }
    if (read(pipes[PROCESS_NUM][0], &y, sizeof(int)) == -1) {
        printf("Error ar reading\n");
        return 3;
    }
    printf("The final result is: %d\n", y);
    close(pipes[0][1]);
    close(pipes[PROCESS_NUM][0]);

    for (i = 0; i < PROCESS_NUM; i++) {
        wait(NULL);
    }

    return 0;
}

在主 fork 循环的末尾,您有以下代码行:

    for (i = 0; i < PROCESS_NUM + 1; i++) {
    wait(NULL);
    }
    return 0;
}
// Main process

主进程执行 return 0 并且永远不会到达注释 Main process 之后的代码行。删除等待循环和 return.