3

I am trying to do the following :

A central application, let us call it alpha, accepts user inputs in command lines, and based on those inputs, spawns other processes, call them bravo, charlie, etc. I want the parent and child to communicate both way. That is, alpha can read from and write to bravo (resp. charlie) and vice-versa

In C++, i can use fork() then exec(), and use FIFO-s - some excellent tutorials, are here : Pipe, Fork, and Exec - Two Way Communication Between Parent and Child Process and http://pronix.linuxdelta.de/C/Linuxprogrammierung/Linuxsystemprogrammieren_C_Kurs_Kapitel5b.shtml

But, I am wondering if the same, is also possible in D? I dont find much from web searching.

Community
  • 1
  • 1
Sean
  • 789
  • 6
  • 26
  • 1
    You can do the same things in D as you can in C and C++. import core.sys.posix.unistd; will get you fork, and I think exec and pipe too. – Adam D. Ruppe Nov 07 '13 at 21:09

2 Answers2

4

It looks like D's standard library supports creating pipes and child processes.

http://dlang.org/phobos/std_process.html

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • My bad, I should have mentioned i am in D-2. I dont think phobos works with D-2 - could you also tell me about Tango support on this issue? – Sean Nov 07 '13 at 20:47
  • 3
    Tango and Phobos can work together on D2. It uses phobos by default, the dlang.org documentation is about phobos for d2. – Adam D. Ruppe Nov 07 '13 at 21:08
  • @Sean: You are wrong. Phobos is and was the main D runtime. Tango has many nice options, indeed, but it was never the default runtime. There is the TangoD2 project which works on top of Phobos as an extra library - https://github.com/SiegeLord/Tango-D2 . – DejanLekic Nov 08 '13 at 10:05
1

As it happens, I remember your previous post here and drew a conclusion two questions are related. For what you asked in the previous question (Switch cas Alternative in D) you really do not need some complex interprocess communication - you basically need to call std.process.execute() and get the output. Think of it as a function similar to popen().

Check that thread, and see how I implemented a very basic "fact" command using execute()... You should be able to build a much more robust solution on top of that code if you implement all necessary checks.

Community
  • 1
  • 1
DejanLekic
  • 18,787
  • 4
  • 46
  • 77