Speaking of which, we have to talk about the difference between synchronous I/O and asynchronous i/o. In some cases, synchronization and asynchrony, blocking and non-blocking are easily mixed up. In fact, they are completely different, and the objects they modify are also different. Blocking and non-blocking refer to whether the process needs to wait if the data accessed by the process is not ready. Simply speaking, this is equivalent to the implementation difference within the function, that is, whether to return directly or wait for ready when not ready; Synchronization and asynchrony refer to the mechanism of accessing data. Synchronization generally refers to the way of actively requesting and waiting for the completion of I/O operations. When data is ready, it must be blocked when reading and writing. Asynchronous refers to the way of actively requesting data and then continuing to process other tasks, and then waiting for the notification of the completion of I/O operations. This can prevent the process from blocking when reading and writing data.
POSIX1003.1 standard defines a set of library functions for accessing files asynchronously. Asynchronous I/O(AIO) here actually means that when a user-state process calls a library function to access files, it makes necessary fast registration, such as entering a read-write operation queue, and then the function returns immediately. At this time, the real I/O transmission has not yet started.
It can be seen that this mechanism is asynchronous I/O in the true sense and non-blocking. It can make the process continue to run after I/O operation is initiated, so that CPU processing and I/O operation can achieve better overlap.
POSIX’s standard library defines a series of AIO interfaces, which shield almost all details of network communication, so it is very simple for users. AIO does not provide a non-blocking open () method, so the process still uses an open () system call to open the file, then populates some data structures that describe I/O requests, and then calls aio_read () or aio_write () to initiate asynchronous I/O operations. Once the request enters the operation queue, the function returns, and the process can check the status of the running I/O operations through aio_error ().
However, for the implementation of AIO, different platforms have different methods. It can even be completely implemented by library functions without kernel support, such as simulating non-blocking aio_read () calls through multithreading. However, as a result, its performance is greatly reduced and becomes meaningless, so in fact many platforms have not implemented it.
In Linux 2.6.16, the implementation of AIO can be seen in/usr /usr/include/libaio.h it adopts a set of interfaces that do not follow POSIX AIO standard, and the implementation method is based on LinuxThreads kernel-level thread library. up to now, this function is still being implemented. currently, Linux AIO can only be used for files opened through O_DIRECT flag.