2

I had a scenario where a process need to be started using ProcessBuilder. After starting the process successfully, I open the inputstream and outputstream of the created process and passed the arguments through the outputstream and retrieved the result through the inputstream.

It is working fine for the first time execution in the process. But if I need to loop multiple times with different arguments passed to the same created process, it is failing and the Process.isAlive() is returned as false.

In our scenario, the process need to be started only once. So please help me in making the process be alive and use the same process for the complete functionality.

Code snippet

ProcessBuilder pb = new ProcessBuilder(command);
Process p = pb.start();
ServerSocket server = new ServerSocket(8765);
PrintWriter pw = new PrintWriter(p.getOutputStream());
BufferedReader br = new BufferedReader( new 
InputStreamReader(p.getInputStream()) );
while(true){

    System.out.println("The p value is "+p.isAlive());

    //server waiting for client to connect
    Socket sock = server.accept();

    //reading the argument for the "command"
    DataInputStream dis = new DataInputStream(sock.getInputStream());
    String  arg=(String)dis.readUTF();
    System.out.println("data received is = "+arg);


    //code for executing the process p(command) with the arguments received from socket
    pw.println(arg);
    pw.flush();
    pw.close();

    String line = "";
    String result ="";
    //storing the result of the final_cmd with arguments in the result string                
    while ((line = br.readLine()) != null) {
        result=result+line+System.lineSeparator();
    }

    //result returning back to socket      
    DataOutputStream dout=new DataOutputStream(sock.getOutputStream());  
    dout.writeUTF(result);  
    dout.flush();  
}
user_3093890
  • 85
  • 1
  • 8
  • 1
    What you are mentioning is called interprocess communication and there are several ways to do it. You can put a message queue middleware in between(RabbitMQ, MSMQ, ZeroMQ, etc.), you can use raw sockets(because these processes are on the same pc, you can select unused localhost ports), RMI can be another alternative, shared memory, named semaphores, all can be used for this purpose – Alpay Jan 23 '18 at 13:37
  • What "Keeping a process alive" means is different for every process. Generally speaking, a process is its own thing, and it will finish when it needs to. You can instruct it not to finish until some specific input is received, but generally this question doesn't make much sense. Please add more details: what the process is, how are you trying to interact with it. – M. Prokhorov Jan 23 '18 at 13:40
  • @Alpay I tried using sockets, I created a process using ProcessBuilder outside while loop and then in the while loop I am waiting on accept(), for the first time I am able to access the Process input and output streams and it is working fine, but on the second iteration, the Process streams are not available. And the reason was the Process.isAlive() is false at that time. – user_3093890 Jan 24 '18 at 04:56
  • @user_3093890 Have you seen this sample: https://stackoverflow.com/a/10131449/1206536 This sample keeps the process up all the time by using an infinite loop, so that you can connect to this socket all the time and it supports accepting connections coming from different sources. – Alpay Jan 24 '18 at 06:22
  • @Alpay attached code snippet. Here the current java process is in while loop iterating infinitely but the process created using ProcessBuilder has exited for the single iteration – user_3093890 Jan 24 '18 at 07:22
  • @user_3093890 Actually I was trying to ask you if you have to create process this way. If both applications are yours and you are able to modify their codes, you can implement such a messaging mechanism INSTEAD OF creating process for the other application to handle your requests. – Alpay Jan 24 '18 at 10:05
  • @Alpay In our scenario, we had no option to modify the source code of the process created using ProcessBuilder. It is actually a process created in C language – user_3093890 Jan 24 '18 at 12:12
  • @user_3093890 If you have no option to modify that c code, you are absolutely stuck at that point. You may re-consider spawning these processes everytime you need, because as far as i understood, the process gets your parameters returns the result and "dies". After it dies, you have no other option other than restart a new one with your new params – Alpay Jan 24 '18 at 13:13
  • @Alpay, Yeah, checking for the other options. Thanks – user_3093890 Jan 24 '18 at 13:18

0 Answers0