简体   繁体   English

我试图在 C 中多次调用 fork

[英]I'm trying to call fork multiple times in C

This is the structure I'm going for, the processes pass an int around and add to it printing out the result each time.这就是我想要的结构,过程传递一个 int 并添加到它每次打印出结果。

Managing multiple pipes.管理多个管道。

|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

I want it to print something like this:我希望它打印这样的东西:

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

But for whatever reason when I run the code it just hangs there.但是无论出于何种原因,当我运行代码时,它都会挂在那里。 Building it doesn't show any errors.构建它不会显示任何错误。 And I checked it for typos so I don't think its that.我检查了它是否有错别字,所以我不认为它是那样的。 I'm guessing it's a logic error but I just don't see it.我猜这是一个逻辑错误,但我只是没有看到。 This is basic stuff based on the C manuals so I don't know why is doesn't work.这是基于 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;
}

At the end of the main fork loop, you have lines of code:在主fork循环结束时,您有以下代码行:

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

The main process executes the return 0 and never gets to the line of code after the comment Main process .主进程执行return 0并且永远不会到达注释Main process之后的代码行。 Delete that wait loop and the return.删除那个等待循环并返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM