Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Using exec() to run external processes

Reply
Thread Tools

Using exec() to run external processes

 
 
John English
Guest
Posts: n/a
 
      10-24-2005
I have a couple of problems using exec():

1) I'm running a program which produces output to both standard
output and standard error streams. How can I read from one or
the other, depending on which has been written to? At the moment
I read from the standard output stream, but this blocks if there
is nothing to be read and I never get a chance to read from the
standard error stream. As a workaround I use "2>&1" to combine
the streams, but might not always be able to do so easily (e.g.
if I want to redirect the standard output to a file).

2) I have a separate thread which calls destroy() on the Process
after 10 seconds to limit maximum execution time. If I have a
program which runs for 50 seconds, it calls destroy() correctly
after 10 seconds, but the call to waitFor() doesn't complete
until the full 50 seconds have elapsed, and then bad things
start happening (e.g. a database connection gets dropped for
no apparent reason). This is on Windows XP, which may be part
of the problem...

Any ideas?

TIA,

-----------------------------------------------------------------
John English | (E-Mail Removed)
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
-----------------------------------------------------------------
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      10-24-2005
On Mon, 24 Oct 2005 13:06:27 +0100, John English <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>1) I'm running a program which produces output to both standard
> output and standard error streams. How can I read from one or
> the other, depending on which has been written to? At the moment
> I read from the standard output stream, but this blocks if there
> is nothing to be read and I never get a chance to read from the
> standard error stream. As a workaround I use "2>&1" to combine
> the streams, but might not always be able to do so easily (e.g.
> if I want to redirect the standard output to a file).


Use separate three threads to write, and read each of the err and
output streams.

See http://mindprod.com/jgloss/exec.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      10-24-2005
On Mon, 24 Oct 2005 13:06:27 +0100, John English <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>2) I have a separate thread which calls destroy() on the Process
> after 10 seconds to limit maximum execution time. If I have a
> program which runs for 50 seconds, it calls destroy() correctly
> after 10 seconds, but the call to waitFor() doesn't complete
> until the full 50 seconds have elapsed,


If you want the waitfor() to terminate early, you must call
thatThread.interrurupt() from some other thread.

waitfor() is sitting there waiting for either you or the child
spawned's task return to kick it off again.

see http://mindprod.com/jgloss/exec.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
John English
Guest
Posts: n/a
 
      10-25-2005
Roedy Green wrote:

> On Mon, 24 Oct 2005 13:06:27 +0100, John English <(E-Mail Removed)>
> wrote, quoted or indirectly quoted someone who said :
>
>
>>2) I have a separate thread which calls destroy() on the Process
>> after 10 seconds to limit maximum execution time. If I have a
>> program which runs for 50 seconds, it calls destroy() correctly
>> after 10 seconds, but the call to waitFor() doesn't complete
>> until the full 50 seconds have elapsed,

>
>
> If you want the waitfor() to terminate early, you must call
> thatThread.interrurupt() from some other thread.


I seem to be doing that. My watchdog thread does this:

try {
sleep(timeout);
process.destroy();
parentThread.interrupt();
}
catch (InterruptedException e) { }

and the parent thread does this:

Process p = runtime.exec(command);
BufferedReader r = new BufferedReader(
new InputStreamReader(p.getInputStream())
);
int x = -1;
String s = null;
Watchdog w = new Watchdog(p,Thread.currentThread(),timeout);
w.start();
try {
while ((s = r.readLine()) != null) {
...
}
p.waitFor();
x = p.exitValue();
w.interrupt();
}
catch (InterruptedException e) {
...
}

-----------------------------------------------------------------
John English | (E-Mail Removed)
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
-----------------------------------------------------------------
 
Reply With Quote
 
John English
Guest
Posts: n/a
 
      10-25-2005
Roedy Green wrote:

> On Mon, 24 Oct 2005 13:06:27 +0100, John English <(E-Mail Removed)>
> wrote, quoted or indirectly quoted someone who said :
>
>
>>1) I'm running a program which produces output to both standard
>> output and standard error streams. How can I read from one or
>> the other, depending on which has been written to? At the moment
>> I read from the standard output stream, but this blocks if there
>> is nothing to be read and I never get a chance to read from the
>> standard error stream. As a workaround I use "2>&1" to combine
>> the streams, but might not always be able to do so easily (e.g.
>> if I want to redirect the standard output to a file).

>
>
> Use separate three threads to write, and read each of the err and
> output streams.


Ah, good idea. Thanks.

-----------------------------------------------------------------
John English | (E-Mail Removed)
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
-----------------------------------------------------------------
 
Reply With Quote
 
Dom Gilligan
Guest
Posts: n/a
 
      10-25-2005
John - burks.bton.ac.uk seems to have been offline for the past few
weeks; are you aware of this? Is it mirrored anywhere else?

Thanks -

Dom
 
Reply With Quote
 
John English
Guest
Posts: n/a
 
      10-27-2005
Dom Gilligan wrote:

> John - burks.bton.ac.uk seems to have been offline for the past few
> weeks; are you aware of this? Is it mirrored anywhere else?


Ah, must change my .sig; the server has been decommissioned now.

-----------------------------------------------------------------
John English | (E-Mail Removed)
Senior Lecturer | http://www.it.bton.ac.uk/staff/je
School of Computing & MIS |
University of Brighton |
-----------------------------------------------------------------
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Controlling processes and what to "feed" other processes Marc Heiler Ruby 1 05-24-2009 05:37 PM
Western Digital EXTERNAL "Elements USB model" drive - How do I run it run under Windows 98 please me Computer Support 3 09-29-2008 09:40 PM
Using fork() on XP to run processes in parallel Franklin H. Perl Misc 8 05-02-2005 10:24 PM
External Processes Darren Smith Perl 1 10-12-2004 03:14 AM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM



Advertisments