Two-way communication between parent and child

advertisements

I'm trying to create two-way communication between parent and child processes using 2 pipes in C.the prog1 running in child1 I want to read 3+4+5 from prog1 after that send something to prog1 with write but I could not. Where is the wrong?

/* 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);
 }


There are few problems with your program:

  1. You are not checking returned values of read, write and execle in prog2.c
  2. You are sending "7+5+11=?\n" string which is 10 characters long but only expecting 4 characters ( 3+4+5 is not even four characters ).
  3. Also "off" you are sending is 3 characters long but without including null termination.
  4. When you read from an fd you will in both cases not get null terminated string and then you are trying to printf it. It's a quick way to undefined behaviour. Put an '\0' after the end of buffer you read from any file descriptor!
  5. Especially what read returns is very important as it tells you how many characters were read. You should never ignore returned value of read (in some cases it's the same with write function).

Next time also provide some output of your program as it will be easier to give some help.