Filtering I/O -- both of them.

Conor P. Cahill cpcahil at virtech.uucp
Mon Jul 9 08:31:03 AEST 1990


In article <1990Jul8.120742.18213 at lth.se> d89cb at efd.lth.se (Christian Brunschen) writes:
>So I thought of :
>* creating two pipes
>* fork()ing
>* in the child process, redirect stdin to one pipe & stdout to the other

You have to be very carefule to avoid a deadlock when using pipes in both
directions between two processes.

>BUT (and here's my question) :
>how do I actually redirect stdin / stdout to the pipes I created ?

>stdin = fdopen (MyPipe [1], "r");

This is real close.  what you do is:

	(void) fclose(stdin);		/* close stdin 			*/
	(void) close(0);		/* should be unnecessary	*/
	i = dup(pipe_read_fd);		/* i should be 0		*/
	(void) fdopen(i, "r");		/* stdin should map to pipe 0 	*/
	close(pipe_read_fd);		/* dont need anymore		*/

The reason for doing the dup() & fdopen is to ensure that fd0 gets 
assigned to stdin since there may be software that does a read(0,...).

Note that I did no error checking.  You should add the appropriate error
checking for each step (other than the close(0) which should fail if 
stdin was appropriately mapped to 0).


-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 



More information about the Comp.lang.c mailing list