File Locking - Lock Files

Shell scripts and other programs often use a strategy similar to the use of file locking: creation of lock files, which are files whose contents are irrelevant (although often one will find the process identifier of the holder of the lock in the file) and whose sole purpose is to signal by their presence that some resource is locked. A lock file is often the best approach if the resource to be controlled is not a regular file at all, so using methods for locking files does not apply. For example, a lock file might govern access to a set of related resources, such as several different files, directories, a group of disk partitions, or selected access to higher level protocols like servers or database connections.

When using lock files, care must be taken to ensure that operations are atomic. To obtain a lock, the process must verify that the lock file does not exist and then create it, whilst preventing another process from creating it in the meantime. Various methods to do this include:

  • Using the lockfile command (a conditional semaphore-file creator distributed in the procmail package).
  • System calls that create a file, but fail if the file already exists. (System calls are available from languages such as C or C++, and shell scripts can make use of noclobber)
  • Using the mkdir command and checking the exit code for failure

Certain Mozilla products (such as Firefox, Thunderbird, Sunbird) use this type of file resource lock mechanism (using a temporary file named "parent.lock".)

Read more about this topic:  File Locking

Famous quotes containing the words lock and/or files:

    and wife or husband
    who does not lock the door of the marriage
    against you, finds you
    not as unwelcome third in the room, but as
    the light of the moon on flesh and hair.
    Denise Levertov (b. 1923)

    But some who this blithe mood present,
    As on in lightsome files they fare,
    Shall die experienced ere three days be spent—
    Perish, enlightened by the vollied glare;
    Or shame survive, and, like to adamant,
    Thy after shock, Manassas, share.
    Herman Melville (1819–1891)