Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1.1 9/4/83; site scc.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxl!houxm!houxz!vax135!cornell!uw-beaver!tektronix!hplabs!pesnta!scc!steiny From: steiny@scc.UUCP (Don Steiny) Newsgroups: net.unix-wizards Subject: Re: Why doesn't this work (fork and pipe problem) ??? Message-ID: <248@scc.UUCP> Date: Thu, 19-Jul-84 13:25:02 EDT Article-I.D.: scc.248 Posted: Thu Jul 19 13:25:02 1984 Date-Received: Sat, 21-Jul-84 03:08:49 EDT References: <342@mddc.UUCP> Organization: Santa Cruz Computer, Aptos, Calif. Lines: 49 **** 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 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