File locking is a mechanism that restricts access to a computer file by allowing only one user or process access at any specific time. Systems implement locking to prevent the classic interceding update scenario (see race condition).
It may also refer to additional security applied by a computer user either by using Windows security, NTFS permissions or by installing a third party file locking software.
The following example illustrates the interceding update problem:
Process A reads a customer record from a file containing account information, including the customer's account balance and phone number.
Process B now reads the same record from the same file so it has its own copy.
Process A changes the account balance in its copy of the customer record and writes the record back to the file.
Process B, which still has the original stale value for the account balance in its copy of the customer record, updates the account balance and writes the customer record back to the file.
Process B has now written its stale account-balance value to the file, causing the changes made by process A to be lost.
File locking prevents this problem by enforcing the serialization of update processes to any given file. Most operating systems support the concept of record locking, which means that individual records within any given file may be locked, thereby increasing the number of concurrent update processes.
Database maintenance uses file locking, whereby it can serialize access to the entire physical file underlying a database. Although this does prevent any other process from accessing the file, it can be more efficient than individually locking a large number of regions in the file by removing the overhead of acquiring and releasing each lock.
Poor use of file locks, like any computer lock, can result in poor performance or in deadlocks.
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".)
Unlocker software.
An unlocker is a utility used to determine what process is locking a file, and displays a list of processes as well as choices on what to do with the process (kill task, unlock, etc.) along with a list of file options such as delete or rename. On some Unix-like systems, utilities such as fast and lock can be used to inspect the state of file locks by process, by file name, or both.
On Windows systems, if a file is locked, it's possible to schedule its moving or deletion to be performed on the next reboot. This approach is typically used by installers to replace locked system files. Using EMCO Move On Boot utility, it's possible to move, rename and delete locked files located on non-removable disks on the next Windows reboot.




ConversionConversion EmoticonEmoticon