从管道读取数据后程序不会停止

问题描述 投票:0回答:2

我正在尝试了解管道。我有一个小程序,它使用管道将消息从父进程发送到其子进程。孩子收到了所有 3 条消息,但在阅读最后一条消息后并没有退出,而是挂起。我究竟做错了什么?谢谢。

PS:我注意到如果我在父级的 while 循环中睡 2 秒,它就会起作用。

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

int main(){

    int desc[2];
    pipe(desc);

    int pid = fork();

    if(pid == 0){
        while(1){
            sleep(1);
            char buffer[16];
            if(read(desc[0], buffer, 16) != 16){
                printf("Error or finished");
                exit(0);
            };
            printf("Child: message recieved - '%s'\n", buffer);
        }
        close(desc[1]);
    }
    if(pid > 0){
        int i=0;
        while(i <= 2){
            char buffer[100];
            i++; char x[10];
            strcpy(buffer, "Hello, child!");
            sprintf(x, " %d", i);
            strcat(buffer, x);
            if(write(desc[1], buffer, 16) != 16){
                printf("Error");
                exit(0);
            };
        }
        close(desc[0]);
    }
    return 0;
}
c unix pipe
2个回答
5
投票

您忘记关闭父进程和子进程中管道未使用的一端。实际上,您的孩子拥有管道的读取和写入部分,因此它无法检测到文件结尾,因为存在写入器(本身!),因此它在读取中被阻止。将您的代码更改为:

if(pid == 0){
    close(desc[1]); // Child is not a writer, so close the write part immediately!
    while(1){
      ...
    }
}
if(pid > 0){
    close(desc[0]); // Parent is not a reader, so close the read part immediately!
    int i=0;
    while(i <= 2){
      ...
    }
}

请记住,在管道上,文件结尾是“管道中没有更多内容可读取”“不再有写入器”。


4
投票

必须正确封闭管端。读取器将挂起,直到管道的所有写入端都关闭。

if(pid == 0){
    close(desc[1]); // close write end in reader
    while(1){
        ...
        read(desc[0], buffer, 16);
        ...
    }
}
if(pid > 0){
    int i=0;
    close(desc[0]); // close read end in writer; not required, but makes code cleaner
    while(i <= 2){
      ...
      write(desc[1], buffer, 16);
      ...
    }
    close(desc[1]); // close write end in writer
}
© www.soinside.com 2019 - 2024. All rights reserved.