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:

    Frankly, it’s good enough to lock up in a drawer.
    Molière [Jean Baptiste Poquelin] (1622–1673)

    The good husband finds method as efficient in the packing of fire-wood in a shed, or in the harvesting of fruits in the cellar, as in Peninsular campaigns or the files of the Department of State.
    Ralph Waldo Emerson (1803–1882)