Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.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_Linux_Pipe - Fatal编程技术网

C 通过管道发送多条消息

C 通过管道发送多条消息,c,linux,pipe,C,Linux,Pipe,我试着用一根管子从父母那里向孩子发送两条信息“你好,世界”和“再见”。孩子收到消息时必须打印消息。 我的问题是如何发送第二条消息。我编译并运行程序,但它只打印第一条消息。有什么建议吗? 这是我的密码: #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 50 void main(){ int fd[2], n

我试着用一根管子从父母那里向孩子发送两条信息“你好,世界”和“再见”。孩子收到消息时必须打印消息。 我的问题是如何发送第二条消息。我编译并运行程序,但它只打印第一条消息。有什么建议吗? 这是我的密码:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 50

void main(){

    int fd[2], n, status;
    char buf[MAX];
    pid_t pid;
    char str1[]="Hello World!\n";
    char str2[]="Goodbye\n";
    pipe(fd);

    if((pid=fork())<0){
        abort();
    }

    else if(pid>0){// parent code goes here
        close (fd[0]); // close read channel of parent

        /*Send "hello world" through the pipe*/

        write(fd[1],str1,(strlen(str1))); // write to the pipe

        wait(&status);

        close(fd[0]);
        write(fd[1],str2,(strlen(str2)));
    }  
    else{ // child code goes here
        close(fd[1]); // close write channel of child

        n=read(fd[0], buf, sizeof(buf)); // reads from the pipe
        write(STDOUT_FILENO, buf, n);

        exit(0);
    }
}
#包括
#包括
#包括
#包括
#定义最大值50
void main(){
int-fd[2],n,状态;
char buf[MAX];
pid_t pid;
char str1[]=“你好,世界!\n”;
char str2[]=“再见\n”;
管道(fd);
如果((pid=fork())0){//父代码在这里
关闭(fd[0]);//关闭父级的读取通道
/*通过管道发送“hello world”*/
写入(fd[1],str1,(strlen(str1));//写入管道
等待(&状态);
关闭(fd[0]);
写入(fd[1],str2,(strlen(str2));
}  
else{//子代码在这里
关闭(fd[1]);//关闭子级的写入通道
n=读取(fd[0],buf,sizeof(buf));//从管道读取
写入(标准文件号,buf,n);
出口(0);
}
}

在父级中,只需写入两条消息,然后关闭管道的写入端:

close(fd[0]); // close read channel of pipe in parent
write (fd[1], str1, strlen(str1)); // write "hello world"
write (fd[1], str2, strlen(str2)); // write "goodbye"
close(fd[1]); // Tell child that we're done writing

wait(&status); // Wait for child to read everything and exit
在子级中,您应该在循环中读取,直到获得EOF,由返回0的
read()
指示:

close(fd[1]); // close write channel of pipe in child
while ((n = read(fd[0], buf, sizeof(buf)) > 0) { // Read until it returns 0 (EOF) or -1 (error)
    write(STDOUT_FILENO, buf, n);
}
if (n < 0) { // -1 = error
    perror("read from pipe");
}
close(fd[1]);//关闭子级中管道的写入通道
而((n=read(fd[0],buf,sizeof(buf))>0){//read,直到返回0(EOF)或-1(错误)
写入(标准文件号,buf,n);
}
如果(n<0){/-1=错误
perror(“从管道读取”);
}

为什么要关闭
fd[0]
在父对象中两次?
等待
等待子对象退出。为什么在子对象完成后尝试向管道中写入内容而不尝试读取它?第二次关闭没有意义,你是对的。我的错误是,我试图等待子对象结束在管道上再次写入,但我不知道这是否正确:/建议在编译时启用所有警告。然后“void main()”将被更正为“int main(void)”,并且头文件“sys/types.h”将被包括在内,因此将定义“pid”。关于行:“wait(&status)'等待子项退出,同时捕获退出状态/值。可能不是您想要做的。为什么代码关闭fd[0]不止一次?一个“管道”不知道EOF、记录或任何形式的格式。读取管道的代码需要考虑到这一点,是的,它是有效的!非常感谢!!!但我不理解while条件。n是整数吗?n的含义是什么?read函数给n的值是什么?我们需要的字节数重读。你在自己的程序中使用了它,你怎么会不知道它是什么?我有一个想法,只是为了确保我没有错!非常感谢。:)