I have an assignment where I must create two instances of a process and the other process must terminate when one of them is terminated. I can only do this when I close the first process created, does this means that created processes have some kind of hierarchy, even though they are both children of the same process?
Thanks in advance.
ProcessBuilder pb = new ProcessBuilder(args);
Process proc_1 = pb.start();
Process proc_2 = pb.start();
System.out.println("Child is running...wait for child to terminate");
int exitValue_1 = proc_1.waitFor();
System.out.println("Child_1 finished with exit value -> " + exitValue_1);
if(exitValue_1==0) proc_2.destroy();
int exitValue_2 = proc_2.waitFor();
System.out.println("Child_1 finished with exit value -> " + exitValue_2);
if(exitValue_2==0) proc_1.destroy();
Process#exitValuewill throw aIllegalThreadStateExceptionif the process has not yet exited, you could exploit this.