Detailed php file read and write lock

Source: Internet
Author: User
Read-Write related problems are always there, file lock is to solve this problem and do, in fact, it is a simple signal volume. Read-write correlation refers to the random conflict of file data caused by both reading and writing files. In order to know exactly when to change or read the data in a file, it is necessary to serialize, atomically, and synchronize the operation so that the user can be sure of what data is in the file. A file lock is one of the tools.

File systems generally have two types of locks, shared and exclusive locks, and can also be called Read and write locks.

Features of file system lock:

A file can only have a lock when it is opened, which means that at the same time, you cannot assign more than two locks to a file simultaneously.

The user who reads and writes the locked file can hold the lock, that is, the user who holds the lock can perform the appropriate operation on the file, such as reading or writing. The user can request to hold a file lock, and if the file starts unlocked, the system creates a lock for the file before it is requested, and then the applicant holds it.

Rules for holding locks: If the file already has a read (shared) lock, the other user cannot assign an exclusive lock or a read-only lock to the file, but can hold the lock, that is, the other user 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 already held for 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 and the lock does not have anything to do, regardless of whether the file is locked, the user can arbitrarily to the file under normal circumstances of any operation, but the operating system will check the lock, for different situations to give different processing. For example, in the case of no lock, anyone can read and write to a file at the same time, of course, it is likely that the content will be read and write errors-note that only the content error, the operation is not error. After locking, some operations are rejected in some cases. The role of the file lock is not to protect the file and the data itself, but to ensure the synchronization of the data, so that the file lock only for the user who holds the lock is really effective, and only all users use the same way to take advantage of file lock restrictions on the file operation, file lock for all users, otherwise, as long as there is an exception , the functionality of the entire file lock will be compromised. For example, all people follow the open file, lock, read and write, unlock, close the steps of the file, all the person operation will not have problems, because the file lock based on the allocation and holding principle, the data in the file update is as an atomic operation exists, is inseparable, it is also synchronous, secure. But if someone does not take this step, then he will have problems in reading and writing, not reading is not allowed to write and so on.

Based on the above principles, it is worth saying whether the reading data is locked. In general, write data when the exclusive lock is the only operation, it is guaranteed to write to the data in the file is correct, when the file is locked, other users can not get the lock, and therefore not authorized to do any action. In the case of reading, depending on the situation, in most cases, if you do not need to be particularly accurate or sensitive data, without locking, because the lock takes time and resources, a person to apply for holding the lock can not take time, more people have problems, the most important thing is, if the file needs to be updated, if it is read-only lock, The write cannot be made, because those who want to write will not get an exclusive lock, if you also apply for a read-only lock too many people, the exclusive lock may have been unable to apply, the performance is that the file may not be written for a long time, it appears very slow. Generally speaking, the chances of writing a file are relatively small and more important, so it is mostly good to lock out a lock, and read-only locks are not necessary in most cases. So where does a read-only lock work? Read-only locks are actually useful only to the user itself, and the read-only lock ensures that the data that the user reads is real data that is actually read from the file, not dirty data called "dirty". In fact, this is for those who do not have to lock the other users of the file misoperation, if the file is locked, other users do not necessarily have to read and write through the lock file, if he is directly read and write, the lock on the file operation is not necessarily effective, A user who holds a read lock is sure to read the data when he reads it from a real file, rather than the data that has been overwritten at the same time.

Therefore, the lock should be justified at the time of writing, which guarantees that the data will not go wrong. If you do not apply for a shared lock, there may be errors in the reading of the data, but the file itself has no effect, the impact is only for the user, after the application of the shared lock read the data must be read at the time of the real data in the file, if not to ensure the accuracy of the data, the shared lock can not add, at best If you read it in order to write, it is better to directly add the lock, there is no need to use a shared lock.

There is a point to emphasize is: File lock only for users who use it, and is the rule to use its users to be effective, otherwise, you use your, I use my, some use, some do not, or will be confused, error or will appear, on the same file, only everyone with the same rule with a file lock, To ensure that each user does not have read and write errors when sharing the file.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.