File locking |
File locking is a mechanism that enforces access to a computer file by only one user (computing) at any specific time. The purpose of locking is to prevent the classic interceding update scenario.
The interceding update problem may be illustrated as in the following example: computer process A reads a customer database record from a file containing account information, including the customer s account balance. Process B now reads the same record from the same file. Process A changes the account balance and writes the new value back to the file. However, process B - which still has the original values of that customer record, makes a different change to its copy of the data (for example, changes the customer s phone number) and re-writes this data to the file. Process B s copy of the data, still containing the original account balance, overwrite the changes made by process A, revert (computing) the account balance to its previous value and causing the update of the customer balance 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, so increasing the number of Concurrency (computer science) update processes.
= File locking in Windows =
Program files are automatically locked upon execution, thus preventing them from being modified or deleted while running. Programs are automatically notified if an open file is being modified by an external program. File locking in Windows is referred to as mandatory locking , since locks are enforced by the operating system.
The Windows locking approach can result in problems where the operating system or an application crashes without being terminated properly. This leads to a situation where access is denied to files even though they do not appear to be in use, because the operating system or a crashed application still has a lock on the file. The user will in this case have to terminate a program manually to remove the lock. This is typically done through the Task Manager utility.
File locking permissions are determined by the sharing mode parameter in the [http://msdn.microsoft.com/library/default.aspurl=/library/en-us/fileio/fs/createfile.asp CreateFile] function used to open files. This parameter may be either exclusive , shared read and/or shared write . The [http://msdn.microsoft.com/library/default.aspurl=/library/en-us/fileio/fs/readdirectorychangesw.asp ReadDirectoryChangesW] function can be used for notification of external changes to files opened in non exclusive mode.
Locked files, also known as file handles in Windows, can be explored with the [http://www.sysinternals.com/Utilities/ProcessExplorer.html Process Explorer] utility. This utility can also be used to force-close handles without needing to terminate the application holding them.
= File locking in UNIX =
Open files and programs are not automatically locked in UNIX. There are different kinds of file locking mechanisms available in different flavours of UNIX and many operating systems support more than one kind for compatibility. The two most common mechanisms are [http://unixhelp.ed.ac.uk/CGI/man-cgifcntl+2 fcntl] and [http://unixhelp.ed.ac.uk/CGI/man-cgiflock+2 flock]. Although some types of locks can be configured to be mandatory, file locks under UNIX are by default advisory . This means that cooperating processes may use locks to coordonate access to a file between themselves, but programs are also free to ignore locks and access the file in any way they choose to.
Two kinds of locks are offered: read locks (or shared locks) and write locks (or exclusive locks). In the case of fcntl, different kinds of locks may be applied to different sections (byte ranges) of a file, or else to the whole file. Read locks can be acquired held by an unlimited number of processes at the same time, but a write lock can only be acquired by one process, and cannot coexist with a read lock. To acquire a read lock, a process must wait until there are no processes holding any write locks. To acquire a write lock, a process must wait until there are no processes holding either kind of lock.
This combination of inode usage and non-mandatory locking leads to great flexibility in accessing files from multiple processes. On the other hand, the cooperative locking approach can lead to problems when a process writes to a file without obeying file locks set by other processes. For this reason, some UNIX and UNIX-like operating systems support mandatory locking as well.
Linux 2.4 and later added notification of external changes to files with dnotify mechanism (through the F_NOTIFY parameter in fcntl). This mechanism is however being replaced by iNotify [http://www-128.ibm.com/developerworks/linux/library/l-inotify.htmlca=dgr-lnxw52Inotify], which were introduced in Linux 2.6.13. Linux also supports mandatory locking through the special [http://www.tin.org/bin/man.cgisection=8&topic=mount mount] -o mand parameter for filesystem mounting, but this is rarely used.
= Lock files =
A system similar to the use of file locking is often used by shell scripts and other programs: 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 only 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.
When using file locks, care must be taken to ensure that operations are Atomic (computer science). When creating the lock, the process must verify that it does not exist and then create it, but without allowing another process the opportunity to create it in the meantime. Various schemes are used to implement this, such as taking advantage of system calls designed for this purpose (but such system calls are not usually available to shell scripts) or by creating the lock file under a temporary name and then attempting to move it into place.|
|