www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Bidirectional PIPE, and do not wait for child to terminate

reply "seany" <seany uni-bonn.de> writes:
I read the manual here: 
http://dlang.org/phobos/std_process.html#.spawnProcess

However, I need to (I can not remember, nor can I find in the 
forums any info thereon) create

1. Bidirectional Pipes - I would like to write something to a 
second program (UNIX, resp GNU/LINUX environment) and listen to 
what it has to say.

2. I would like to continue with my program when the child 
process has spawned (the child process is guaranteed to 
terminate), unlike wait, and trywait does not seem to guarantee 
that the parent process will continue.

Help? Please.
Jul 01 2014
parent reply Justin Whear <justin economicmodeling.com> writes:
On Tue, 01 Jul 2014 13:00:47 +0000, seany wrote:

 I read the manual here:
 http://dlang.org/phobos/std_process.html#.spawnProcess
 
 However, I need to (I can not remember, nor can I find in the forums any
 info thereon) create
 
 1. Bidirectional Pipes - I would like to write something to a second
 program (UNIX, resp GNU/LINUX environment) and listen to what it has to
 say.
 
 2. I would like to continue with my program when the child process has
 spawned (the child process is guaranteed to terminate), unlike wait, and
 trywait does not seem to guarantee that the parent process will
 continue.
 
 Help? Please.
A pipe can be unidirectional only, but you can use more than one. In this case, you want to create at least two files in the parent process, one open for writing and one for reading. Pass the one open for writing as the child's stdin, the one for reading as the child's stdout. This will allow you to write to and read from the child. Regarding your second question, spawnProcess is not blocking, so the parent process will continue execution immediately. Unless you mean you want the child process to outlive the parent, in which case... Having the parent terminate without causing problems for the child is a tricky issue. Here's why: when the parent terminates, the child is going to be unowned and have its stdin and stdout closed (because the parent's ends are closed). It is possible to reown the process (to nohup for instance), but I'm not sure what you can do about the closed stdin and stdout.
Jul 01 2014
next sibling parent Justin Whear <justin economicmodeling.com> writes:
On Tue, 01 Jul 2014 13:00:47 +0000, seany wrote:
 1. Bidirectional Pipes - I would like to write something to a second
 program (UNIX, resp GNU/LINUX environment) and listen to what it has to
 say.
BTW, for convenience, you probably want to use pipeProcess or pipeShell.
Jul 01 2014
prev sibling parent reply "seany" <seany uni-bonn.de> writes:
On Tuesday, 1 July 2014 at 15:32:31 UTC, Justin Whear wrote:

 A pipe can be unidirectional only, but you can use more than 
 one.
and what about FIFO or LIFO s?
Jul 01 2014
parent reply Justin Whear <justin economicmodeling.com> writes:
On Tue, 01 Jul 2014 20:42:14 +0000, seany wrote:

 On Tuesday, 1 July 2014 at 15:32:31 UTC, Justin Whear wrote:
 
 
 A pipe can be unidirectional only, but you can use more than one.
and what about FIFO or LIFO s?
You can use the C mkfifo function (import core.sys.posix.sys.stat) to create new named pipes. Once created you should be able to open a FIFO with `File("blah.ipc", "rw")` in both processes. Because you aren't using the standard file descriptors, you can spawn the child process however you like.
Jul 01 2014
parent Justin Whear <justin economicmodeling.com> writes:
On Tue, 01 Jul 2014 21:00:58 +0000, Justin Whear wrote:

 On Tue, 01 Jul 2014 20:42:14 +0000, seany wrote:
 
 On Tuesday, 1 July 2014 at 15:32:31 UTC, Justin Whear wrote:
 
 
 A pipe can be unidirectional only, but you can use more than one.
and what about FIFO or LIFO s?
You can use the C mkfifo function (import core.sys.posix.sys.stat) to create new named pipes. Once created you should be able to open a FIFO with `File("blah.ipc", "rw")` in both processes. Because you aren't using the standard file descriptors, you can spawn the child process however you like.
Actually, if I recall correctly, trying to read and write a FIFO pipe from the same end is undefined behavior on Linux and illegal in Unix. If you really want a single "pipe" with two-way traffic, use a domain socket.
Jul 01 2014