Process Runtime.exec causes subprocess hang.

Discussion in 'Java' started by Nishi Bhonsle, Jan 30, 2004.

  1. Hi:

    I am facing an issue on windows platform using Runtime.exec in my java program. The parent process executing Runtime.exec is not able to consume the output stream of the child process and hence the former is blocked or hangs.

    I found this mentioned in the java doc on Process
    "The parent process uses these streams(Process.getOutputStream(), Process.getInputStream(), Process.getErrorStream()) to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock. "

    In my case the output from the child process is 2-4Kbyte long. How can I workaround this hang/deadlock issue on windows platform?

    Any help would be appreciated.

    Thanks.
    Nishi Bhonsle, Jan 30, 2004
    #1
    1. Advertising

  2. "Nishi Bhonsle" <> wrote in message
    news:...
    >
    > Hi:
    >
    > I am facing an issue on windows platform using Runtime.exec in my java

    program. The parent process executing Runtime.exec is not able to consume
    the output stream of the child process and hence the former is blocked or
    hangs.
    >
    > I found this mentioned in the java doc on Process
    > "The parent process uses these streams(Process.getOutputStream(),

    Process.getInputStream(), Process.getErrorStream()) to feed input to and get
    output from the subprocess. Because some native platforms only provide
    limited buffer size for standard input and output streams, failure to
    promptly write the input stream or read the output stream of the subprocess
    may cause the subprocess to block, and even deadlock. "
    >
    > In my case the output from the child process is 2-4Kbyte long. How can I

    workaround this hang/deadlock issue on windows platform?

    You have to read both the output stream and the error output stream in
    separate threads, gobbling up the output so the exec'd process doesn't
    block. Your main launcher can wait on processFor for the process to exit.
    Here is some code that shows how to eat up stream output while waiting. The
    StreamGobbler class manages a thread that reads an input stream. The code
    that launches the process starts up two of these and then waits for the
    process to end. You will have to add whatever extra code that processes the
    output, if that's important to you.

    import java.lang.*;
    import java.io.*;
    public class StreamGobbler implements Runnable {
    String name;
    InputStream is;
    Thread thread;

    public StreamGobbler (String name, InputStream is) {
    this.name = name;
    this.is = is;
    }

    public void start () {
    thread = new Thread (this);
    thread.start ();
    }

    public void run () {
    try {
    InputStreamReader isr = new InputStreamReader (is);
    BufferedReader br = new BufferedReader (isr);

    while (true) {
    String s = br.readLine ();
    if (s == null) break;
    System.out.println ("[" + name + "] " + s);
    }

    is.close ();

    } catch (Exception ex) {
    System.out.println ("Problem reading stream " + name + "... :" + ex);
    ex.printStackTrace ();
    }
    }
    }



    public class SafeProgA {
    public static void main(String[] args) throws Exception {
    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec("Whatever your exec does");
    StreamGobbler s1 = new StreamGobbler ("stdin", p.getInputStream ());
    StreamGobbler s2 = new StreamGobbler ("stderr", p.getErrorStream ());
    s1.start ();
    s2.start ();
    p.waitFor();
    System.out.println("Process Returned");
    }
    }

    Cheers,
    Matt Humphrey http://www.iviz.com/
    Matt Humphrey, Jan 30, 2004
    #2
    1. Advertising

  3. Nishi Bhonsle

    pahujadeepanshu

    Joined:
    Nov 30, 2009
    Messages:
    1
    It hangs on Linux

    Hi Matt,

    I tried the same implementation and it hangs on Linux platform randomly if this SafeProgA is invoked around 1000 times in a loop.

    Any ideas??


    Thanks,
    Deepanshu
    pahujadeepanshu, Nov 30, 2009
    #3
    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. Jesse Cates via DotNetMonster.com

    Process.Start is causes application to hang

    Jesse Cates via DotNetMonster.com, Jan 28, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    3,527
    Steve C. Orr [MVP, MCSD]
    Jan 31, 2005
  2. Hal Vaughan
    Replies:
    11
    Views:
    1,086
    Gordon Beaton
    May 22, 2006
  3. Earl Eiland
    Replies:
    1
    Views:
    455
    Dennis Lee Bieber
    Mar 19, 2005
  4. Jeff Epler
    Replies:
    0
    Views:
    360
    Jeff Epler
    Mar 19, 2005
  5. Tim Johnson
    Replies:
    0
    Views:
    193
    Tim Johnson
    Mar 12, 2011
Loading...

Share This Page