- File locking
File locking is a mechanism that enforces access to a
computer fileby only one user or process 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:
# 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
serializationof 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.
One use of file locking is in database maintenance where it can
serializeaccess 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 achieving and releasing each lock.
Poor use of file locks, like any computer lock, can result in poor performance or
File locking in Microsoft Windows
Shared file access in Windows is managed by three distinct mechanisms:
#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; and
#By Windows file systems disallowing executing files from being opened for write or delete access.
The semantics of share access controls in Windows are inherited from the original
MS-DOSsystem (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.
File sharing is determined by the "sharing mode" parameter in the [http://msdn2.microsoft.com/en-us/library/aa363858.aspx CreateFile] function used to open files. 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 [http://msdn2.microsoft.com/en-us/library/aa365203.aspx LockFileEx] function used to lock a region of a file. The
Windows APIfunction [http://msdn2.microsoft.com/en-us/library/aa365202.aspx 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 "file handles". These file handles can be explored with the
Process Explorerutility. This utility can also be used to force-close handles without needing to terminate the application holding them. Microsoft Windows XPand Server 2003 editions have introduced volume snapshot capability to NTFS, allowing open files to be accessed by backup softwaredespite 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 File Access Manager, and Open File Manager. It operates by installing its own drivers to access the files in kernel mode.
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 ]
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 the first application will be locked 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 which call subroutine libraries that may access files.
Linux 2.4 and later added notification of external changes to files with
dnotifymechanism (through the "F_NOTIFY" parameter in fcntl). This mechanism is however being replaced by inotify, which was introduced in Linux 2.6.13. Linux also supports "mandatory locking" through the special " "-o mand" parameter for filesystem mounting, but this is rarely used.
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 identifierof 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 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. Various methods are used to achieve this, such as taking advantage of a system call for this purpose (such system calls are usually unavailable to shell scripts) or by creating the lock file under a temporary name and then attempting to move it into place.
Mozillaproducts (such as Firefox) use this type of file resource lock mechanism (such as the parent.lockfile.)
Wikimedia Foundation. 2010.
Look at other dictionaries:
File Locking — [dt. »Dateisperre«], in einem Netzwerk ein Verfahren zum Dateischutz, durch das eine Datei während der Bearbeitung durch einen Netzteilnehmer für Zugriffe anderer Benutzer gesperrt ist. Auf diese Weise lässt sich vermeiden, dass zwei Anwender… … Universal-Lexikon
file locking — UK US noun [uncountable] computing a feature of software that prevents information in a file from being changed by two different users at the same time Thesaurus: parts of computer programs, systems or softwaremeronym computer programs and… … Useful english dictionary
file locking — See file and record locking … Dictionary of networking
file locking — Method of data management which reserves a file for the first user that requests it, thus locking out other users … IT glossary of terms, acronyms and abbreviations
file locking — prevention of access to a file by more than one application, blocking of a file … English contemporary dictionary
file locking — UK / US noun [uncountable] computing a feature of software that prevents information in a file from being changed by two different users at the same time … English dictionary
File Access Manager — is a software program for accessing exclusively opened and locked files in windows. File Access Manager is owned and operated by VisionWorks Solutions.ee also*File locking *Volume Shadow Copy Service *Open File Manager External links *… … Wikipedia
File system — For library and office filing systems, see Library classification. Further information: Filing cabinet A file system (or filesystem) is a means to organize data expected to be retained after a program terminates by providing procedures to store,… … Wikipedia
file and record locking — A method of controlling file access in a multiuser environment, where there is always a possibility that two users will attempt to update the same file at the same time but with different information. The first user to access the file locks… … Dictionary of networking
file sharing — The sharing of files over a network or between several applications running on the same workstation. Shared files can be read, reviewed, and updated by more than one individual. Access to the file or files is often regulated by password… … Dictionary of networking