Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!sri-spam!sri-unix!hplabs!hpcea!hpfcdc!hpfclj!rdg From: rdg@hpfclj.HP.COM (Rob Gardner) Newsgroups: net.unix Subject: Re: Dissimilar Program Interaction? (sorry, long) Message-ID: <1560002@hpfclj.HP.COM> Date: Tue, 7-Oct-86 14:44:37 EDT Article-I.D.: hpfclj.1560002 Posted: Tue Oct 7 14:44:37 1986 Date-Received: Sun, 12-Oct-86 02:10:14 EDT References: <19300054@uiucdcsb> Organization: HP Ft. Collins, Co. Lines: 61 > > Unfortunately I don't know how to > > connect the pipe(s) to standard input and standard output > > of the sub-program. > > Also this does not permit fscan and fprint. > > int fildes[2]; > int pid; > int c; > > pipe (fildes); > > if( (pid = fork()) == 0 ) > { > /* CHILD */ > > close(0); > dup (fildes[0]); /* Redirect stdin of child from parent*/ > close(1); > dup (fildes[1]); /* Redirect stdout of child to parent */ > > close (fildes[0]); /* Don't need these anymore */ > close (fildes[1]); > > exec_ (the_child); > /* > * Now when the child writes to its file descriptor 1, it is > * actually going down the pipe's write end. Likewise its read > * on file descriptor 0 is actually reading from the read end of > * the pipe. > */ > } Be warned: the above code does NOT work. Pipes are unidirectional, and you need two of them to do what you want. The above code runs a program with its stdout connected to its stdin! What you really want is something like: int input[2], output[2]; pipe(input); /* parent reads 0, child writes 1 */ pipe(output); /* parent writes 1, child reads 0 */ if (fork() == 0) { close(0); dup(output[0]); close(1); dup(input[1]); close(output[1]); close(input[0]); exec... } close(output[0]); close(input[1]); /* now reads on input[0] will read stuff output by child, and writes on output[1] will be read by child. another neat thing is that when the child dies, the reads will fail inthe parent, and the writes will produce SIGPIPE */ Rob Gardner {ihnp4,hplabs,hpbbn}!hpfcla!rdg Hewlett Packard or rdg%hpfcla@hplabs.hp.com Fort Collins, Colorado 303-229-2048