Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #27743 > unrolled thread

a question about using ProcessBuilder to call external command

Started byzyng <xsli2@yahoo.com>
First post2014-03-12 13:06 -0700
Last post2014-03-16 01:03 -0700
Articles 5 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  a question about using ProcessBuilder to call external command zyng <xsli2@yahoo.com> - 2014-03-12 13:06 -0700
    Re: a question about using ProcessBuilder to call external command zyng <xsli2@yahoo.com> - 2014-03-12 13:10 -0700
      Re: a question about using ProcessBuilder to call external command Roedy Green <see_website@mindprod.com.invalid> - 2014-03-17 03:02 -0700
    Re: a question about using ProcessBuilder to call external command Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-12 16:36 -0400
    Re: a question about using ProcessBuilder to call external command Roedy Green <see_website@mindprod.com.invalid> - 2014-03-16 01:03 -0700

#27743 — a question about using ProcessBuilder to call external command

Fromzyng <xsli2@yahoo.com>
Date2014-03-12 13:06 -0700
Subjecta question about using ProcessBuilder to call external command
Message-ID<8f4aa46a-16f4-4b36-a951-7e8eda1692db@googlegroups.com>
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.

[toc] | [next] | [standalone]


#27744

Fromzyng <xsli2@yahoo.com>
Date2014-03-12 13:10 -0700
Message-ID<3ac9c98d-5ac9-4bf6-a28f-151ff91c08de@googlegroups.com>
In reply to#27743
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.

[toc] | [prev] | [next] | [standalone]


#27771

FromRoedy Green <see_website@mindprod.com.invalid>
Date2014-03-17 03:02 -0700
Message-ID<mvhdi9t5ft3pvok45uaat0pl4q0eeb38cm@4ax.com>
In reply to#27744
On Wed, 12 Mar 2014 13:10:58 -0700 (PDT), zyng <xsli2@yahoo.com>
wrote, quoted or indirectly quoted someone who said :

>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.
-- 
Roedy Green Canadian Mind Products http://mindprod.com
"Incorrect documentation is often worse than no documentation."
~ Bertrand Meyer (born: 1950 age: 63), creator of design by contract 
and the Eiffel language.

[toc] | [prev] | [next] | [standalone]


#27745

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2014-03-12 16:36 -0400
Message-ID<lfqggf$ad4$1@dont-email.me>
In reply to#27743
On 3/12/2014 4:06 PM, zyng wrote:
> 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.

-- 
Eric Sosman
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#27766

FromRoedy Green <see_website@mindprod.com.invalid>
Date2014-03-16 01:03 -0700
Message-ID<tlmai9tfv6t4hhmopadmm8e6udqdcjgkmk@4ax.com>
In reply to#27743
On Wed, 12 Mar 2014 13:06:42 -0700 (PDT), zyng <xsli2@yahoo.com>
wrote, quoted or indirectly quoted someone who said :

>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
-- 
Roedy Green Canadian Mind Products http://mindprod.com
"Incorrect documentation is often worse than no documentation."
~ Bertrand Meyer (born: 1950 age: 63), creator of design by contract 
and the Eiffel language.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web