8

我开始研究一种我不知道的新范例,称为 Linux 中的异步 IO。

我的目标是使用面向套接字的异步 IO 来编写高性能高效的服务器。原因是我的应用程序受 IO 限制。

在搜索更多信息时,我遇到了以下 2 个介绍。

  1. Posix AIO

  2. Linux AIO 接口

在异步框架中,我想避免的情况是为我需要异步处理的每个通知创建一个新线程,因为它会杀死我的应用程序。

我的问题如下:

  1. 这两个框架的幕后解决了这个问题吗?

  2. 如果是的话,你会建议考虑套接字吗?

问候

AFG

4

2 回答 2

8

None of these are really intended for sockets.

The POSIX AIO interface creates threads that use normal blocking IO. They work with the buffer cache and should in principle even work with sockets (though I've admittedly not tried).

The Linux kernel AIO interface does not create threads to handle requests. It works exclusively in "no buffering" mode. Beware of non-obvious behaviour such as blocking when submitting requests in some situations, which you can neither foresee nor prevent (nor know about other than your program acting "weird").

What you want is nonblocking sockets (a nonblocking socket is "kind of asynchronous") and epoll to reduce the overhead of readiness notification to a minimum, and -- if you can figure out the almost non-existing documentation -- splice and vmsplice to reduce the IO overhead. Using splice/vmsplice you can directly DMA from disk to a kernel buffer and push to the network stack from there. Or, you can directly move pages from your application's address space to kernel, and push to the network.
The downside is that the documentation is sparse (to say the least) and especially with TCP, some questions remain unaddressed, e.g. when it is safe to reclaim memory.

于 2012-07-08T19:58:59.657 回答
1

For purpose of network programming, you will want event based I/O, implemented (in most basic form) with select(2) call, and on some systems with poll(), epoll(), kpoll() etc.

POSIX AIO (of which Linux AIO is an implementation), doesn't necessarily work on sockets (It did at some point in Linux 2.5 codebase, but I can't be certain it's still possible).

High-performant socket I/O on Unix is instead done in event-based manner, where you process incoming events in a loop. An event can be "socket is ready for write", "socket has new data" etc., then react on them.

于 2012-07-08T19:58:44.463 回答