How do file systems handle simultaneous read / write?

User A asks the system to read the file foo , and at the same time, user B wants to save his data in one file. How is this situation handled at the file system level?

+48
filesystems locking
May 01, '10 at 22:15
source share
2 answers

Most file systems (but not all) use locks to protect simultaneous access to a single file. The lock can be exclusive, so the first user gets access to the lock - subsequent users receive a "denied access" error. In your example script, user A will be able to read the file and obtain a file lock, but user B will not be able to write while user A is reading.

Some file systems (such as NTFS) allow you to specify a lock level to allow, for example, simultaneous readers, but no writers. Byte locks are also possible.

Unlike databases, file systems are usually not transactional, not atomic, and changes from different users are not isolated (if changes can even be seen, blocking can prevent this.)

Using whole file locks is a rough approach, but it will protect against inconsistent updates. Not all file systems support locking of entire files, so it is common practice to use a lock file β€” a typically empty file whose presence indicates that its associated file is being used. (Creating a file is an atomic operation on most file systems.)

+35
May 01 '10 at 22:22
source share

For Linux, the short answer is: you can get some strange information from a file if there is a parallel author. The kernel uses internal locking to start each read () and write () operation in turn. (Although, I forget if the entire file is locked or if it depends on each page). But if the application uses several write () calls to write information to a file, reading () may occur between any calls, so it could see inconsistent data. This is a violation of integrity in the operating system.

As mentioned in mdma, you can use file locking to make sure that there is only one reader and one writer at a time. NTFS seems to use mandatory locking, where if one program locks a file, all other programs receive error messages when they try to access it.

Unix programs usually do not use locking at all, and when they do, locking is usually advisory. Check lock prevents other processes from receiving advisory lock in one file; this does not actually interfere with reading or writing. (That is, it only locks the file for those who check the lock.)

+25
May 01 '10 at
source share



All Articles