Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29285 Discussions

How to check if a file is opened by another process

portoon
Beginner
842 Views

Dear friends

Is there any one know that how to check if a file is opened by another process?
I want to READ a file when the file is writing by another process with SHARE='DENYNONE' in OPEN statement.
It is sometimes OK but sometimes is NOT though both process has SHARE='DENYNONE'.
So, I would like to read the file after the writing process is done.
Please give me some comments.

Thanks a lot.

0 Kudos
2 Replies
DavidWhite
Valued Contributor II
842 Views
Quoting - portoon

Dear friends

Is there any one know that how to check if a file is opened by another process?
I want to READ a file when the file is writing by another process with SHARE='DENYNONE' in OPEN statement.
It is sometimes OK but sometimes is NOT though both process has SHARE='DENYNONE'.
So, I would like to read the file after the writing process is done.
Please give me some comments.

Thanks a lot.


the Process which is writing probably should prevent any other process from accessing the file until it has finished - I think the risk of corrupting a file is high.

the process which needs to read the file probably should use DENYWR and put this into a timer loop until you the process successfully opens the file -- suggest also a maximum number of times this can occur before issuing a diagnostic error.

My 2c worth!

David
0 Kudos
Paul_Curtis
Valued Contributor I
842 Views
Quoting - portoon

... check if a file is opened by another process?


It's easy to do this using the Win32 API functions:

[cpp]    ntry = 0
10 ihandl = CreateFile (fullpath, &
access, &
IOR(FILE_SHARE_READ,FILE_SHARE_WRITE), &
NULL_SECURITY_ATTRIBUTES, &
OPEN_ALWAYS, &
FILE_ATTRIBUTE_NORMAL, &
NULL )

IF (ihandl == INVALID_HANDLE_VALUE) THEN
IF (GetLastError() == ERROR_SHARING_VIOLATION) THEN
ntry = ntry + 1
IF (ntry > max_try) RETURN
CALL delay_ms (5)
GO TO 10
END IF
END IF
[/cpp]

0 Kudos
Reply