Re: Q: Buffered vs Cached vs mmap()ed

Malcolm Beattie (mbeattie@sable.ox.ac.uk)
Tue, 1 Jun 1999 17:04:34 +0100 (BST)


Martijn van Oosterhout writes:
> Let me get this straight...
>
> Buffered is when a program writes to a file and
> it is buffered.

Er, yes, sort of. If you mean the sort of buffering stdio does then
yes. That's different from the "buffer cache" you mention below.

> Cached is for when a program reads from a file.
>
> Is that right?

Yes, that's one sort of caching.

> Also, if one process has a file mmap()ed and another
> process is writing to it, is the other process directly
> writing to the same pages? Or are they kept in sync
> some other way?
>
> Basically, when you mmap() a file, are the actual pages
> in the buffer cache be mapped into your address space?

Those are two different questions. Linux has two separate caches:
a "page cache" indexed by (inode, page-aligned-offset) and a
"buffer cache" indexed by (block-device, block-number). The page
cache is used for file data; the buffer cache for metadata. When you
mmap() a file, the actual pages are mapped from the page cache (not
buffer cache) into your address space When you read() from a file
(on an ordinary filesystem such as ext2 that uses generic_file_read
and bmap), the data is read directly into the page cache. That
means everything is nicely in sync with only one copy of data.

File writes (your first question) are uglier at the moment. Each
filesystem has its own write() hander. Typically (for ext2 for
example), it uses the buffer cache (via getblk()) to get hold of
memory buffers containing current copies of the blocks to be
written (completely separate from copies which may or may not be
in the page cache). Then it writes to those buffers. Then it calls
update_vm_cache() to find the corresponding page cache pages and
copies the data into there too. This ugliness for file writing is
due to be cleaned up during 2.3 development, I believe/hope.

--Malcolm

-- 
Malcolm Beattie <mbeattie@sable.ox.ac.uk>
Unix Systems Programmer
Oxford University Computing Services

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/