🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #19523

Re: Problem using Process.waitFor() after exec()

#19523
From: Brian Burton
Date: Mon, 19 Jan 1998 00:00
59 lines
2758 bytes
Matthew Hyne wrote:
> 
> I have a peace of code that executes an application and places it's
> STDOUT and STDERR into a Java Frame.
> 
> The problem is, that after executing exec(application) I need to wait
> for it to finish before I read the buffer but waitFor() never seems to
> return.
> 
> If I replay waitFor() with a delay the program works for most
> applications - BUT WHY doesn't waitFor() work !!!
> 

Deadlock.  I have run into a similar problem in the past.  Some OS's (NT in
particular) will block a process writing to a pipe if the pipe is "full".  When
you call waitFor() with no timeout the process will block because it is waiting
for you to read some of what it has written to it's stdout and/or stderr.  And
you are waiting for the process to exit before reading anything from it's output
and error streams.  That's a classic "deadly embrace".

When you call waitFor() with a timeout you wake up after the timeout period and
read some of the process's output.  That allows the child process to continue
operation.  It's very important to read the child process's output and error
streams while it's running to avoid deadlock.  Then call waitFor() when both
streams report end of file.

All the best,
++Brian


> I have placed an except of the code below
> 
> Process process = Runtime.getRuntime().exec(command_string);
> 
> // Capture output
> BufferedReader proc_output =
>   new BufferedReader(new InputStreamReader(
>   new BufferedInputStream(process.getInputStream())));
> 
> BufferedReader proc_error =
>   new BufferedReader(new InputStreamReader(
>   new BufferedInputStream(process.getErrorStream())));
> 
> process.waitFor();
> 
> // For testing we use the below to simulate above
> //Thread.currentThread().sleep(2000);
> 
> while (proc_output.ready())
>   textout.addLine(proc_output.readLine()+"\n");
> 
> while (proc_error.ready())
>   textout.addLine(proc_error.readLine()+"\n");

-- 
Brian Burton                        Custom Software Development
Burton Computer Corporation         Java, C++, Orbix, Tuxedo
http://www.burton-computer.com      OO Consulting and Mentoring
Technical Details
Message-ID: <34C3F347.827BBD5F@burton-computer.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <3487A88C.736F96CD@tip.csiro.au>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,880cdb0fcebe042c
X-Google-Attributes: gidf74b3,public
From: Brian Burton <brian.burton@burton-computer.com>
Subject: Re: Problem using Process.waitFor() after exec()
Date: 1998/01/19
Message-ID: <34C3F347.827BBD5F@burton-computer.com>
X-Deja-AN: 317514514
Content-Transfer-Encoding: 7bit
References: <3487A88C.736F96CD@tip.csiro.au>
Mime-Version: 1.0
To: mhyne@tip.csiro.au
Content-Type: text/plain; charset=us-ascii
Organization: Burton Computer Corporation
Newsgroups: borland.public.jbuilder.java.language