user10440380
user10440380

Reputation: 3

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.

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.

#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;
}

Upvotes: 0

Views: 425

Answers (1)

William Pursell
William Pursell

Reputation: 212248

At the end of the main fork loop, you have lines of code:

    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. Delete that wait loop and the return.

Upvotes: 1

Related Questions