Why doesn't this work (fork and pipe problem) ???

Don Steiny steiny at scc.UUCP
Fri Jul 20 03:25:02 AEST 1984


****

	When a program is reading from a pipe it does not read an EOF
until the write end of the pipe is closed.  

	When one forks a process, closes standard output and makes
file descriptor 1 the write end of the pipe, file discriptor
1 is sill open in the parent.   The problem program keeps writing to
1 in the parent, so 1 is never closed, the read inside the fork
never reads an EOF and the program hangs.  The following modifications
will make it work:

diff tst.c new.c
---------------------------------


5a6
> # include <stdio.h>
26c27,29
< 	    printf("about to exec who\n");
---
> 	    close(0);
> 	    close(pipefd[0]);
> 	    fprintf(stderr,"about to exec who\n");
28c31
< 	    printf("could not execute\n");
---
> 	    fprintf(stderr,"could not execute\n");
33a37,38
> 	    close(1);
> 	    close(pipefd[1]);
34a40
> 
38c44
< 		putchar(buffer[0]);
---
> 		putc(buffer[0],stderr);


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


				Don Steiny
				Personetics
				109 Torrey Pine Terr.
				Santa Cruz, Calif. 95060
				(408) 425-0382
				ucbvax!hplabs!pesnta!scc!steiny
				harpo!fortune!idsvax!scc!steiny



More information about the Comp.unix.wizards mailing list