Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!uwm.edu!linac,att!ucbvax!ulysses!kpv From: kpv@ulysses.att.com (Phong Vo[drew]) Newsgroups: comp.unix.wizards Subject: Re: Taking Control of stdin/stdout of a slave process Message-ID: <14485@ulysses.att.com> Date: 19 Mar 91 01:06:49 GMT References: <1991Mar13.163756.26785@evax.arl.utexas.edu> <15500@smoke.brl.mil> Organization: AT&T Bell Laboratories, Murray Hill Lines: 32 In article <15500@smoke.brl.mil>, gwyn@smoke.brl.mil (Doug Gwyn) writes: : In article <1991Mar14.140749.24337@cm.cf.ac.uk> bharat@cf-cm.computing-maths.cardiff.ac.uk (Bharat Mediratta) writes: : -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? ... : : The fellow said FILE*s, but in any case there is a fundamental problem : with the request that has no simple solution. Namely, deadlock can : easily occur in such a scenario. Only with a carefully-designed : communication protocol can one have confidence that deadlock will not : occur. That requires support on the slave end as well as the master, : so the slave cannot be an arbitrary process. : Indeed there is no way to this with the current stdio. However, a simple way to solve this problem is to introduce the notion of pool auto-synchronization. A pool is a collection of streams (FILE*'s) in which only one stream at a time can be done io on. All the other ones are guaranteed to be synchronized. Now, to put this to work, we need two things done automatically by the library: 1. popen() (or rather an appropriate extension of it) puts the two connecting streams into the same pool. 2. stdin and stdout if not seekable are put into the same pool. Given these two features, deadlocks because of buffering and simultaneous read requests can no longer arise. (2) also solves the famous C-novice problem of what happens to the prompt: printf("Please type something: "); gets(buf); I'll discuss the idea of stream pools and a few others this Summer at Usenix in a joined paper with Dave Korn.