Search This Blog

Sunday, May 29, 2005

Asynchronous I/O - Introduction

I just felt like taking a break from discussing code that I've already written (which is everything on the blog so far) and discuss some code I'm currently writing. In addition to threading, one of the features I most wanted to support in LibQ was asynchronous I/O. The reason for this is that, like threading, asynchronous I/O is fiercely platform-dependant (even more than threads, actually); as well, not all platforms support asynchronous I/O at all (Windows 9x comes readily to mind - it supports asynchronous I/O for sockets, but not for files).

Synchronous I/O is the most common kind of file I/O. With synchronous I/O functions like fread, fwrite, etc., the function waits for the kernel to perform the I/O, and does not return until the I/O is completed (or failed). Asynchronous I/O is just the opposite - the I/O functions return immediately, before the I/O has been performed. The I/O will then be performed at some later time, while the application continues executing.

Asynchronous I/O is extremely useful when significant amounts of slow I/O are being performed, as it allows the application to continue using the CPU while the I/O is performed in parallel. Depending on how much the kernel needs the CPU to communicate with the device, the savings from asynchronous I/O can vary greatly. In the worst case, you may only gain a bit of speed by allowing the CPU and bus be used in parallel; in the best case, the increases in speed can be hundreds of percent, if multiple independent devices are accessed simultaneously.

No comments: