File locking
≈ 6 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
File locking is a mechanism that restricts access to a computer file, or to a region of a file, by allowing only one user or process to modify or delete it at a specific time, and preventing reading of the file while it's being modified or deleted. Systems implement locking to prevent an interceding update scenario, which is an example of a race condition, by enforcing the serialization of update processes to any given file. 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. 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.…
File locking is a mechanism that restricts access to a computer file, or to a region of a file, by allowing only one user or process to modify or delete it at a specific time, and preventing reading of the file while it's being modified or deleted. Systems implement locking to prevent an interceding update scenario, which is an example of a race condition, by enforcing the serialization of update processes to any given file. 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. 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 many 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. File locking 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. === Problems === More than one process can hold an exclusive flock on a given file if the exclusive lock was duplicated across a later fork . This simplifies coding for network servers and helps prevent race conditions, but can be confusing to the unaware. Mandatory locks have no effect on unlink . Consequently, certain programs may, effectively, circumvent mandatory locking. Stevens & Rago (2005) observed that the ed editor indeed did that. Whether and how flock locks work on network filesystems, such as NFS, is implementation dependent. On BSD systems, flock calls on a file descriptor open to a file on an NFS-mounted partition are successful no-ops. On Linux prior to 2.6.12, flock calls on NFS files would act only 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 -style POSIX locks, but invisible to those that do not. 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 may get the exclusive lock and lock the first application out. This means that lock downgrades can block, which may be counter-intuitive. 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. Neither of these "bugs" occurs using real flock -style locks. Preservation of the lock status on open file descriptors passed to another process using a Unix domain socket is implementation dependent. use std::fs::File; fn main() -> std::io::Result { let f = File::create("foo.txt")?; f.lock()?; Ok(()) } .NET supports file locking using a . using System.IO; var textLength = 1; var byteCount = 1; using var stream = new FileStream("foo.txt", FileMode.OpenOrCreate); stream.Lock(textLength - 1, byteCount); PHP supports file locking using the function. $fp = fopen("/tmp/lock.txt", "r+"); if (flock($fp, LOCK_EX)) { // acquire an exclusive lock ftruncate($fp, 0); // truncate file fwrite($fp, "Write something here\n"); fflush($fp); // flush output before releasing the lock flock($fp, LOCK_UN); // release the lock } else { echo "Couldn't get the lock!"; } fclose($fp); Everything you never wanted to know about file locking, a review of the Unix file locking options and their problems (dated December 13, 2010) File-private POSIX locks, a LWN.net article on file locks supported on Linux that behave differently from POSIX locks regarding inheritance and behavior on close
Text derived from https://en.wikipedia.org/w/index.php?oldid=1362915290 under CC BY-SA 4.0; modifications have been made.