Re: [netcdfgroup] How to inquire if a NetCDF file is already open for reading or writing?

  • To: Hernan Arango <arango@xxxxxxxxxxxxxxxxxx>
  • Subject: Re: [netcdfgroup] How to inquire if a NetCDF file is already open for reading or writing?
  • From: Ryo Furue <ryofurue@xxxxxxxxx>
  • Date: Sat, 23 Jan 2021 15:21:53 +0900
Hi Hernan and Lucas

On Sat, Jan 23, 2021 at 12:35 PM Hernan Arango <arango@xxxxxxxxxxxxxxxxxx>
wrote:

>  Given ONLY the NetCDF dataset filename, is there a function that can be
> used in a Fortran code (or a C-binding routine) to inquire if the filename
> is open (with a logical true or false result).
>

You can use Linux's command "lsof" to see if the file is already opened or
not:

call execute_command_line("lsof yourdatafile.nc", exitstat=i)
if (i == 0) then ! it's opened.


because lsof returns a nonzero integer on error and error includes the case
where the file is not opened by any processes.

But, there are other reasons for error.  To make your program robust, you
would need to read the manual of lsof or to examine the output of the lsof
command like so

call execute_command_line("lsof yourdatafile.nc > output.txt 2>&1",
existstat=i)
open(newunit=uni, file="output.txt", . . .)
! . . . examine the contents of output.txt . . .


(I wish Fortran had a facility like popen() so that you could examine the
output more easily.)

  If true,  return the one or more identifiers (vector) associated with the
> opening of that specific filename.   Notice that It is possible that such
> filename has been opened more than once and not closed.
>

lsof would show all processes that have opened the file, except that you
might encounter permission problems: you may not be allowed to know about
processes not owned by you.  (The same problem should occur with /proc/*/fd
.)

Hope this helps.

Ryo
  • 2021 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: