Advanced I/O

Linux provides some advanced I/O system calls. These have advantages like:
– Allow single call to read from or write data to many buffers at once
– Memory mapped I/O ie maps a file into memory which allows file I/O via simple memory manipulations
– Provide hints to kernel on intension of use of a file, which can be used to improve performance
– Asynchrnous I/O: issue I/O requests without waitinbg for them to complete.

Opening files

O_DIRECT
The kernel implements complex layer of caching, buffering and there is a I/O management that coordinates between the application and devices. Sometimes high performance application would like to bypass this layer of complexity and perform its own I/O management. Usually the implementation by the OS is very efficient, but sometimes applications like database systems would like this.
So while calling open() we should pass the flag O_DIRECT which minimize the presence of I/O management. The requests are directly from the user-space to device. All I/O will be synchronous ie operations are not returned unless complete.

O_SYNC
Unlike read(), the write() are normally not synchronized. We dont know when the data is actually written to the disk. The O_SYNC makes the calls to write() perform synchronized I/O.
The O_SYNC results in higher time spent on write operations. It is better to use this as the last resort. If the applications requires write guarentee, fsync() could be used where ever required.

Synchronized, Synchronous, and Asynchronous Operations

Synchronous and asynchronous refer to whether I/O operations wait for some event (eg. storage of the data) before returning. The term synchronized and nonsynchronized specifies what event must occur (eg writing the data to the disk).

The asynchronous operations may not actually take place when requested, but only be queued for later. This is usefull when the action needs lot of time, so that process can do other tasks and will get signalled when the action is completed.

src : Linux System Programming by Robert Love

Asynchronous I/O

The aio library supports asynchronous I/O. It helps to initiate multiple transfer at the same time. Each transfer has it its own notification method when it completes.

Some of the APIs :
– aio_read : to request an asynchronous read operation
– aio_error : to check the status of an asynchronous request eg whether its completed or cancelled
– aio_return : returns the number of byte transfered

Design a site like this with WordPress.com
Get started