Reading input from piped stdin.

nazriel spam at dzfl.pl
Fri Feb 14 11:09:05 PST 2014


On Friday, 14 February 2014 at 19:05:02 UTC, Thomas wrote:
> I'm new to D, and I find it quite enjoyable so far.
> I have however stumbled upon a problem which I can't seem to
> figure out.
>
> I am trying to make a program that creates a child process,
> writes something to the child process stdin and reading from its
> stdout. I am going to use it later for testing out process pair
> redundancy.
>
> Appearently the child blocks at "s = stdin.readln()". If I 
> remove
> all writing to the child, and instead just read its output,
> everything works fine. My code is attached below:
>
> import std.process,std.stdio,std.getopt,core.thread;
>
> void main(string[] args){
> 	bool backup = false;
> 	getopt(args, "backup", &backup);
> 	writeln("Something worked!");
> 	string s = "test";
> 	if (backup){
> 		writeln("Backup up & running");
> 		while(true){
> 			s = stdin.readln();
> 			writeln(s);
> 		}
> 	}
> 	auto pipes = pipeProcess(["./pipetest", "--backup"],
> Redirect.all);
> 	for(int j = 0; j<5; j++){
> 		writeln(j);
> 		pipes.stdin.writeln(j);
> 		writeln(pipes.stdout.readln());
> 		Thread.sleep(500.msecs);
> 	}
> 	while(true){}
>
> }
>
>
>
> If anyone could spot what rudimentary mistake I have done, I
> would greatly appreciate it. Alternatively, suggesting another
> way to implement inter-process communication would also be
> appreciated :D

Maybe try closing stdin pipe after you are done writing to child.

pipes.stdin.close();

Or try flushing after writing to childs stdin, IIRC:

pipes.stdin.flush();





More information about the Digitalmars-d-learn mailing list