Re: fortran+java i/o (stupid) problem
From: Larry I Smith (larryXiXsmith_at_verizon.net)
Date: Mon, 13 Sep 2004 23:26:01 GMT
> Thanks Larry,
> That keeps my test case from hanging. But I'd like to be able to keep
> the streams open, since the real program repeatedly sends input and gets
> results from the fortran code. It seems like the read thread doesn't
> spring into action until the write thread is closed. Is there a way
> around this?
> Thanks again,
> Larry I Smith wrote:
The multi-threaded approach is what we always use in unix/linux.
We have several quite large (huge really) Java WEB Services that
spawn C/C++ progs to do compute-intensive work. In all cases we
use the multi-threaded approach. The Java write-thread may live
for several hours, sending GB's of data to the spawned C/C++
process. The Java read-thread lives concurrently, reading MB's
of processed data back from the spawned C/C++ process. When the
Java write-thread has sent the last of the data it closes its write
handle and quits; that causes the spawned C/C++ process to get
an EOF on its next read (or read zero bytes - depending on the
underlying OS); the C/C++ process then closes its read-handle,
does any cleanup, writes the last data back to the Java read-thread,
closes its write handle, and terminates. When the C/C++ process
closes its write handle, the Java read thread will get an EOF on
the next read, close its read handle, and quit.
It is mandatory that each process/thread close its read/write
handles before terminating - otherwise one or the other may hang
By default the Java IO pipes are buffered. You may set the IO streams
to unbuffered, but they should get flushed each time you send a newline.
You can always do a manual flush after each write to force the buffered
data down the pipe to spawned process. You also have to pay attention
to the IO buffering in your Fortran program; if it is buffered, the Java
read-thread may not see any output data from the Fortran program
until: a) the Fortran output buffer is full, b) the Fortran program
closes its write handle, c) the Fortran program ends.
In general, when two processes share an IO pipe, the write end
of the pipe should be closed first - thus signalling the reader
to close the read end of the pipe. You have two such pipes in
your sample program: java --> fortran, and fortran --> java
The implementation behind Runtime.exec() varies from OS to OS
and Java VM to Java VM. Some buffer the inter-process pipes,
and some do not. You may have to experiment a little to
figure out what works best for you. I am not familiar with
the underlying code used in the Fortran IO, buffered or
unbeffered - I do not know.
-- Anti-spam address, change each 'X' to '.' to reply directly.