Software Archive
Read-only legacy content
17061 Discussions

file access

Intel_C_Intel
Employee
645 Views
Hi all

I have a file which is accessed by 2 processes. One process writes to this file while the other constantly reads from it. I constantly get a violation acess when I do this.
I think this occurs at the instant that the read write operation occurs simultaneously by the 2 processes.

I want to be able to make the file being accessed both read / write possible, for both processes at the same time.

Could anyone be kind enough to help, or make suggestions.
MY commands in this region are as below:

thanks
eugenio

open (unit=12,file='usrx.txt',status='old')
rewind(unit=12)
read(12,7)usrw1,usrw2
7 format(f10.7,1x,f15.10)
close(12)
0 Kudos
5 Replies
isn-removed200637
645 Views
You have not said whether you are reading/writing a pair of numbers to a single or multiple records. It is much simpler to arrange if only one pair of numbers is relevant at any one time than if there is a stored sequence of number-pairs to be dealt with.

FWIW and OTTOMH, I would suggest setting up routine in a dynamic-link library which defines an array to store your data-pairs and two indexes which are updated to keep track of the number of 'writes' and the number of 'reads'. Allocate more memory for the array, as required, although I am unfamiliar with dynamic array allocation myself.

You will probably also need to keep track of the number of your reads in the read process and to keep track of the number of your writes in the write process and feed each number to the DLL routine as required.

Communication using COMMON block storage may also be possible between two or more processes running in seperate threads of the same application.
0 Kudos
david_jones
Beginner
645 Views
For a file based solution, add appropriate options to the ACCESS and SHARE keys of the open statements in the two programs and test for errors on opening via the value returned from the IOSTAT key of the open. If an error occurs due to a file clash it will be detected by IOSTAT ... in that case apply a small delay via a call to SLEEP or SLEEPQQ and try re-opening again. I would suggest slightly different delays in the two processes.

Other points:
(i) the rewind is unnecessary with CVF on Windows, but required for at least some other combinations, so this depends on what you need by way of portability of code.
(ii) if the file is large then using an unformatted open/read/write could well save a useful amount of time.
(iii) if the process doing the read is testing the new values found against old values and doing nothing unless a change is found, then you may again benefit by inserting a delay before reopenning the file ... this would prevent this process using CUP unnecessarily, thus holding up the other.

I have found this file-based approach to work OK and have resisted the DLL-based approach since the file-based approach allows two or more pairs of processes to be ongoing separately, provided they use transfer-files in different working directories.

David Jones
0 Kudos
david_jones
Beginner
645 Views
Sorry ... I meant ACTION, not ACCESS in the above.
0 Kudos
Intel_C_Intel
Employee
645 Views
Hi again
thanks for the tip it seems to be doing something. I still have bit of problem though.
Of the 2 processes I am running simultaneously only one can be easily controlled via fortran written user subroutines. The other has its own special language. Therefore the other program is difficult to control in terms of the file opening specifiers. The problem I have is that when the 2nd process tries to open a file that should be shared by the 2 process it will terminate if the initial program is accessing the common file at the same time. I cannot really control this via time delay, as timing for each process varies from each cycle.
I thought of changing the file access of the file in the initial program to make it shared so that the second process will be able to open it regardless.
This however does not seem to work too well, as the initial process still seems to loack the file.

I have used in my file specifiers, share='denynone' and also tried shared.
I am not too sure what the difference is between the 2 specifiers as well ?

could someone please help

thanks in advance eugenio
0 Kudos
david_jones
Beginner
645 Views
In my Fortran-only case, I use share='denyrw' for both processes, which means that only one process can have the file open at a given time.

I think you may need to look more carefully at the capabilities of your "special language" to see if some form of inter-process signalling can be arranged other than the ability to open a file. Possibilities would seem to be:
(i) use of one or two environment varaibles;
(ii) use of detecting the existence of certain special files, which is subtly different to trying to open files.
Then use these signals to ensure that only one process has the file containing the data open at any given time. You would probably need to draw a flow diagram to ensure that you had covered the full range of eventualities.

Another possibility would be to put one process fully under the control of the other. By this I mean that you might put a loop in the outer program which starts a "single execution" of the second, waits for it to finish and close, opens and reads a transfer file, closes it and processes it, then returns to start a another execution of the inner program. This would effectively prevent both programs accessing the file at the same time.

David Jones
0 Kudos
Reply