Taking Control of stdin/stdout of a slave process
Bharat Mediratta
bharat at cf-cm.computing-maths.cardiff.ac.uk
Fri Mar 15 00:07:49 AEST 1991
In article <1991Mar13.163756.26785 at evax.arl.utexas.edu> rduff at 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 at cm.cf.ac.uk |
+--------------------+ UUNET: bharat%cm.cf.ac.uk%cunyvm.cuny.edu at uunet.uucp |
|On a clear disk... | uk.co: bharat%cm.cf.ac.uk%cunyvm.cuny.edu%uunet.uucp at ukc|
|you can seek forever| UUCP: ...!uunet!cunym.cuny.edu!cm.cf.ac.uk!bharat |
More information about the Comp.unix.wizards
mailing list