File lock is a mechanism that limits access to computer files by allowing only one user or process to access them within a certain time.
The system implements locking to prevent the classic interceding update scenario , which is a typical example of race conditions, by applying serialization of the update process to a given file. The following example illustrates the issue of update interceding:
- Process A reads customer records from files containing account information, including customer account balances and phone numbers.
- Process B now reads the same record from the same file so it has its own copy.
- Process A alters the account balance in a copy of the customer record and rewrites the note to the file.
- Process B, which still has a native stale value for account balances in a copy of customer records, updating the account balance, and rewriting customer records to file.
- Process B has now written the value of stale account balance to file, causing changes made by process A to be lost.
Most operating systems support the concept of locking records, which means that individual records in a given file can be locked, thus increasing the number of concurrent updates process. Database maintenance uses file locking, where it can create serial access to all physical files underlying the database. While this prevents other processes from accessing files, it can be more efficient than locking large numbers of areas in a file individually by removing overheads to acquire and release any keys.
The use of a bad file key, like any other computer key, can result in poor performance or in deadlock. File locking can also refer to additional security that is applied by computer users either by using Windows security, NTFS permissions or by installing third-party file locking software.
Video File locking
Di mainframe
IBM pioneered file locking in 1963 for use in mainframe computers using OS/360, where it was called "exclusive control".
Maps File locking
In Microsoft Windows
Microsoft Windows uses three different mechanisms to manage access to shared files:
- uses a shared access control that allows the app to specify access-sharing of files in its entirety for read, write, or delete
- use byte key keys to mediate read and write access to regions within a file
- by the Windows file system that prohibits executable files from opening to write or delete access
Windows inherits the share access control semantics of the MS-DOS system, where sharing is introduced in MS-DOS 3.3. Thus, the application must explicitly allow sharing; otherwise the app has read, write, and delete access exclusively to a file (other access types, such as to retrieve the allowed file attributes.)
For files with shared access, the app can use a lock of the byte range to control access to a specific region of the file. The key of the byte range specifies the file area (offset and length) and key type (shared or exclusive). Note that the file area being locked is not required to have data in the file, and the application sometimes exploits this ability to implement their functionality.
For applications that use file read/write APIs in Windows, byte-range keys are enforced (also referred to as mandatory keys ) by file systems that run in Windows. For apps that use the file mapping API in Windows, the byte keylock is not enforced (also referred to as advisory key. ) The byte-lock lock may also have other side effects on Windows systems. For example, a Windows file sharing mechanism would normally disable client-side caching of a file for all clients when a byte-range key is used on each client to control file access. The client will observe slower access because read and write operations must be sent to the server where the files are stored.
Handling the wrong errors in the application program can cause scenarios where files are locked (either using "share" access or by locking the file byte range) and not accessible by other applications. If so, the user may be able to recover the file access by manually terminating the program that is not working. This is usually done through the Task Manager utility.
The shared mode parameter in the CreateFile function used to open the file specifies file-sharing. Files can be opened to allow file sharing to read, write, or delete access. The subsequent attempt to open the file must be compatible with all previously granted sharing access to the file. When the file is closed, sharing access restrictions are adjusted to remove the restrictions imposed by certain open files.
The lock type of the byte range is determined by the dwFlags parameter in the LockFileEx function used to lock the file area. The Windows LockFile API function can also be used and obtain an exclusive key in the file area.
Any file that executes on a computer system as a program (for example, EXE, COM, DLL, CPL or other binary program file formats) is usually prevented by the file system from being opened to write or delete access, report sharing violations, despite the fact that the program is not opened by any app. However, some access is still allowed. For example, a running app file can be renamed or copied (read) even when executing.
Files are accessed by apps in Windows by using file handling . The handle of this file can be explored with the Process Explorer utility. This utility can also be used to handle forcibly-close without the need to stop the app holding it. This can lead to undefined behavior, because the program will receive unexpected errors when using closed-door handles and can even operate on unexpected files because the number of handles can be recycled.
Microsoft Windows XP and Server 2003 editions have introduced volume snapshot (VSS) capabilities to NTFS, allowing open files to be accessed by the backup software even though there is an exclusive lock. However, unless the software is rewritten to specifically support this feature, the snapshot will be consistent crash only, while properly supported applications can help the operating system create a consistent "transactional snapshot". Other commercial software to access files locked under Windows includes File Access Manager and Open File Manager. This works by installing their own drivers to access files in kernel mode.
In Unix-like systems
Unix-like operating systems (including Linux and Apple MacOS) usually do not automatically lock open files. Several types of file locking mechanisms are available in various Unix types, and many operating systems support more than one type for compatibility. The most common mechanism is fcntl
. The other two mechanisms are flock (2)
and lockf (3)
, which may be separated or may be implemented above fcntl . Although some key types can be configured to be required, the key file under Unix by default is advisory . This means that cooperating processes can use keys to coordinate access to files among themselves, but uncooperative processes are also free to ignore keys and access files in whatever way they choose. In other words, the lock file locks the other file lockers instead of I/O.
Two types of keys are offered: shared keys and exclusive locks. In the case of fcntl
, different types of keys can be applied to different sections (byte ranges) of the file, or else to the entire file. Shared keys can be held by multiple processes at the same time, but exclusive keys can only be owned by one process, and can not coexist with shared keys. To obtain a shared key, a process must wait until no process holds the exclusive lock. To obtain an exclusive key, a process must wait until no process holds both types of keys. Unlike keys created by fcntl
, created by flock
are maintained throughout forks
, making them useful in server forking. It is therefore possible for more than one process to hold an exclusive lock on the same file, provided that this process shares the filial relationship and the exclusive key was originally created in one process before being duplicated in a
fork.
Shared keys are sometimes called "read locks" and exclusive keys are sometimes called "write keys." However, since the key on Unix is ââan advisor, this is not enforced. Thus it is possible for the database to have the concept of "write together" vs. "write exclusive"; for example, changing available fields can be allowed under shared access, while garbage collection and rewriting databases may require exclusive access.
The file lock applies to the actual file, not the file name. This is important because Unix allows many names to refer to the same file. Along with non-mandatory locking, this leads to great flexibility in accessing files from multiple processes. On the other hand, a cooperative locking approach can cause problems when writing to a file without complying with a file key defined by another process.
For this reason, some Unix-like operating systems also offer limited support for mandatory locking . On such systems, files that are setgid bit are active but the group execution bit is off when the opened file is subject to automatic mandatory locking if the underlying file system supports it. However, non-local NFS partitions tend to ignore this bit. If the file is subject to mandatory locking, attempts to read from an area locked with an exclusive lock, or writing to a region locked with a shared or exclusive lock, will block until the key is released. This strategy first came from System V, and can be seen today on Solaris, HP-UX, and Linux operating systems. It's not part of POSIX, however, and BSD-derived operating systems like FreeBSD, OpenBSD, NetBSD, and Apple's MacOS do not support it. Linux also supports mandatory locking through the " mount (8)
-o mand " parameters specifically for the installation of the file system, but this is rarely used.
Some Unix-like operating systems prevent attempts to open executable files from programs that are running to write; this is the third form of locking, separate from that provided by fcntl
and flock
.
Issues
More than one process can save an exclusive flock
in a given file if an exclusive key is duplicated on the newer fork
. It simplifies encoding for network servers and helps prevent race conditions, but can be confusing to the unconscious.
The required keys have no effect on system calls unlink
. As a result, certain programs can, effectively, avoid mandatory locking. The authors of Advanced Programming in the UNIX Environment (Second Edition) observed that the editor ed
did that indeed (page 456).
Either and how the flock keys
works on a network filesystem, such as NFS, depending on the implementation. In the BSD system, flock
calls the open file descriptors for files on the NFS-installed partition to no avail. On Linux before 2.6.12, flock
calling the NFS file will only act locally. Kernel 2.6.12 and above implements flock
calls the NFS file using a POSIX byte key key. This key will be visible to other NFS clients implementing fcntl
-style POSIX keys , but not visible to those who do not.
Upgrade and downgrade keys release old key before applying a new key. If an application shrinks an exclusive key to a shared key when another app is blocked waiting for an exclusive lock, the latter app may get an exclusive lock and lock the first app. This means that locking keys can be blocked, which may be contrary to intuition.
All fcntl
the key associated with the file for the given process is deleted when the each file descriptor for the file is closed by the process, even if the key never asked to descriptor the file. Also, the fcntl
key is not inherited by the child process. Closed semantics fcntl
is very inconvenient for apps that call library subroutines that can access files. None of these "bugs" occurred using the original flock
key.
Preservation of key states on open file descriptors is forwarded to other processes using a Unix domain socket depending on the implementation.
AFS and buffered I/O issues
Andrew File System (AFS) presents an interesting case where file locking fails. If the AFS file can be accessed from several different machines simultaneously, then the keys obtained on one machine are unknown to other machines. Therefore, two (or more) users on different machines can lock the same file for exclusive use, and each believes their read/write operation is being performed by only their machine, when in fact, both can write to the same section of the same file. Therefore, "flock" or "fcntl" fails to lock exclusively across machines. The key works on every single machine, therefore the only practical way to handle exclusive write access simultaneously is to have all users log in to the same machine. This can be controlled by requiring an access program or script that checks that the user is on a particular host.
Another source of key failure occurs when the I/O buffer has a buffer assigned in the user's local workspace, rather than in the operating system buffer pool. "Fread" and "fwrite" are usually used to buffer I/O, and after part of a file is read, another attempt to read the same part will, most likely, obtain data from a local buffer. The problem is other users attached to the same file have their own local buffers, and the same thing happens to them. "fwrite" data obtained from a buffer with "Fread" will NOT get data from the file itself, and some other users can change it. Both can use "flock" for exclusive access, which prevents simultaneous writing, but because reading reads from a buffer and not the file itself, any data changed by user # 1 can be lost by user # 2 (overwritten). The best solution to this problem is to use Unbuffered I/O ("read" and "write") with "flock", which also means using "lseek" instead of "fseek" and "ftell". Of course, you should make adjustments to the function parameters and returned results. In general, I/O buffers are unsafe when used with shared files.
In Amiga OS
In Amiga OS, keys in the file (or directory) can be obtained by using the Lock
function (in dos.library
). Keys can be shared (other processes can read files/directories, but can not modify or delete them), or exclusively so that only processes that successfully acquire keys can access or modify objects. The keys are all over the object and not part of it. The key must be released with the UnLock
functionality: Unlike in Unix, the operating system does not implicitly unlock the object when the process ends.
File key
Shell scripts and other programs often use a strategy similar to the use of file locking: making key files , which are files that are irrelevant (although often people will find the process identifiers of the document holder). locking files) and whose primary purpose is to signal them in the presence that some resources are locked. Key files are often the best approach if controlled resources are not plain files at all, so using methods to lock files does not apply. For example, a lock file might set access to a set of related resources, such as several different files, directories, bunch of disk partitions, or access selected to higher-level protocols such as servers or database connections.
When using key files, care must be taken to ensure that the operation is atomic. To get the key, the process must verify that the key file does not exist and then create it, while preventing other processes from creating it for awhile. Various methods to do this include:
- Use the
lockfile
command (the conditional semaphore file creator distributed in theprocmail
package). - The system call creates the file, but fails if the file already exists. (System calls are available from languages ââlike C or C, and shell scripts can use noclobber)
- Using the
mkdir
command and checking the exit code for failure
File keys are often named with tildes ( ~
) beginning with the file name being locked. If they lock resources other than files, they can be named more arbitrarily.
Certain Mozilla products (such as Firefox, Thunderbird, Sunbird) use this resource key resource type (using a temporary file named "parent.lock".)
Software unlocker
Unlocker is a utility used to determine what processes lock files, and displays a list of processes and choices about what to do with the process (kill task, unlock, etc.) along with a list of file options like delete or rename. On some Unix-like systems, utilities like fstat and lockf can be used to check the status of a file lock by a process, by file name, or both.
On a Windows system, if the file is locked, you can schedule the removal or deletion to take place on the next reboot. This approach is typically used by the installer to replace locked system files.
Version control system
In locking the file version control system is used to prevent two users from changing the same file version in parallel and then when saving, the second user to overwrite what the user first changed. This is implemented by marking the locked file as read-only in the file system. A user who wants to change a file performs an unlock operation (also called checkout), and until the check-in operation (shop) is completed, or the key is returned, no one else is allowed to unlock the file.
See also
- Reader-author key
References
External links
- Everything you never wanted to know about locking files, reviewing Unix file lock options and their issues (dated December 13, 2010)
- Private POSIX key files, LWN.net articles on supported file locks in Linux that behave differently than POSIX keys related to inheritance and behavior in close
Source of the article : Wikipedia