Buffer Cache

Buffer Cache


Now that we’ve seen how file systems work, we turn to several optimizations that help make it fast, since most storage devices tend to be slow. We’ll first discuss some software optimizations and then turn to hardware. If you’re first reaction is, let’s use a cache, then you’re on the right track. Indeed most operating systems do use free portions of main memory as a cache for the much slower mass storage device. Statistics vary on this sort of thing over time as technologies changes, but by most measurements, memory is as much as 100,000 times faster than disk for random access. Would use the portion of memory used as a cache for disc, the unified buffer cache, a name it earned for some obscure historical reasons. The terminology here can be a little confusing. So I’m going to emphasize that we’re talking about in memory caching of the contents of the disc, not the RAM on the disc controller that the device controller here might use. When data is read from disc, is stored in this unified buffer cache so that subsequent reads can find it there. Instead of having to bother the disk again, because disk access is often sequential. It is common also to read ahead in a file, loading up subsequent blocks to main memory so that is there when the application needs it. Writing to disk is usually done with the write back policy. The change is made only in the unified buffer cache at first. And the page is marked as dirty. The slower operation of writing the data to the disk is postponed until some more opportune time. New files can also be created and stored in the unified buffer cache. If a file has a very short life span, writing to the disk might not be necessary at all. The existence of the end memory cache. Means that a call to write isn’t a guarantee that the data is changed on the disk and will persist. The changes are only reflected in memory. The advantage is that the program gets to resume faster and get on with its work. The downside is that if the system crashes before the write has been made to disk then changes will be lost. If you really do want to make sure that changes are reflected on disk, need to call fsync or msync to flush out the unified buffer cache. The write-back policy of the buffer cache is also the reason that operating systems will warn you not to move storage devices without ejecting it first. It needs a chance to flush the buffer cache and any dirty pages so that changes will be reflected on the device.