Address: http://num7.iteye.com/blog/710280
Read/write problems always exist. File locks are designed to solve this problem. In fact, they are simple semaphores. Read-write Relevance refers to the random conflict between file data caused by simultaneous reading and writing of files. To determine when to modify or read the data in the file, it is necessary to serialize, atomize, and synchronize the operation so that you can know when the file contains data. File lock is one of the tools.
File systems generally have two types of locks: Shared locks and exclusive locks. They can also be called read locks and write locks.
File System lock features:
A file can only have one lock when it is opened. That is to say, at the same time, two or more locks cannot be assigned to a file.
Users who read or write a locked file can hold the lock, that is, users who hold the 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 starts to be unlocked, the system creates a lock for the file before applying to hold the lock, and then the applicant holds it.
Lock hold rules: if the file already has a read (SHARE) Lock, other users cannot assign an exclusive lock or read-only lock to the file, but can hold this lock, that is to say, other users can read the file, but as long as the file is locked, no user can write it. If the file already has an exclusive lock held by a user, no user can hold the lock again unless the holder is unlocked.
there is an important concept to remember: operations on a file itself have nothing to do with the lock, no matter whether the file is locked or not, users can perform any operations on files under normal circumstances, but the operating system checks the lock and performs different operations for different situations. For example, in the case of no lock, anyone can perform any read/write operations on a file at the same time. Of course, it is very likely that the read/write content will be wrong-note that only the content is wrong and the operation will not go wrong. After the lock is applied, some operations are rejected in some cases. Filelock does not protect files and data, but ensures data synchronization. Therefore, filelock is truly effective only for users who hold locks, only when all users use the same method to operate on files using the filelock restriction can the filelock be valid for all users. Otherwise, if there is one exception, the entire file lock function will be damaged. For example, if all users follow the steps of file opening, locking, reading/writing, unlocking, and closing, all operations will not be affected because of the principles of file lock allocation and holding, data updates in files exist as atomic operations and cannot be divided. Therefore, they are synchronized and secure. However, if a person does not take this step, he may encounter problems during reading and writing, either inaccurate reading or writing.
Based on the above principles, whether the read data is locked is worth mentioning. Generally, the exclusive lock 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, so they do not have the right to perform any operation. During reading, it depends on the actual situation. In most cases, if you do not need special precise or sensitive data, you do not need to lock it because it takes time and resources to lock it, it takes no time for a person to apply for a lock. If the file needs to be updated, if the file is read-only, it cannot be written, because those users who want to write data will not be able to get the exclusive lock. If too many people apply to hold the read-only lock at the same time, the exclusive lock may not be applied for until now, this means that the file may not be written for a long time, and it looks very slow. Generally, there are fewer and more important opportunities to write files. Therefore, the primary priority is to set the exclusive lock. Read-Only locks are not necessary in most cases. So where is the read-only lock used? The read-only lock is only useful to the user. the read-only lock ensures that the data read by the user is the real data actually read from the file, rather than the dirty data called "dirty. In fact, this is intended for the misoperation of other users who do not need to lock the file. If the file is locked, other users do not have to read and write the file through the lock. If the file is read and written directly, the operation on the locked file is not necessarily effective. The user holding the read lock can certainly read the data from the real file, instead of the data that has been overwritten at the same time.
Therefore, the exclusive lock should be well-handled during writing to ensure that data will not go wrong at this time. If you do not apply for a shared lock, the data you read may be incorrect, but it has no impact on the file itself. The impact is only for the user, the data read after applying for a shared lock must be the real data in the file at the time of reading. If it is not to ensure data accuracy, the shared lock can be reread, if you read it to write data, you should apply the exclusive lock directly, and there is no need to use the shared lock.
Another point should be emphasized: the file lock is only valid for users who use it, and it is only valid for users who use it according to rules. Otherwise, you use yours, I use mine, some use, some do not need to be used, they will still be garbled, and errors will still occur. For the same file, only users who use the same rule use File locks, in order to ensure that each user shares the file, there will be no read/write errors.
The above are some of my knowledge's principles and may be incorrect. If you need to correct them, please advise me more. (Http://bbs.chinaunix.net/viewthread.php? Tid = 255335)
Address: http://num7.iteye.com/blog/710280