Problem with pipe/fork

Gaumond Pierre gaumondp at JSP.UMontreal.CA
Wed Apr 17 01:05:57 AEST 1991


The following program creates a pipe and generates two processes with fork. The
parent reads data from standard input and writes it to the pipe. The child
reads the pipe and writes data to the standard output.

Quite simple. However, it doesn't work.

The program reads all the data and everything is transfered (I have tried a
small file as a redirection of input). The processes block after the last
character is transfered to the standard output. The processes seem to wait for
something... for what? EOF?

I understood that the "fclose" on the write end of the pipe would generate an
EOF status at the read end. Is it correct. Perhaps the problem is somewhere
else...

-------------------------------------------------------------------------------
<necessary include files>

main()
{
  FILE *fp;
  int fdi[2];
  char c;
  
  pipe(fdi);
  if (fork()!=0)
  { /* parent */
    fp=fdopen(fdi[1],"w");
    for (c=getchar(); !feof(stdin); c=getchar()) fputc(c,fp);
    fclose(fp);
    wait(0);
  }
  else
  { /* child */
    fp=fdopen(fdi[0],"r");
    for (c=fgetc(fp); !feof(fp); c=fgetc(fp)) putchar(c);
    fclose(fp);
  }
}

-------------------------------------------------------------------------------

Pierre Gaumond.
-- 
Pierre Gaumond.                                 | gaumondp at JSP.UMontreal.CA
Services Informatiques, Universite de Montreal. | gaumondp at centrcn.UMontreal.CA
C.P. 6128, Succursale "A", Montreal,            |
Quebec, Canada.   H3C 3J7                       |




More information about the Comp.sys.sgi mailing list