Home > Article > Backend Development > Detailed explanation of PHP file read and write locks
Problems related to reading and writing will always exist. File lock is designed to solve this problem. In fact, it is a simple semaphore. Read and write dependency refers to the random conflict of file data caused by reading and writing files at the same time. In order to know clearly when and what operations were used to change or read the data in the file, it is necessary to serialize, atomicize, and synchronize the operations so that users can know exactly what data is in the file at what time. . File Lock is one of these tools.
File systems generally have two types of locks, shared locks and exclusive locks, which can also be called read locks and write locks.
Characteristics of file system locks:
A file can only have one lock when it is opened, which means that more than two locks cannot be assigned to a file at the same time.
Users who read and write files that have been locked can hold this lock, that is, users who hold this lock can perform corresponding operations on the file, such as reading or writing. The user can apply to hold a file lock. If the file is initially unlocked, the system creates a lock for the file before applying to hold the lock, and then the applicant holds it.
Rules for holding locks: If this file already has a read (shared) lock, other users cannot assign exclusive locks or read-only locks to the file, but they can hold the lock, that is to say Other users can read the file, but as long as the file is locked, no user can write to it. If the file already has an exclusive lock and is held by a user, no user can hold the lock again unless the holder unlocks it.
There is an important concept to remember: the operation of the file itself has nothing to do with the lock. Regardless of whether the file is locked or not, the user can perform any normal operation on the file at will, but the operation The system will check the lock and give different treatments to different situations. For example, in a lock-free situation, anyone can read and write any file at the same time. Of course, it is very likely that the content read and written will be wrong - note that only the content is wrong, and the operation will not be wrong. After locking, certain operations will be denied under certain circumstances. The role of file locks is not to protect the files and data themselves, but to ensure the synchronization of the data. Therefore, file locks are only truly effective for the user who holds the lock, and only if all users use the same and identical way to use the file. The file lock can be effective for all users only if the lock limit operates on the file. Otherwise, as long as there is an exception, the entire file lock function will be destroyed. For example, if everyone follows the steps of opening a file, locking, reading and writing, unlocking, and closing the file, there will be no problems with everyone's operations, because based on the allocation and holding principles of file locks, the data in the file The update exists as an atomic operation and is indivisible, so it is also synchronous and safe. But if a person does not take this step, then he will have problems when reading and writing, either he cannot read accurately or he cannot write, etc.
Based on the above principles, it is worth talking about whether the read data is locked. Generally speaking, exclusive locking is the only operation when writing data. It ensures that the data written to the file is correct. When the file is locked, other users cannot obtain the lock and therefore have no right to do any operations. When reading, it depends on the specific situation. In most cases, if there is no need for particularly precise or sensitive data, there is no need to lock, because locking takes time and resources. It does not take time for one person to apply for and hold the lock. There are more problems. The most important thing is that if the file needs to be updated, if it is locked on a read-only lock, writing will not be possible because those users who want to write will not get the exclusive lock. If at the same time If there are too many people applying to hold the read-only lock, the exclusive lock may never be applied for. This means that the file may not be written for a long time, which appears to be very slow. Generally speaking, the opportunity to write files is relatively rare and more important, so exclusive locking is mainly done. Read-only locks are not necessary in most cases. So where are read-only locks used? Read-only locks are actually only useful to the user itself. Read-only locks ensure that the data read by the user is the real data read from the file, rather than dirty data called "dirty". In fact, this is still aimed at misoperations on files by other users who do not use locks. If the file is locked, other users do not necessarily have to use the lock to read and write the file. If they read and write directly, they will not be able to access the locked file. The operation may not be valid. The user holding the read lock can be sure that when he reads the data, what he reads is from the real file, not the data that has been overwritten at the same time.
Therefore, it should be a matter of course to apply an exclusive lock when writing to ensure that the data will not go wrong at this time. If you do not apply for a shared lock, the data read may be incorrect, but it will not have any impact on the file itself. The impact is only on the user. The data read after applying for a shared lock must be the real data in the file when it is read. If If it is not to ensure the accuracy of the data, the shared lock does not need to be added. At best, it will be read again. If you read it for writing, it is better to add an exclusive lock directly. There is no need to use a shared lock.
Another point to emphasize is that file lock is only effective for users who use it and use it according to the rules. Otherwise, you use yours and I use mine. Some use it and some don’t. , things will still get messed up, and errors will still occur. For the same file, only if everyone uses the same rules to use file locks, can we ensure that each user will not have read or write errors when sharing the file.
The above is the detailed content of Detailed explanation of PHP file read and write locks. For more information, please follow other related articles on the PHP Chinese website!