gaumondp@JSP.UMontreal.CA (Gaumond Pierre) (04/16/91)
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@JSP.UMontreal.CA Services Informatiques, Universite de Montreal. | gaumondp@centrcn.UMontreal.CA C.P. 6128, Succursale "A", Montreal, | Quebec, Canada. H3C 3J7 |
Hannu.Martikka@lut.fi (Hannu Martikka) (04/17/91)
>>>>> On 16 Apr 91 15:09:07 GMT, gaumondp@JSP.UMontreal.CA (Gaumond Pierre) said: .> character is transfered to the standard output. The processes seem .> to wait for something... for what? EOF? I think you haveto close all other pipe ends too. (stdin/stdout) Here I have simple program in witch child does who and parent does sort ie. who | sort. -- clip ---------------------------------------------------------- #include <stdio.h> #include <sys/types.h> main(argc,argv) int argc; char **argv; { int pid; int pipeline[2]; pipe (pipeline); if ((pid=fork())== -1) { perror("fork"); exit(1); } if (pid != 0) { /* father */ close(0); /* Haveto close stdin */ dup (pipeline[0]); execlp("sort","sort",0); } else { /* child */ close(1); /* Haveto close stdout */ dup(pipeline[1]); execlp("who","who",0); } } -- Regards from Goodi ______________________________________________________________________________ Internet: Hannu.Martikka@lut.fi / \ Bitnet : GOODGULF@FINFILES // \\ \-\-\-\-\-\-\ oh5lhh Hannu Martikka, Punkkerikatu /// \\\ | on 70cm 7C 36, 53850 Lappeenranta,SF /// | \\\______________|_________________________ Tel. 953-251446 | :) Lappeenranta University Of Technology | ------------------------------------------------------------------------------