a question about using ProcessBuilder to call external command

Z

zyng

Hi:

I am using the following code to call a system command

final ProcessBuilder processBuilder = new ProcessBuilder(new String[]{"/bin/sh", "-c", the_command_and_arguments});

final Process processRunSysCmd = processBuilder.start();//start a new process -- a child process


I have learned that I need to drain the output of the external command (input stream to the parent process -- the java program) in case that it has too much output and the buffer is saturated. In that case, the program will hang.

But when I think deeper, I am puzzled: there are two processes running now(one is the one i called "parent process", another is the child process). Since each one is single threaded, may I say there are two threads right now?!

So if I don't drain the output of the child process, and too much output and buffer saturated, the parent process will hang?! Will the child process hang too? Or both processes hang? Since they are two threads, so one thread cause another thread hang?

Thank you.
 
Z

zyng

Following that, I have another question on different perspective. My code:
final ProcessBuilder processBuilder = new ProcessBuilder(new String[]{"/bin/sh", "-c", the_command_and_arguments});

final Process processRunSysCmd = processBuilder.start();//start a new process -- a child process

runs fine on Linux, but when on Cygwin(on Microsoft Windows), it fails due to "/bin/sh" issue. On the terminal of Cygwin, the command:
/bin/sh -c "the command and arguments"

works fine. But if calling from Java program, it is not ok. I am puzzled.

Thank you again.
 
E

Eric Sosman

Hi:

I am using the following code to call a system command

final ProcessBuilder processBuilder = new ProcessBuilder(new String[]{"/bin/sh", "-c", the_command_and_arguments});

final Process processRunSysCmd = processBuilder.start();//start a new process -- a child process


I have learned that I need to drain the output of the external command (input stream to the parent process -- the java program) in case that it has too much output and the buffer is saturated. In that case, the program will hang.

The details vary from one operating system to another, but yes:
The amount of output that can be held "in transit" waiting to be read
is usually limited, and may be rather small. If the writing process
fills that output buffer faster than the reading process drains it,
the writer will eventually need to wait for the reader to make some
progress. And if the reader's progress rate is zero, the writer may
need to wait for a v-e-r-y l-o-n-g t-i-m-e.
But when I think deeper, I am puzzled: there are two processes running now(one is the one i called "parent process", another is the child process). Since each one is single threaded, may I say there are two threads right now?!

Yes, or better "At least three, and probably more." The parent
process, written in Java and running on the JVM, is almost certainly
*not* single-threaded (think garbage collector threads, finalizer
threads, ...) But even if the JVM were single-threaded, you'd then
have a child process running "/bin/sh" (at least one thread) and
probably a grandchild process running "the_command_and_arguments"
(at least one thread), and maybe great-grandchildren and further
descendants.
So if I don't drain the output of the child process, and too much output and buffer saturated, the parent process will hang?! Will the child process hang too? Or both processes hang? Since they are two threads, so one thread cause another thread hang?

I think you've got it backwards: The *writer* will hang when its
buffer fills, but the *reader* isn't impeded. For all the system
knows, the (non-)reader may be doing some housekeeping before getting
ready to read what the writer wrote.

If you don't care about the output from the child/grandchild/...
processes, an alternative to draining and discarding it may be to
have it written to a sinkhole destination like "/dev/null" instead
of to a pipe or socket or other IPC channel.
 
R

Roedy Green

So if I don't drain the output of the child process, and too
much output and buffer saturated, the parent process will hang?! Will
the child process hang too? Or both processes hang? Since they are two
threads, so one thread cause another thread hang?

A thread is a execution stream running in the same address space.
exec/ProcessBuilder spawn a whole new address space, and usually do
not use the Java run time. If it were java,you would just call the
main method.

for tips see http://mindprod.com/jgloss/exec.html
 
R

Roedy Green

runs fine on Linux, but when on Cygwin(on Microsoft Windows), it fails due to "/bin/sh" issue. On the terminal of Cygwin, the command:
/bin/sh -c "the command and arguments"

Are you sure sh lives in the bin directory in the root of the current
drive?

Try spelling that out in full.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top