Process.destroy() -- process carries on running?

J

John English

Still having trouble with Process.exec(). The story so far:
- I create a separate thread to run a process and do a join(10000)
to wait for it with a timeout of 10 seconds. If the thread is
still alive after the join, interrupt it.
- The thread running the process uses exec() to create the external
process, creates two threads to read from stdout and stderr until
they get a null (indicating the process has ended), and then waits
usinf proc.waitFor(). If interrupted, it calls proc.destroy().

I display some debugging messages, and I have written a little test
program that I spawn using exec() and which counts from 1 to 60 once
a second, writing each value to a file as it does so.

What I see is:
- after 10 seconds, a message saying "Process destroyed!" from the
line after the call to proc.destroy()
- the rest of the system carries on as normal, assuming that the
process has been destroyed
- the process continues writing output to the file for the full 60
seconds.

I'm using Java 1.5. This happens on both Windows and Linux, BTW.

Here is some more detailed code FWIW:

CommandThread c = new CommandThread(cmd,dir);
c.start();
try {
c.join(10000);
}
catch (InterruptedException e) { ... }
if (c.isAlive) {
c.interrupt();
}

//-------------------------------------------------

class CommandThread extends Thread {
private String command;
private File directory;
public CommandThread (String cmd, File dir) {
command = cmd;
directory = dir;
}
public void run() {
Process proc = null;
ReaderThread stdout = null;
ReaderThread stderr = null;
try {
proc = Runtime.getRuntime().exec(command,null,directory);
stdout = new ReaderThread(proc.getInputStream());
stderr = new ReaderThread(proc.getErrorStream());
stdout.start();
stderr.start();
exit = proc.waitFor();
}
catch (InterruptedExecption e) {
if (proc != null) {
proc.destroy();
System.out.println("Process destroyed!");
}
}
}
}

//-------------------------------------------------

class ReaderThread extends Thread {
private BufferedReader reader;
public ReaderThread (InputStream str) {
reader = new BufferedReader(new InputStreamReader(str));
}
public void run() {
try {
String line;
while ((line = reader.readLine()) != null) {
...
}
}
catch (IOException e) { ... }
}
}

-----------------------------------------------------------------
John English | mailto:[email protected]
Senior Lecturer | http://www.it.bton.ac.uk/staff/je
School of Computing & MIS | ** NON-PROFIT CD FOR CS STUDENTS **
University of Brighton | -- see http://burks.bton.ac.uk
-----------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top