David K
David K

Reputation: 1346

How to send commands to a subprocess

I am creating a simulation system which consists of a test harness creating multiple processes using ProcessBuilder. I would like to be able to send multiple commands to the seperate processes, and I've only thought of a couple options - neither of which seem very fun.

The first method would be to communicate between the parent process and the subprocesses using sockets, which is how the subprocesses communicate with each other. The other would be to use the Writer method, and I've been using the Reader method to read and print the intput stream from each process. I think that both of these would require a similar level of bookkeeping. Ideally it would be nice to call a function like you would for any subclass, but I know that just isn't how multi-process works.

Please let me know what you think the best way to implement this is!

Thanks, David

Update: I ended up creating a server socket in the test harness that communicates with all of the sub-processes. Once the system is set up, it's as simple as adding a Message to a queue, which is then sent to the correct client.

Upvotes: 2

Views: 845

Answers (1)

ArjunShankar
ArjunShankar

Reputation: 23680

This answer is in response to your statement:

"Ideally it would be nice to call a function like you would for any subclass, but I know that just isn't how multi-process works."

If you want to, here is how you can actually do that, if the sub-processes are Java programs running on a JVM:

Use Remote Method Invocation. The wikipedia article has a small example of an RMI server and client.

In essence this works in the following way:

  1. A server provides some services, via remote methods, by implementing a 'remote interface' (whose definition should be available the the client also)
  2. When the server starts up, it creates an instance of the object that implements the service, and 'binds' it to an 'RMI Registry'
  3. A client looks up the 'RMI Registry' for a remote object which it wants to call methods on, and obtains an object which appears to implement the remote interface.
  4. Then the client can call methods on this object, and the RMI runtime ensures that the call makes it to the remote object, and the results are returned.

This seems to be an 'official' Hello World example: http://docs.oracle.com/javase/1.5.0/docs/guide/rmi/hello/hello-world.html

The arguments of the call must be Serializable (so that they may be transmitted over the network). Usually, this should be as simple as appending implements Serializable to their type definition.

Upvotes: 2

Related Questions