Path: utzoo!attcan!uunet!virtech!cpcahil From: cpcahil@virtech.uucp (Conor P. Cahill) Newsgroups: comp.lang.c Subject: Re: Filtering I/O -- both of them. Message-ID: <1990Jul08.223103.1244@virtech.uucp> Date: 8 Jul 90 22:31:03 GMT References: <1990Jul8.120742.18213@lth.se> Reply-To: cpcahil@virtech.UUCP (Conor P. Cahill) Organization: Virtual Technologies Inc., Sterling VA Lines: 34 In article <1990Jul8.120742.18213@lth.se> d89cb@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