Weird Process problem

F

FET

Hi all,
I am using the following snippet of code in a program of mine:

final String[] saCommandArray =
{
"/bin/sh" ,
"scripts/RESTORE_MY_DB.sh" ,
"192.12.52.21" ,
"5432" ,
"mydb_admin" ,
"my_db" ,
"mydbpass" ,
"MY_BACKUP_FILE.SQL"
};

Process proc = Runtime.getRuntime().exec ( saCommandArray ) ;

//////////////////FROM HERE//////////////////
BufferedInputStream bis = new BufferedInputStream (
proc.getInputStream () ) ;
BufferedOutputStream fos = new BufferedOutputStream ( new
FileOutputStream ( "/tmp/out" ) );
int iRead = 0 ;
while ( (iRead = bis.read () ) != -1 )
{
fos.write ( iRead ) ;
}
fos.close () ;
bis.close () ;
/////////////////TILL HERE////////////////
final int iWait = proc.waitFor() ;
final int iRetVal = proc.exitValue() ;
if ( 0 != iRetVal )
{
throw new Exception ( "Error, script returned " +
Integer.toString ( iRetVal ) ) ;
}

If I comment the block of code between "FROM HERE" to "TILL HERE",
then the waitFor () call just hangs. But with that block of code in
place, then command executes successfully and comes out of the waitFor
because the process is over by the time the WHILE loop finishes
executing.
What is the reason for this strange behaviour ? I am using Linux, and
the shell script that is being executed produces a lot of output
statements (basically its a 'psql' call).
Please help.
Thanks in advance.
Best regards.
 
M

Matt Humphrey

FET said:
Hi all,
I am using the following snippet of code in a program of mine:

final String[] saCommandArray =
{
"/bin/sh" ,
"scripts/RESTORE_MY_DB.sh" ,
"192.12.52.21" ,
"5432" ,
"mydb_admin" ,
"my_db" ,
"mydbpass" ,
"MY_BACKUP_FILE.SQL"
};

Process proc = Runtime.getRuntime().exec ( saCommandArray ) ;

//////////////////FROM HERE//////////////////
BufferedInputStream bis = new BufferedInputStream (
proc.getInputStream () ) ;
BufferedOutputStream fos = new BufferedOutputStream ( new
FileOutputStream ( "/tmp/out" ) );
int iRead = 0 ;
while ( (iRead = bis.read () ) != -1 )
{
fos.write ( iRead ) ;
}
fos.close () ;
bis.close () ;
/////////////////TILL HERE////////////////
final int iWait = proc.waitFor() ;
final int iRetVal = proc.exitValue() ;
if ( 0 != iRetVal )
{
throw new Exception ( "Error, script returned " +
Integer.toString ( iRetVal ) ) ;
}

If I comment the block of code between "FROM HERE" to "TILL HERE",
then the waitFor () call just hangs. But with that block of code in
place, then command executes successfully and comes out of the waitFor
because the process is over by the time the WHILE loop finishes
executing.
What is the reason for this strange behaviour ? I am using Linux, and
the shell script that is being executed produces a lot of output
statements (basically its a 'psql' call).

I think it's operating correctly. If you do not include the stream reading
code, your exec process generates output that eventually fills up its output
buffer. This buffer is unrelated to your BufferedInputStream but is part of
the OS exec launch mechanism and its size varies. I've seen it around
1K-4Kor so. When the buffer gets full the shell process blocks. Now you
have deadlock because the shell is waiting for your java app to read the
data and your java app is waiting for the process to finish. Alternatively,
when you include the stream reading code, your reader is instructed to read
until the stream gets EOF (is closed), which will happen when your shell
finishes. By the time your reading is done the waitFor has nothing left to
wait for. This is normal.

You are probably lucky to get anything to work at all because the same
blocking behaviour occurs when the exec process writes output to standard
error. The normal practice for exec is that you must read both standard
output and standard error in separate threads. Try this:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
B

bilaribilari

Thanks a lot! That solved the problem. Intuitively, I also arrived upon
the same conclusion (that the buffer is waiting to be read), but I just
wanted to be sure. Thanks again.

Best regards.
 

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,772
Messages
2,569,593
Members
45,113
Latest member
Vinay KumarNevatia
Top