使用winsock,您可以将套接字或单独的I/O 操作配置为“重叠”。这意味着执行 I/O 的调用会立即返回,而实际操作则由单独的工作线程异步完成。
Winsock 还提供“完成端口”。据我了解,完成端口充当句柄(套接字)的多路复用器。如果句柄不在 I/O 操作中间,即如果其所有 I/O 操作都已完成,则可以对句柄进行多路分解。
那么,关于我的问题...Linux 是否支持完成端口甚至套接字的异步 I/O?
Using winsock, you can configure sockets or seperate I/O operations to "overlap". This means that calls to perform I/O are returned immediately, while the actual operations are completed asynchronously by separate worker threads.
Winsock also provides "completion ports". From what I understand, a completion port acts as a multiplexer of handles (sockets). A handle can be demultiplexed if it isn't in the middle of an I/O operation, i.e. if all its I/O operations are completed.
So, on to my question... does linux support completion ports or even asynchronous I/O for sockets?
发布评论
评论(7)
如果您正在寻找与 IOCP 完全相同的东西,您将找不到它,因为它不存在。
Windows 使用完成通知模型(因此称为 I/O 完成端口)。您异步启动某些操作,并在该操作完成时收到通知。
Linux 应用程序(以及大多数其他 Unix 类似应用程序)通常使用就绪通知模型。您会收到一条通知,表明可以在不阻塞的情况下读取或写入套接字。然后,您进行 I/O 操作,这不会阻塞。
使用此模型,您不需要异步 I/O。数据立即被复制到套接字缓冲区中/从套接字缓冲区中复制出来。
这种编程模型有点棘手,这就是为什么有像 libevent 这样的抽象库。它提供了更简单的编程模型,并抽象了支持的操作系统之间的实现差异。
Windows 中还有一个关于就绪模型的通知(select 或 WSAWaitForMultipleEvents),您之前可能已经看过。它无法扩展到大量套接字,因此不适合高性能网络应用程序。
不要因此而失望 - Windows 和 Linux 是完全不同的操作系统。在一个系统上无法很好扩展的东西可能在另一个系统上工作得很好。这种方法实际上在 Linux 上效果很好,性能与 Windows 上的 IOCP 相当。
If you're looking for something exactly like IOCP, you won't find it, because it doesn't exist.
Windows uses a notify on completion model (hence I/O Completion Ports). You start some operation asynchronously, and receive a notification when that operation has completed.
Linux applications (and most other Unix-alikes) generally use a notify on ready model. You receive a notification that the socket can be read from or written to without blocking. Then, you do the I/O operation, which will not block.
With this model, you don't need asynchronous I/O. The data is immediately copied into / out of the socket buffer.
The programming model for this is kind of tricky, which is why there are abstraction libraries like libevent. It provides a simpler programming model, and abstracts away the implementation differences between the supported operating systems.
There is a notify on ready model in Windows as well (select or WSAWaitForMultipleEvents), which you may have looked at before. It can't scale to large numbers of sockets, so it's not suitable for high-performance network applications.
Don't let that put you off - Windows and Linux are completely different operating systems. Something that doesn't scale well on one system may work very well on the other. This approach actually works very well on Linux, with performance comparable to IOCP on Windows.
IOCP 在各种 UNIX 平台上发音为“异步 I/O”:
IOCP is pronounced "asynchronous I/O" on various UNIX platforms:
使用 boost::asio。把手放下。它具有温和的学习曲线,但它是跨平台的,并且会自动使用适合您正在编译的系统的最佳可用方法。根本没有理由不这样做。
我知道这并不能完全回答你的问题,但这是我能给出的最好建议。
Use boost::asio. Hands down. It has a mild learning curve, but it's cross-platform, and automatically uses the best available method for the system you're compiling on. There's simply no reason not to.
I know that this isn't quite an answer to your question, but it's the best advice I could give.
关于套接字,在 5.3 及更高版本的内核中,Linux 有类似于
io_uring
形式的完成端口的东西 (对于文件/块设备io_uring
支持出现在 5.1 内核中)。With regard to sockets, in 5.3 and later kernels, Linux has something analogous to completion ports in the shape of
io_uring
(for files/block devicesio_uring
support appeared in the 5.1 kernel).可能有点晚了,但这个答案是为了完整性。
Linux 自 2020 年 (v5.11) 起引入了
io_uring
这在语义上类似于 Windows 上的完成端口,但具有非常不同的 API。在这种情况下,您可以使用 struct io_uring_sqe 来对诸如read()
或write()
之类的操作进行排队,并通过 struct io_uring_cqe< 获取通知/code> 完成后。它还严重依赖于 struct iovec,因此建议熟悉该结构和相关 API。有一个名为
liburing
的小型 Linux 专用库,它可以访问内核 API 更容易。Might be a bit late, but this answer is for the sake of completeness.
Linux, as of 2020 (v5.11), has introduced
io_uring
which is semantically similar to completion port on Windows but with very different APIs. In that, you queue an operation such asread()
orwrite()
usingstruct io_uring_sqe
and get notifications viastruct io_uring_cqe
when it's done.It also relies heavily on
struct iovec
, so familiarity with that structure and associated APIs is recommended. There is a tiny Linux-only library calledliburing
which makes access to the kernel APIs a lot easier.阅读 Google 关于
libevent
的博客文章,可以在 Unix 上使用异步 IO 实现 IOCP 语义,但不能直接使用 IOCP 实现异步 IO 语义,http://google-opensource.blogspot.com/2010/01/libevent-20x-like- libevent-14x-only.html
有关使用 BSD 套接字 API 的跨平台异步 IO 示例,请查看最近在 LWN.net 上发布的 ZeroMQ,
http://www.zeromq.org/
LWN 文章,
http ://lwn.net/Articles/370307/
Read the blog entry from Google on
libevent
, you can implement IOCP semantics on Unix using asynchronous IO but cannot directly implement asynchronous IO semantics using IOCP,http://google-opensource.blogspot.com/2010/01/libevent-20x-like-libevent-14x-only.html
For an example cross platform asynchronous IO with a BSD socket API look at ZeroMQ as recently published on LWN.net,
http://www.zeromq.org/
LWN article,
http://lwn.net/Articles/370307/
Boost ASIO 使用 epoll(Reactor 模式)在 Linux 上实现 Windows 风格的 IOCP(Proactor 设计模式)。请参阅 http://think-async。 com/Asio/asio-1.5.3/doc/asio/overview/core/async.html
Boost ASIO implements Windows style IOCP (Proactor design pattern) on Linux using epoll (Reactor pattern). See http://think-async.com/Asio/asio-1.5.3/doc/asio/overview/core/async.html