About the file read and write, is too complex, too much detail, so can only analyze his principle, the details involved too much.
This article describes the read operation of the file.
Let's talk about the outline of the read operation. The read operation reads the data from the disk into the kernel buffer, the data is organized into a radix_tree form in the kernel buffer, and then the contents of the buffer are copied to the user buffer, and the user can manipulate the data in its own buffer.
The specific point is:
For each process open file, have a file object descriptor, from the file object descriptor can find the index node, from the index node and can find Adress_space, this object is the file in memory cache. Therefore, each process that opens this file will operate on this Adress_space object.
For each given file, the index and the length to be read are checked.
The user will first look for the page it needs from the cache, and if this page is found, check that the page is not up to date. Therefore, this page is mutually exclusive, but this can happen before:
This page may be a page that another process reads from a file, but does not have an IO operation on this page. So he thinks the page data is invalid, need to read the page from disk, but before reading, that process may also delete this page, so at this time need to check the page is not in memory, if not, request a physical page box, and marked as pg_locked, indicating lock, cannot remove this page from memory, Because you want to do an IO operation on this page. You can now start populating this page from disk read content.
Reading this page is not everything, because the above mentioned may exist another process in the operation of this file, and the modification of its contents are written back to the disk, so it is possible to delete the probability of the file, so at this time check if the index + length is found, the page is not valid, to free this page.
Finally, the page is copied to the user-state buffer.
For read operations in Linux