Locking a file from update by other users

writelock

Lock a file from update by other users


SYNTAX

writelock file_id [riu = label] [traps = label]

The writelock command attempts to place a write lock on the file specified. The file must be open in update mode. A write lock prevents the file from being locked or updated by any other process.

The command enables the update of more than one record in a file, with the knowledge that no other user is updating the file. First the write lock is obtained, then the updates are made. Finally, the write lock is removed, freeing all the updated records simultaneously.

A write lock is removed by means of the unlock command.

file_id

The identifier given to the file when it was declared (using ![o]file) .The file must be open in update mode.

[riu = label]

Traps the condition where the read lock cannot be obtained due to an already existing locking condition.

It is not possible to obtain a write lock on a file if any record in it is currently locked by another process. If writelock discovers that the file contains a locked record, a Record in use status occurs. This event may be trapped by use of the riu clause, or the traps clause, in which case control passes to the line indicated by label.

If this condition is not trapped, the message Waiting… is displayed, and the lock is retried every three seconds until successful.

[traps = label]

General purpose trap clause that traps any condition not explicitly trapped, passing control to the line indicated by label. It has the lowest priority, and is only invoked if a trappable condition has not been more explicitly trapped.


NOTES

  • On some systems, a write lock prevents other processes from reading the file, even if these processes have the file open in read-only mode.

  • If the process attempting the write lock has a record locked itself within the file, this causes no obstacle to the write lock. The single locked record becomes part of the write lock and is unlocked along with all the other records when the write lock is removed.

  • The related readlock command locks the file from update by all processes, including the one that placed the lock. However, Windows 95, 98 and ME do not support read locks, and so all locks obtained locally on those platforms are write locks.

  • If a write lock is required on a file during a transaction, it should be applied before the begin(), and the unlock should be applied after the commit() or rollback(). An attempt to remove a write lock inside a transaction is ignored. See begin() for full details of the transaction system.

  • If the specified file is not open in update mode the write lock is downgraded to a read lock.


EXAMPLE

     openfile Tmpfile name = "tmpdata/tmpfields" create traps = FILEERR
     writelock Tmpfile

     /* statements requiring file lock */

     unlock Tmpfile
     return

FILEERR:
     error "Unable to create temporary file"
     return

RELATED TOPICS

Sculptor keyed files

unlock

readlock