Problem with pipe/fork

Paul Jackson pj at sam.wpd.sgi.com
Wed Apr 17 05:17:06 AEST 1991


In article <1991Apr16.150557.2344 at jsp.umontreal.ca>, gaumondp at JSP.UMontreal.CA (Gaumond
Pierre) writes:
[[ Sample program presented that has parent writing pipe to child.
   He asks why child doesn't notice EOF on the pipe after the parent closes it. ]]

|>   int fdi[2];
|>   
|>   pipe(fdi);
|>   if (fork()!=0) { /* parent */
	[[ Loops writing to fdi[1], then closes fdi[1]. ]]
|>   } else { /* child */
	[[ Loops reading from fdi[0] until eof - but eof never seen. ]]
|>   }
|> }

In the child, before the loop until eof, add the line:

	close(fdi[1]);

The fork resulted in both the parent and child having
fdi[1] open for writing, and the child will see eof
on fdi[0] only when all available writers to the pipe
have closed the write end.

-- 

				I won't rest till it's the best ...
				Software Production Engineer
				Paul Jackson (pj at wpd.sgi.com), x1373



More information about the Comp.sys.sgi mailing list