Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!spool.mu.edu!munnari.oz.au!uniwa!cc.curtin.edu.au!cutmcvax!corbettm From: corbettm@cutmcvax.cutmcvax.cs.curtin.edu.au (Michael Corbett) Newsgroups: comp.sys.sgi Subject: Re: Problem with pipe/fork Message-ID: Date: 17 Apr 91 08:21:40 GMT References: <1991Apr16.150557.2344@jsp.umontreal.ca> Sender: news@cutmcvax.cs.curtin.edu.au (Usenet News System) Organization: Curtin University of Technology, Computing Science Lines: 60 Nntp-Posting-Host: cutmcvax.cs.curtin.edu.au gaumondp@JSP.UMontreal.CA (Gaumond Pierre) writes: >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... This is partially correct... The EOF is not sent to the pipe until all the file descriptors have have closed in every process... due to the fork there are 2 file descriptors (one in each process) associated with the write end of the pipe.... So these both need to be closed before the loop in the child process For neatness you should also close the read end of the pipe in the parent process... I have added the appropriate lines in the code below... (Hope it helps) >------------------------------------------------------------------------------- > >main() >{ > FILE *fp; > int fdi[2]; > char c; > > pipe(fdi); > if (fork()!=0) > { /* parent */ close (fdi[0]); > fp=fdopen(fdi[1],"w"); > for (c=getchar(); !feof(stdin); c=getchar()) fputc(c,fp); > fclose(fp); > wait(0); > } > else > { /* child */ close (fdi[1]); > fp=fdopen(fdi[0],"r"); > for (c=fgetc(fp); !feof(fp); c=fgetc(fp)) putchar(c); > fclose(fp); > } >} -- >> Michael Corbett | "Last night, I thought my arm was << >> corbettm@anger.cipal.cs.curtin.edu.au | hanging out of bed. So I got out << >> corbettm@cutmcvax.cs.curtin.edu.au | to push it in." <<