I've got a problem with <defunct> (zombie) processes on Linux...
I'm running child processes using Runtime.exec() and it usually goes well.
But just sometimes (undefined sometimes), process finishes as defunct. Is
there anything I can do about it? I suck out both inputStream and
errorStream, check Process.exitValue(), but it stucks on Process.waitFor().
I even try to suck these streams in separate threads, but it doesn't help.
I had the idea to detect if the process has stucked, and then try from
another thread to Process.destroy(), but it didn't help! Destroy does
nothing, process is still defunct, and waitFor() blocks it again... There
are no more methods in class Process to try, and I've already tried all the
combinations of existing ones...
Newsgroups and web are full of similar questions, but no answer. They all
say that a process is zombie if it is finished, but parent process hasn't
yet read the exit code. But I do read exitValue, and I also do
Process.waitFor(). Can I consider this a bug in Java VM?
Where could find the solution? Which direction should I go? I've been trying
for months, but no results...
Now, this is the latest version of code, as seen on java.sun.com...
{
....
Process process = Runtime.getRuntime().exec(commandLine);
inputVacuum = new TextVacuum(process.getInputStream());
errorVacuum = new TextVacuum(process.getErrorStream());
process.waitFor();
int exitValue = process.exitValue();
....
}
public class TextVacuum extends Thread {
private BufferedReader reader;
private StringBuffer buffer = new StringBuffer();
....
public TextVacuum(InputStream inStream) {
reader = new BufferedReader(new InputStreamReader(inStream));
start();
}
public void run() {
try {
char[] cbuf = new char[4096];
int numRead = reader.read(cbuf);
while (numRead != -1) {
buffer.append(new String(cbuf, 0, numRead));
numRead = reader.read(cbuf);
}
}
catch (IOException e) { }
finally {
try { reader.close(); }
catch (IOException e) { }
reader = null;
}
}
....
}
Thanks!