Skip to content

Disk Buffering

April 17, 2012

I took a little time out from writing an editor to implement a fairly simple method of buffering disk access. Obviously this is desirable as direct access to the hard disk is much slower than memory access.

Previously, whenever a sector was required the OS just loaded it from the disk. Now the sector is put into a buffer which is managed by a binary tree. When the OS requires a sector it first checks this tree to see if the sector is already in it. If so it returns a pointer to the existing buffer; otherwise it loads the sector and adds it to the tree.

In the current implementation the buffer is never freed. That’s not such a problem as the disk is small, it has very little data on it, and the system never runs for very long anyway. In due course I will implement flushing of old buffers from the tree. Not only is this necessary for memory reasons, but at some stage the tree will become so large that it is less efficient than reading direct from the disk. I’ll probably use a “least recently used” algorithm.

When the buffer is written to it is marked as dirty. At crucial points the OS checks for dirty buffers and writes them to disk. All of this code is to be found in filesystem.c (plus btree.c, which contains routines to manage the binary tree).

Work on the editor progresses. It’s doing quite well now but still has a few bugs.

Whilst working on these two points a few bugs have arisen, which have now been corrected. (One of these involves a slight change to the format of executable files in “tasks”.) The download, and the source listings, now contain all of these changes.

From → IanOS

  1. Frew Cen permalink

    Won’t this method waste the memory? In one moment system works with lot of sectors (despite the freeing, lets say on computer with RAM memory much lower than CPU speed) and the sectors are be swapped in to disk because of no memory. When they’re swapped, it’s same as the weren’t ever buffered.

    • Indeed, this is very wasteful of memory. It is only the very initial attempt at disk buffering. In reality buffering needs to be more intelligent, recognising which sectors are used frequently and caching only them. Directories are an obvious candidate, as are the sectors containing inodes (the in ode and block bitmaps are separately cached). All in good time! This is a good enough approach for an initial attempt, but certainly requires a lot more work,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: