Multiple processes accessing the same file

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Running windows XP:

I am using perl to run multiple simultaneous instances of the same
fortran program.  The fortran program parses 2 files, one of which is
common between the two instances, i.e.
fortran1 file1.dat file2.dat
fortran2 file1.dat file3.dat

when I run the processes I get the error:
forrtl: The process cannot access the file because it is being used by
another process.
forrtl: severe (30): open failure, unit 24, file file1.dat

I do not have access to the fortran source, but the developer tells me
that the open call does not lock the file for reading, and if I run
each instance of the program from different machines on a network there
are no errors.

Here is the Perl code I am using to call the processes:

use strict;
use Win32::Process;
use Win32;

my ($proc1, $proc2);
               "fortran test1.dat",
               ".") || die ERRORREPORT();
               "fortran test2.dat",
               ".") || die ERRORREPORT();

sub ErrorReport{
    print Win32::FormatMessage( Win32::GetLastError() );

Any ideas on what is happening or whether I need to fix the problem in
my perl code or the fortran?

Re: Multiple processes accessing the same file

Quoted text here. Click to load it

Well, I'm a little short on data to be definitive, but I'd guess that
you probably need to fix the problem in the Fortran source, which is
unfortunate if you don't have access to that.

If you are reporting the exact words of the developer, they are a little
"strange" for Fortran. Fortran doesn't have an open "call" or the
concept of locking. But then, the words might have gotten a bit mangled.
Anyway, my first guess.... but very weak because of the lack of data...

A plain old ordinary Fortran OPEN (statement, not call) will typically
open a file for both reading and writing unless you go out of your way
to specify otherwise. In fact, in f77, there wasn't even a way to
specify otherwise, so lots of old code has problems like this (not to
speak of new code written by people with old habits). File locking is
not addressed by the Fortran standard, but it is quite typical (and
reasonable) for an operating system to refuse to let two processes open
the same file for writing at the same time unless you go to a lot of

Again, the Fortran standard doesn't specify this, but it is very typical
(perhaps even universal in practice these days) for multiple OPENs to
work fine as long as they all specify that only read access is required.
This is done with action='read' in the OPEN statement. Note it is
action= instead of access=; that's easy to confuse, but access= is
something unrelated. Note also that there is nothing about locking;
that's why the reported statement from the developer puzzles me. The
Fortran OPEN just specifies the desired action; any locking implications
are up to the implementation, but typically (always?) multiple OPENs
with action='read' will be ok. There are sometimes compiler-specific
extensions to specify things like sharing/locking modes, but for a
common and simple scenario like this, you shouldn't need those.

P.S. Of course there is always the workaround of making multiple copies
of the file, but that's far from ideal for many reasons.

Richard Maine                       |  Good judgment comes from experience;
email: my first.last at org.domain  |  experience comes from bad judgment.
org: nasa, domain: gov              |        -- Mark Twain

Re: Multiple processes accessing the same file

Thanks a lot; from all the tests I've run it sounds like that is the
problem (didn't even think about it since I'm used to specifying
read/write in Perl and C++).  As far as what the developer said, I
think that was more what I heard rather than exactly what he said.

Site Timeline