www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Spawning a process: Can I "have my cake and eat it too"?

reply "Nick Sabalausky (Abscissa)" <SeeWebsiteToContactMe semitwist.com> writes:
I'd like to include this functionality in Scriptlike, but I don't know 
if it's even possible:

Launch a process (spawnProcess, pipeShell, etc) so the child's 
stdout/stderr go to the parent's stdout/stderr *without* the possibility 
of them getting inadvertently reordered/reinterleaved when viewed on the 
terminal, *and* still allow the parent to read the child's stdout and 
stderr?

How could this be accomplished? Is it even possible?
Mar 01 2018
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
I would suggest redirecting the child to the parent pipe, but 
then having the parent write the data back out to its own 
stdout/err.

It'd be a bit tricky with just Phobos' file though because it 
doesn't make it easy to wait for or be notified about input on 
it, but the underlying OS apis make this reasonably simple 
(WaitForMultipleObjects or select) if you're willing to write a 
bit more code to do it yourself or at least pull the underlying 
handles out of the Phobos file.
Mar 01 2018
prev sibling next sibling parent Vladimir Panteleev <thecybershadow.lists gmail.com> writes:
On Friday, 2 March 2018 at 04:50:06 UTC, Nick Sabalausky 
(Abscissa) wrote:
 Launch a process (spawnProcess, pipeShell, etc) so the child's 
 stdout/stderr go to the parent's stdout/stderr *without* the 
 possibility of them getting inadvertently 
 reordered/reinterleaved when viewed on the terminal, *and* 
 still allow the parent to read the child's stdout and stderr?
I think it's possible on Linux. 1. Disable buffering on the pipe (see stdbuf etc.) 2. Fake output type to fool isatty (e.g. script -c 'child_program args...' /dev/null)
Mar 01 2018
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 3/1/18 11:50 PM, Nick Sabalausky (Abscissa) wrote:
 I'd like to include this functionality in Scriptlike, but I don't know 
 if it's even possible:
 
 Launch a process (spawnProcess, pipeShell, etc) so the child's 
 stdout/stderr go to the parent's stdout/stderr *without* the possibility 
 of them getting inadvertently reordered/reinterleaved when viewed on the 
 terminal, *and* still allow the parent to read the child's stdout and 
 stderr?
 
 How could this be accomplished? Is it even possible?
You'd have to do this in the parent. You can duplicate the file descriptor, so that writing to either goes to the same spot, but you can't "fork" the file descriptor, so that writing to one goes to 2 spots. With Phobos's std.process, you would have to allocate a buffer to store the data as you read from the pipe and wrote it to the terminal, it wouldn't be automatic. I can think of an easy way to do it in iopipe, but it still would be dependent on the reader actually reading the data from the pipe. In other words, it wouldn't come out when the child wrote, it would only come out when the parent read. But this might be inherent in what you want, because the only way to prevent interleaving is to control it all from one place. -Steve
Mar 02 2018
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 3/2/18 9:23 AM, Steven Schveighoffer wrote:
 On 3/1/18 11:50 PM, Nick Sabalausky (Abscissa) wrote:
 How could this be accomplished? Is it even possible?
You'd have to do this in the parent. You can duplicate the file descriptor, so that writing to either goes to the same spot, but you can't "fork" the file descriptor, so that writing to one goes to 2 spots.
Hm.. you could potentially do this via an intermediary process that does the copying. Then you could avoid writing the buffering code in your parent or child. i.e. something like the Unix tee command. -Steve
Mar 02 2018