Process Runtime.exec causes subprocess hang.

N

Nishi Bhonsle

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.
 
M

Matt Humphrey

Nishi Bhonsle said:
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 (e-mail address removed) http://www.iviz.com/
 
Joined
Nov 30, 2009
Messages
1
Reaction score
0
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
 

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

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top