父母和孩子之间的双向管道通信

时间:2014-03-04 17:07:59

标签: c linux exec fork pipe

我正在尝试在C.中使用2个管道创建父进程和子进程之间的双向通信。在child1中运行的prog1 我想从prog1读取3 + 4 + 5之后用write写东西到prog1但我不能。 哪里错了?

/* prog1.c */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
void
main(void){
    int FD;
unsigned int buf;
char buf[15];

printf("7+5+11=?\n");
FD=read(0,buf,10);
if(FD<0){
    perror("FAIL\n");
exit(EXIT_FAILURE);
}
     printf("TAKED:%s\n",buf);
}

prog2.c

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
void ERR_SYS(const char *msg);
int
main(void){
    char buf[15];
    int pipe1[2];
    int pipe2[2];
    pid_t childpid;

    memset(buf,'\0',14);

    if(pipe(pipe1) < 0 || pipe(pipe2) < 0)
     ERR_SYS("fail_pipe");

    if((childpid = fork()) < 0)
     ERR_SYS("fail_fork");

    if(childpid==0)
    {
      dup2(pipe2[1],1);
          dup2(pipe1[0],0);
      close(pipe1[1]);
          close(pipe2[0]);
      close(pipe2[1]);
      close(pipe1[0]);
          //close(1);
          //close(0);
      execle("./prog1",NULL,NULL,NULL);
    }else{

     close(pipe1[0]);
     close(pipe2[1]);
     read(pipe2[0],buf,4); /*I hope to read 3+4+5*/
     printf("BuF::%s\n",buf);
     write(pipe1[1],"off",3);/*send {off}*/
     wait(NULL);
    }
 return 0;
 }

 void 
 ERR_SYS(const char *msg)
 {
     perror(msg);
     exit(EXIT_FAILURE);
 }

2 个答案:

答案 0 :(得分:1)

您的计划几乎没有问题:

  1. 您没有在prog2.c中检查read,write和execle的返回值
  2. 您发送的是“7 + 5 + 11 =?\ n”字符串,长度为10个字符,但只需要4个字符(3 + 4 + 5甚至不是4个字符)。
  3. 您发送的“off”也是3个字符,但不包括空终止。
  4. 当您从fd读取时,您将在两种情况下都不会获得以空字符结尾的字符串,然后您尝试printf它。这是一种未定义行为的快捷方式。在从任何文件描述符中读取的缓冲区结束后放置一个'\ 0'!
  5. 特别是read返回的内容非常重要,因为它会告诉您读取了多少个字符。你永远不应该忽略read的返回值(在某些情况下它与write函数相同)。
  6. 下次还会提供一些程序输出,因为它会更容易提供帮助。

答案 1 :(得分:0)

我没有按照你设置管道的所有逻辑,所以我修改并希望澄清你的原件。我应该注意,无论出于何种原因,我从外部程序(prog1)的角度命名fd_in和fd_out(例如fd_out是prog1写入的地方,fd_in是prog1读取的地方)。

这是我的prog3.c的内容:

...
#define READ_END 0
#define WRITE_END 1
void ERR_SYS(const char *msg);
int main(void) {

    char buff[15];
    char *msg = "hello";
    int fd_out[2];
    int fd_in[2];
    int nbytes;
    pid_t childpid;

    if(pipe(fd_out) < 0 || pipe(fd_in) < 0) {
            ERR_SYS("fail_pipe");
    }

    if((childpid = fork()) < 0) { 
            ERR_SYS("fail_fork");
    }

    if(childpid==0) { //child 
            //connect the write end of fd_out to stdout
            dup2(fd_out[WRITE_END], STDOUT_FILENO);
            close(fd_out[WRITE_END]);
            //connect the read end of fd_in to stdin
            dup2(fd_in[READ_END], STDIN_FILENO);
            close(fd_in[READ_END]);
            //the exec'd prog1 will inherit the streams
            execlp("./prog1", "prog1", NULL); //TODO: check return
    } else { //parent
            nbytes = write(fd_in[WRITE_END], msg, strlen(msg));
            //TODO: handle any errors from write
            nbytes = read(fd_out[READ_END],buff,sizeof(buff)-1);
            //TODO: handle any errors from read
            buff[nbytes] = '\0';
            printf("contents of buff::%s",buff);
    }
    return 0;
}
void ERR_SYS(const char *msg) {
    perror(msg);
    exit(EXIT_FAILURE);
}

这是我的prog1.c的内容

int main(void){
    char buff[15];
    int nbytes;
    nbytes = read(STDIN_FILENO, buff, sizeof(buff)-1);
    buff[nbytes] = '\0';
    printf("%s world\n", buff);
    return 0;

}

相关问题