digitalmars.D.learn - Writing to stdin of a process
- Craig Dillabaugh (30/30) Apr 26 2014 I want to be able to write to the stdin stream of an external
- yazd (5/35) Apr 26 2014 What you are missing is a call to flush() after writeln(). This
- Adam D. Ruppe (16/17) Apr 26 2014 In this case, I'd close the pipe when you're done.
- Craig Dillabaugh (4/22) Apr 26 2014 Thank you Adam, and yazd for your responses, now it works.
I want to be able to write to the stdin stream of an external process using std.process. I have the following small test app. myecho.d ------------------------------------------------------------------ import std.stdio; void main(string[] args) { foreach (line; stdin.byLine()) { stdout.writeln(line); } } ------------------------------------------------------------------ If I call this from the command line, it echo's back whatever I write to it. Then I have the following program. testpipes.d ------------------------------------------------------------------- import std.process; import std.stdio; void main( string[] args ) { auto pipes = pipeProcess("./myecho", Redirect.stdin ); scope(exit) wait(pipes.pid); pipes.stdin().writeln("Hello world"); } -------------------------------------------------------------------- If I compile and run testpipes.d, nothing happens. I was expecting it to echo back "Hello world" to me. Can anyone tell me what I am dong wrong.
Apr 26 2014
On Saturday, 26 April 2014 at 08:45:59 UTC, Craig Dillabaugh wrote:I want to be able to write to the stdin stream of an external process using std.process. I have the following small test app. myecho.d ------------------------------------------------------------------ import std.stdio; void main(string[] args) { foreach (line; stdin.byLine()) { stdout.writeln(line); } } ------------------------------------------------------------------ If I call this from the command line, it echo's back whatever I write to it. Then I have the following program. testpipes.d ------------------------------------------------------------------- import std.process; import std.stdio; void main( string[] args ) { auto pipes = pipeProcess("./myecho", Redirect.stdin ); scope(exit) wait(pipes.pid); pipes.stdin().writeln("Hello world"); } -------------------------------------------------------------------- If I compile and run testpipes.d, nothing happens. I was expecting it to echo back "Hello world" to me. Can anyone tell me what I am dong wrong.What you are missing is a call to flush() after writeln(). This is necessary for pipes as they are only automatically flushed if an internal buffer is full, AFAIK.
Apr 26 2014
On Saturday, 26 April 2014 at 08:45:59 UTC, Craig Dillabaugh wrote:Can anyone tell me what I am dong wrong.In this case, I'd close the pipe when you're done. pipes.stdin().writeln("Hello world"); pipes.stdin.close; myecho loops on stdin until it receives everything; until the pipe is closed. testpipes waits for myecho to complete before termination. The two processes are waiting on each other: testpipes won't close the file until it exits, and myecho won't exit until testpipes closes the file. an explicit call to close breaks the deadlock. In this case, flushing would cause the one line to appear, because of the buffering yazd talked about, but the program still wouldn't terminate since a flushed buffer still potentially has more coming so echo would see that line, then wait for more..
Apr 26 2014
On Saturday, 26 April 2014 at 13:30:41 UTC, Adam D. Ruppe wrote:On Saturday, 26 April 2014 at 08:45:59 UTC, Craig Dillabaugh wrote:Thank you Adam, and yazd for your responses, now it works. Thanks also for the explanations, I was wondering how myecho.d would know when the input was finished so it could terminate.Can anyone tell me what I am dong wrong.In this case, I'd close the pipe when you're done. pipes.stdin().writeln("Hello world"); pipes.stdin.close; myecho loops on stdin until it receives everything; until the pipe is closed. testpipes waits for myecho to complete before termination. The two processes are waiting on each other: testpipes won't close the file until it exits, and myecho won't exit until testpipes closes the file. an explicit call to close breaks the deadlock. In this case, flushing would cause the one line to appear, because of the buffering yazd talked about, but the program still wouldn't terminate since a flushed buffer still potentially has more coming so echo would see that line, then wait for more..
Apr 26 2014