Java Process.Destroy() does not kill all process
I have a Java process which launch a program using Runtime.getRuntime().exec("myBin")
this "myBin" may fork several times to finish its job.
I have a reader thread to read all output of the "myBin" and its children, from the InputStream and ErrorStream of the Process object returned by exec()
My question is: if it taks too much time to finish the job, I need kill the process and开发者_StackOverflow wait for the reader thread to complete.(the reader thread will complete if it have read an EOF)
Now I found, even I use Process.Destroy(), I can only kill "myBin" insead of itself and all its children. So after the time-out, the EOF never reached, so the reader thread hang until all children process terminated...
Is there a way to safty kill process and all children launched by Runtime.exec()
I am on Linux, cross-platform is not in my mind.
One way to achieve this would be to have the process which invokes the fork to save a list of children pids. You could implement a handler in the mybin which triggers to kill also the "childrens".
Another option is to use threads instead of using forks.
The main problem why this will not work is that if you invoke fork it will create a new process which has no real dependency to the parent process.
精彩评论