Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 程序不';从管道中读取读数后不要停止_C_Unix_Pipe - Fatal编程技术网

C 程序不';从管道中读取读数后不要停止

C 程序不';从管道中读取读数后不要停止,c,unix,pipe,C,Unix,Pipe,我在试着理解管道。我有一个小程序,它使用管道将消息从父进程发送到子进程。孩子接收所有3条消息,但在阅读最后一条消息后,它挂起,而不是退出。我做错了什么?谢谢 PS:我注意到,如果我在父母的while循环中睡2秒钟,它就会起作用 #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <errno.h> int mai

我在试着理解管道。我有一个小程序,它使用管道将消息从父进程发送到子进程。孩子接收所有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;
}
#包括
#包括
#包括
#包括
#包括
int main(){
int desc[2];
管道(描述);
int-pid=fork();
如果(pid==0){
而(1){
睡眠(1);
字符缓冲区[16];
如果(读取(描述[0],缓冲区,16)!=16){
printf(“错误或完成”);
出口(0);
};
printf(“子:接收到的消息-“%s”\n”,缓冲区);
}
关闭(描述[1]);
}
如果(pid>0){
int i=0;

while(i必须正确关闭管道末端。读卡器将一直挂起,直到管道的所有写入末端都关闭为止

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
}
if(pid==0){
关闭(desc[1]);//关闭读卡器中的写入端
而(1){
...
读取(描述[0],缓冲器,16);
...
}
}
如果(pid>0){
int i=0;
close(desc[0]);//在writer中关闭读取端;不是必需的,但会使代码更干净

而(i您忘记关闭parent和child中管道的无效端。实际上,您的孩子拥有管道的读写部分,因此他无法检测文件的结尾,因为存在写入程序(自身!),因此它在读取中被阻止。请将代码更改为:

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){
      ...
    }
}
if(pid==0){
close(desc[1]);//孩子不是编写器,所以立即关闭编写部分!
而(1){
...
}
}
如果(pid>0){
close(desc[0]);//父对象不是读卡器,请立即关闭已读部分!
int i=0;
虽然(我)你关于“在writer中关闭write end”的说法是对的,但在那之后,父级将退出,因此文件描述符将被关闭。但是+1表示显式。子级的退出机制似乎也不自然。但我想这是另一个问题。