Multiple processes accessing the same file

S

scottmf

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);
Win32::process::Create($proc1,
"C:\\Software\\fortran.bat",
"fortran test1.dat",
0,
NORMAL_PRIORITY_CLASS,
".") || die ERRORREPORT();
Win32::process::Create($proc2,
"C:\\Software\\fortran.bat",
"fortran test2.dat",
0,
NORMAL_PRIORITY_CLASS,
".") || 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?
 
R

Richard E Maine

scottmf said:
forrtl: The process cannot access the file because it is being used by
another process. ....
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, ....
Any ideas on what is happening or whether I need to fix the problem in
my perl code or the fortran?

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
trouble.

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.
 
S

scottmf

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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top