- Order:
- Duration: 3:49
- Published: 09 Aug 2009
- Uploaded: 10 Jul 2011
- Author: FirstStudiosP
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 customer's phone number 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, so increasing the number of concurrent update processes.
Database maintenance makes use of file locking, whereby it can serialize access to the entire physical file underlying a database. While this prevents any other process from accessing the file, it can actually 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.
# using share access controls that allow applications to specify whole-file access sharing for read, write or delete # using byte-range locks to arbitrate read and write access to regions within a single file # by Windows file systems disallowing executing files from being opened for write or delete access
Windows inherits the semantics of share-access controls from the MS-DOS system (where sharing was introduced in MS-DOS 3.3) Thus, an application must explicitly allow sharing - otherwise an application has exclusive read, write and delete access to the file (other types of access, such as those to retrieve the attributes of a file are allowed.)
For a file with shared access, applications may then use byte-range locking to control access to specific regions of the file. Such byte range locks specify a region of the file (offset and length) and the type of lock (shared or exclusive). Note that the region of the file being locked is not required to have data within the file and applications sometimes exploit this ability to implement their functionality.
For applications that use the file read/write APIs in Windows, byte-range locks are enforced (also referred to as mandatory locks) by the file systems that execute within Windows. For applications that use the file mapping APIs in Windows, byte range locks are not enforced (also referred to as advisory locks.) Byte range locking may also have other side-effects on the Windows system. For example, the Windows file sharing mechanism will typically disable client side caching of a file for all clients when byte range locks are used on any client to control access of the file. The client will observe slower access to the file because all read and write operations must be sent to the file server where the file is stored.
Improper error-handling in an application program can lead to a situation where a file is locked (either using share access or with byte range file locking) and cannot be accessed by other applications. In this case the user may be able to restore access to the file by terminating the malfunctioning program manually. This is typically done through the Task Manager utility.
The sharing mode parameter in the CreateFile function used to open files determines file sharing. Files can be opened to allow sharing the file for read, write or delete access. Subsequent attempts to open the file must be compatible with all previously granted sharing access to the file. When the file is closed, the sharing access restrictions are adjusted to remove the restrictions imposed by that specific file open.
Byte-range locking type is determined by the dwFlags parameter in the LockFileEx function used to lock a region of a file. The Windows API function LockFile can also be used and acquires an exclusive lock on the region of the file.
Any file that is executing on the computer system as a program (e.g., an EXE, COM, DLL, CPL or other binary program file format) is normally prevented by the file system from being opened for write or delete access, reporting a sharing violation, despite the fact that the program is not opened by any application. However, some access is still allowed. For example, a running application file can be renamed or copied (read) even when executing.
Files are accessed by applications in Windows by using . These file handles can be explored with the Process Explorer utility. This utility can also be used to force-close handles without needing to terminate the application holding them.
Microsoft Windows XP and Server 2003 editions have introduced volume snapshot (VSS) capability to NTFS, allowing open files to be accessed by backup software despite any exclusive locks. However, unless software is rewritten to specifically support this feature, the snapshot will be crash consistent only, while properly supported applications can assist the operating system in creating "transactionally consistent" snapshots. Other windows commercial software for accessing locked files include , and Open File Manager. It operates by installing its own drivers to access the files in kernel mode.
Two kinds of locks are offered: shared locks and 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. Shared locks can be acquired by an unlimited number of processes at the same time, but an exclusive lock can only be acquired by one process, and cannot coexist with a shared lock. To acquire a shared lock, a process must wait until there are no processes holding any exclusive locks. To acquire an exclusive lock, a process must wait until there are no processes holding either kind of lock.
Shared locks are sometimes called "read locks" and exclusive locks are sometimes called "write locks". However, because locks on UNIX are advisory, this isn't enforced. Thus it is possible for a database to have a concept of "shared writes" vs. "exclusive writes"; for example, changing a field in place may be permitted under shared access, whereas garbage-collecting and rewriting the database may require exclusive access.
File locks apply to the actual file, rather than the file name. This is important since UNIX allows multiple names to refer to the same file. Together with non-mandatory locking, this leads to great flexibility in accessing files from multiple or many 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.
Mandatory locks have no effect on the unlink function. As a result, certain programs may, effectively, circumvent mandatory locking. The authors of Advanced Programming in the UNIX Environment (Second Edition) observed that the ed editor did so (page 456).
Whether flock locks work on network filesystems, such as NFS, is implementation-dependent. On BSD systems flock calls are successful no-ops. On Linux prior to 2.6.12 flock calls on NFS files would only act locally. Kernel 2.6.12 and above implement flock calls on NFS files using POSIX byte range locks. These locks will be visible to other NFS clients that implement fcntl()/POSIX locks.
Lock upgrades and downgrades release the old lock before applying the new lock. If an application downgrades an exclusive lock to a shared lock while another application is blocked waiting for an exclusive lock, the latter application will get the exclusive lock and lock the first application out.
All fcntl locks associated with a file for a given process are removed when any file descriptor for that file is closed by that process, even if a lock was never requested for that file descriptor. Also, fcntl locks are not inherited by a child process. The fcntl close semantics are particularly troublesome for applications that call subroutine libraries that may access files.
When using lock files, care must be taken to ensure that operations are atomic. In order 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. There are various methods to accomplish this including:
Certain Mozilla products (such as Firefox, Thunderbird, Sunbird) use this type of file resource lock mechanism (using a temporary file named "parent.lock".)
This text is licensed under the Creative Commons CC-BY-SA License. This text was originally published on Wikipedia and was developed by the Wikipedia community.