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

Discussion in 'Java' started by John English, Nov 7, 2005.

  1. John English

    John English Guest

    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:
    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
    -----------------------------------------------------------------
     
    John English, Nov 7, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joshua Jones

    Process.destroy() on Win32

    Joshua Jones, Aug 27, 2004, in forum: Java
    Replies:
    8
    Views:
    6,919
    Joshua Jones
    Aug 31, 2004
  2. Replies:
    0
    Views:
    568
  3. bengali

    Destroy a process

    bengali, Oct 6, 2006, in forum: Java
    Replies:
    2
    Views:
    507
    Nigel Wade
    Oct 9, 2006
  4. Ook
    Replies:
    2
    Views:
    344
  5. Tomás Ó hÉilidhe

    [META] News server that carries comp.lang.c

    Tomás Ó hÉilidhe, Jan 15, 2009, in forum: C Programming
    Replies:
    12
    Views:
    730
    CBFalconer
    Jan 17, 2009
Loading...

Share This Page