PIPES

Arvind Krishnan akrishn at csserv2.ic.sunysb.edu
Tue Nov 20 13:22:47 AEST 1990


PLEASE HELP!!

I have this program which spawns 'mailx' as a child process. It then uses 
two pipes for full duplex communications with its child (mailx). 
     
     1. stdout of the child is the stdin of the parent and
     2. stdout of the parent is the stdin of the child

For some unknown reason, '1' works but '2' doesn't. The parent process can 
read the output of its child, but the child doesn't seem to be reading the
output of the parent process.

Can someone please figure out whats happening and let me know!

The code fragment at fault is as follows.

Thanks a lot in advance. Any help will be greatly appreciated.

Arvind

-------------------------------------------------------------------------------
char command[] = "1";

int start_mail() 
{ 
  int to_par[2], to_child[2];
  char buf[8];
  int flag = 0;

  if (pipe(to_par))
     return -1; 
  if (pipe(to_child))
     return -1; 

  if (fork() == 0)
   {
     close(0);
     dup2(to_child[0], 0);
     close(1);
     dup2(to_par[1], 1);
     close(to_par[1]);
     close(to_child[0]);
     close(to_par[0]);
     close(to_child[1]);
     if (execl("/usr/bin/mailx", "mailx", (char *) 0))
        fprintf(stderr,"Exec Error\n");
   }
  close(1);
  dup2(to_child[1], 1);
  close(0);
  dup2(to_par[0], 0);
  close(to_child[1]);
  close(to_par[0]);
  close(to_child[0]);
  close(to_par[1]);
  fcntl(0, F_SETFL, O_NDELAY);
  while (1)
   {
     if (flag == 0) 
        while ((flag = read(0,buf,1)) == 0);
     write(2,buf,1);
     flag = read(0,buf,1);
     if (flag == 0)
        write(1, command, 1);
   }
} 



More information about the Comp.unix.questions mailing list