• Please review our updated Terms and Rules here

How do DOS buffers work?

alank2

Veteran Member
Joined
Aug 3, 2016
Messages
2,265
Location
USA
I've been looking at some DOS function calls and I noticed that they don't mention any buffering. Like the DOS 40h write command. If you write 10 bytes to it, does it go into a buffer and not make it to disk?

How do you make DOS flush pending writes?

I saw some different things, like 3.3 and above has a function to do this, but that before that one might have had to close the file and reopen it to make sure the writes were written. Is that right?
 
BUFFERS in CONFIG.SYS defines the number of 512-byte buffers to be used as a read cache. When DOS loads data, it does so in two ways:

  • If the user-provided destination is paragraph-aligned and a multiple of 512, data is loaded from the storage device directly to the destination buffer.
  • If the user-provided destination is not paragraph-aligned and a multiple of 512, it is loaded into one or more BUFFERS, and then copied to the destination via REP MOVS.
BUFFER data is not immediately flushed. So if you read 1K from a file handle associated with a non-removable device (ie. hard disk), then later read that same data again from the same file handle, the second request is serviced from the BUFFER area if it is still in there (ie. no other disk reads have occurred between requests). Depending on the use case, this can be a noticeable speedup.

It is not coincidence that a BUFFER is 512 bytes; this is to maintain parity with sector sizes. The optimal BUFFERS number is at least the number of 512-byte sectors in a track, so that an entire track read request can go into BUFFERS without needing subsequent calls. For example, on my system that uses an ST-251, that drive has 17 sectors per track, so I have BUFFERS=17 in my CONFIG.SYS. I have not personally noticed a huge speedup going beyond this optimal number, but there is a measurable slowdown if I go under this number.

It has been a few years since I looked at DOS internals in depth, so if I've made a mistake, I welcome any corrections.
 
DOS buffers are the size of a sector plus a header (16 or 20 bytes depending on the DOS version). Since a sector is almost always 512 bytes this is the size of the buffer but it can be larger (e.g. 1024 bytes). If BUFFERS=HIGH (DOS 5+) is used then there is an additional transfer buffer used to guard against systems which can't do DMA into the HMA (floppy drives use DMA for transfer).
 
Back
Top