Operating System - OpenVMS
1748006 Members
4382 Online
108757 Solutions
New Discussion юеВ

Check if a given file is open by another process

 
SOLVED
Go to solution
Xavier Llobet
New Member

Check if a given file is open by another process

I manage a series of files that are rewritten and updated by users. From time to time I compress them, but to do it without problems I have to be sure that no other process has access to the file I want to compress. Thus I would like to know how, in DCL, I may check whether a given file is being accessed (if it is, I will proceed to the next file).

9 REPLIES 9
Ian Miller.
Honored Contributor
Solution

Re: Check if a given file is open by another process

You can try and open it in DCL. Depends on how the file is opened by the users but if shared write is not allowed then
OPEN/READ/WRITE would fail if the file was open by another process.

Or look for the file name in SHOW DEVICE/FILES
____________________
Purely Personal Opinion
Heinz W Genhart
Honored Contributor

Re: Check if a given file is open by another process

First of all, Welcome to the HP ITRC Forum

With
$ SHOW DEVICE/FILES disk:
You can find open Files on this Disk.

You could also do something like this:

$ PIPE SHOW DEVICE/FILES disk: | SEARCH SYS$PIPE file-of-interest
$ sts = $STATUS

The variable sts contains the status of Search. If this status is %X10000001 the File was found and is open.

Hope that helps

Regards

Geni
The Brit
Honored Contributor

Re: Check if a given file is open by another process

If you are in a cluster environment, you will need to check on all nodes.

$ show dev /files

will only tell you if it is open on the current node.

Dave.
Craig A
Valued Contributor

Re: Check if a given file is open by another process

Xavier

What happens if an attempt is made to open a fiel while you have, presumably, have it locked due to the compress?

Personaly, I'd be looking at a defined downtime window in order for this sort of housekeeping to take place.

HTH

Craig
Xavier Llobet
New Member

Re: Check if a given file is open by another process

Thanks to all. Let me clarify:

a) I was using already the show devices, but the disks contain many many files, in a cluster environment with many users, and I manage thousands of those files, so it is not very efficient.

b) Compressing a file entails reading it in memory and creating a new, compressed version, and then I purge the old one. The access to the files is shared, so while an user writes, others may read. If a user has the file open to write some data, I read it and create the new version, the user closes the file, and I delete the old version, the user's results are lost. It has happened.

c) The solution is the OPEN/READ/WRITE, and check the $STATUS

Thanks again.
Xavier Llobet
New Member

Re: Check if a given file is open by another process

Closed.
abrsvc
Respected Contributor

Re: Check if a given file is open by another process

The best way is to coordinate file access from within the programs/applications. We accomplish this using locks and ASTs. The "compression" application will request the lock in exclusive mode. This will trigger ASTs to close/release the locks from the other applications. These applications can now wait for some action/ event flag as a trigger to "re-open" the file. Let the compression application complete the work and then set the event flag to trigger the other applications to continue on their merry way...

This is a more coordinated way and will prevent any applications from getting open errors etc.

Dan
John Gillings
Honored Contributor

Re: Check if a given file is open by another process

Xavier,

A general principle for this type of question, which comes down to...

"if I do will it fail?"

with a view to implementing

"1) test if will fail
2) if ok do
3) if not ok handle error

The problem with this concept is you're trying to predict the future, whether it be files being locked, sufficient disk space, or presence or absence of some condition. Whatever, there is a time window between steps 1 and 2. That means if you get an OK at step 1, it can have changed by the time you start step 2. Similarly, a failure at step 1 may have come good by the time you do step 3. No matter what, you have the possibility of false positives and negatives, so will still need to handle failure at step 2.

Thus, it's usually better to not even attempt to predict anything, just charge ahead and try the action. Deal with failure if it happens:

1) do
2) if failure, handle it

Sometimes the objective is to not generate unnecessary error messages. If that's the case, a fairly general DCL construct is:

$ PIPE some-command >NL: 2>NL:
$ stat=$STATUS
$ IF .NOT.stat THEN handle error

Since it's a single stage pipe, it executes in the context of the current process (so it doesn't cost you a process creation), both SYS$OUTPUT and SYS$ERROR are sent to NL:, and you get $STATUS.
A crucible of informative mistakes
Xavier Llobet
New Member

Re: Check if a given file is open by another process

Thanks John,

What you propose is a nice and general way to do things. In my case I am going to wear belt and suspenders:

1) Check whether file is unaccessed (with OPEN/READ/WRITE). If so, record the time/date of last modification, and go to 2); else, process the next file.

2) Read the file, compress it, and create a new version. If no error go to 3); else delete the new version and process the next file.

3) Check the modification date of the old version. If unchanged, delete it. If changed, delete the new version and process the next file.

This procedure is run periodically, and it is a not a big deal to skip some files: they'll be processed the next time.