Java Process. Destroy() does not terminate all processes
I have a java process that uses runtime getRuntime(). Exec ("mybin") launcher
This "mybin" may fork many times to complete its work
I have a reader thread to read all the outputs of "mybin" and its child nodes in the InputStream and errorstream of the process object returned by exec()
My problem is: if it takes too much time to finish the work, I need to terminate the process and wait for the reader thread to finish (if EOF is read, the reader thread will complete)
Now I find that even if I use process Destroy (), I can only kill myself and all my children's "mybin" Therefore, after the timeout, the EOF will never arrive, so the reader thread hangs until all child processing terminates
Is there any way to kill processes and runtime safely All child processes started by exec()
I'm on Linux, cross platform is not in my mind
Solution
One way to do this is to have the process call fork to save the list of child PIDs You can implement a handler in mybin to trigger the killing of "children"
Another option is to use threads instead of forks
The main problem why this doesn't work is that if you call fork, it will create a new process that has no real dependency on the parent process