简体   繁体   中英

Sleep() causing broken pipe?

So on college i am now learning concurrent programming. Im doing exercises on pipes and have the following:

Write a program that creates 10 child processes to play the game “Win the pipe!”. There must be only one pipe, shared by all processes . The game's rules are as follow: The parent process fills, each 2 seconds, a structure with a message “Win” and the round's number (1 to 10) and writes this data in the pipe; Each of child processes is trying to read data from the pipe. The child that succeeds should print the winning message and the round's number, ending its execution with an exit value equal to the winning round's number; The remaining child processes continue to try to read data from the pipe; After all child processes terminate, the parent process should print the PID and the winning round of each child.

I have the following code:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

struct Something {
    char message[4];
    char round;
};

int main() {
    int fd[2];
    pid_t pid;
    struct Something something;
    int status;

    if (pipe(fd) == -1) {
        perror("error creating pipe");
        return 1;
    }

    for (int i = 0; i < 10; ++i) {
        pid = fork();
        if (pid == 0) { break; }
    }

    if (pid > 0) { //parent
        close(fd[0]);
        while (something.round <= 10) {
            //sleep(2);  PROBLEM HERE
            strcpy(something.message, "Win");
            something.round++;
            write(fd[1], &something, sizeof(something));
        }
        close(fd[1]);
        for (int i = 0; i < 10; ++i) {
            pid = wait(&status);
            printf("PID: %d  won round %d\n", pid, WEXITSTATUS(status));
        }
    } else { //child
        close(fd[1]);
        read(fd[0], &something, sizeof(something));
        close(fd[0]);
        printf("%s %d", something.message, something.round);
        _exit(something.round);
    }

    return 0;
}

So when i didnt have sleep() commented, and ran the program, what would happen is it froze and didnt print anything, and child processes would be finishing slowly one after another, then i would get broken pipe message.

But when i comment the sleep(), the program runs just fine, tho the parent doesnt wait 2 seconds, and just fills up the pipe i suppose. I cant understand this problem and been searching for answers for a while, unsuccessfully.

If i could get enlightenment on this i would be thankful.

The problem is in your loop.

  while (something.round <= 10) {
            //sleep(2);  PROBLEM HERE
            strcpy(something.message, "Win");
            something.round++;

will iterate 11 times.

Try something.round < 10

Also, either flush stdout in the child, or call exit instead of _exit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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