Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!ukc!cf-cm!cf-cm!bharat From: bharat@cf-cm.computing-maths.cardiff.ac.uk (Bharat Mediratta) Newsgroups: comp.unix.wizards Subject: Re: Taking Control of stdin/stdout of a slave process Message-ID: <1991Mar14.140749.24337@cm.cf.ac.uk> Date: 14 Mar 91 14:07:49 GMT References: <1991Mar13.163756.26785@evax.arl.utexas.edu> Sender: news@cm.cf.ac.uk (USENET News System) Organization: University of Wales College of Cardiff Lines: 49 In article <1991Mar13.163756.26785@evax.arl.utexas.edu> rduff@evax.arl.utexas.edu (Robert Duff) writes: > >I am interested in starting a UNIX process from a program and having the >slave process' stdin and stdout piped through FILE*'s in the master process. > >I have worked with popen(), but that only allows one-way piping. >How can I get both directions piped to my controller process? Try using pipe(3), and dup2(3): #define READ 0 #define WRITE 1 main() { int master[2], slave[2]; int slave_out, slave_in; if ((pipe(local) < 0) || (pipe(remote) < 0)) { fprintf(stderr, "Pipes failed!\n"); exit(1); } if (fork() == 0) { dup2(master[READ], 0); dup2(slave[WRITE], 1); dup2(slave[WRITE], 2); /* execl the job */ } else { char ch; to_slave = slave[WRITE]; from_slave = slave[READ]; while (read(from_slave,&ch,1)==1) printf("%c", ch); } } This ought to do the trick. Writing to 'to_slave' will send bytes to the slave process, and reading from 'from_slave' will read the output from the slave process. Cheers! -Bharat -- | Bharat Mediratta | JANET: bharat@cm.cf.ac.uk | +--------------------+ UUNET: bharat%cm.cf.ac.uk%cunyvm.cuny.edu@uunet.uucp | |On a clear disk... | uk.co: bharat%cm.cf.ac.uk%cunyvm.cuny.edu%uunet.uucp@ukc| |you can seek forever| UUCP: ...!uunet!cunym.cuny.edu!cm.cf.ac.uk!bharat |