简体   繁体   English

Linux 和 I/O 完成端口?

[英]Linux and I/O completion ports?

Using winsock, you can configure sockets or seperate I/O operations to "overlap".使用 winsock,您可以将套接字或单独的 I/O 操作配置为“重叠”。 This means that calls to perform I/O are returned immediately, while the actual operations are completed asynchronously by separate worker threads.这意味着执行 I/O 的调用会立即返回,而实际操作则由单独的工作线程异步完成。

Winsock also provides "completion ports". Winsock 还提供“完成端口”。 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, ie if all its I/O operations are completed .如果句柄不在 I/O 操作的中间,即如果它的所有 I/O 操作都已完成,则它可以被多路分解。

So, on to my question... does linux support completion ports or even asynchronous I/O for sockets?那么,关于我的问题...... linux 是否支持套接字的完成端口甚至异步 I/O?

If you're looking for something exactly like IOCP, you won't find it, because it doesn't exist.如果您正在寻找与 IOCP 完全相同的东西,您将找不到它,因为它不存在。

Windows uses a notify on completion model (hence I/O Completion Ports). Windows 使用完成模型通知(因此是 I/O完成端口)。 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. Linux 应用程序(和大多数其他类 Unix 应用程序)通常使用就绪模型通知。 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.然后,您执行不会阻塞的 I/O 操作。

With this model, you don't need asynchronous I/O.使用此模型,您不需要异步 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.这个编程模型有点棘手,这就是为什么有像 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.在 Windows 中也有一个关于就绪模型的通知(select 或 WSAWaitForMultipleEvents),您之前可能已经看过了。 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.不要让这让您失望 - Windows 和 Linux 是完全不同的操作系统。 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.这种方法实际上在 Linux 上非常有效,其性能可与 Windows 上的 IOCP 相媲美。

IOCP is pronounced "asynchronous I/O" on various UNIX platforms: IOCP 在各种 UNIX 平台上读作“异步 I/O”:

Use boost::asio.使用 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.我知道这不能完全回答您的问题,但这是我能给出的最佳建议。

So, on to my question... does linux support completion ports or even asynchronous I/O for sockets?那么,关于我的问题...... linux 是否支持套接字的完成端口甚至异步 I/O?

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 devices io_uring support appeared in the 5.1 kernel).关于套接字,在 5.3 和更高版本的内核中, Linux 有一些类似于io_uring形状的完成端口(对于文件/块设备io_uring支持出现在 5.1 内核中)。

Linux kernel does provide the "I/O block completion" concept, everytime you use the "blk_complete_request" API for example. 每次使用“blk_complete_request”API时,Linux内核都会提供“I / O块完成”概念。 Another example: 另一个例子:

http://lxr.free-electrons.com/source/kernel/sched/completion.c http://lxr.free-electrons.com/source/kernel/sched/completion.c

And as explained here: 如下所述:

http://www.ibm.com/developerworks/library/l-async/ http://www.ibm.com/developerworks/library/l-async/

linux does have both synchronous and asynchronous I/O block completion API. linux确实有同步和异步I / O块完成API。

The above are all at the kernel level. 以上都是内核级别的。 At the userspace level there is the "io_submit()" API: 在用户空间级别有“io_submit()”API:

http://www.fsl.cs.sunysb.edu/~vass/linux-aio.txt http://www.fsl.cs.sunysb.edu/~vass/linux-aio.txt

which detailed the complete set of io_*() APIs. 其中详细介绍了完整的io _ *()API。

Partially similar question: 部分相似的问题:

Is there really no asynchronous block I/O on Linux? Linux上真的没有异步块I / O吗?

Boost ASIO implements Windows style IOCP (Proactor design pattern) on Linux using epoll (Reactor pattern). Boost ASIO 在 Linux 上使用 epoll(反应器模式)实现了 Windows 风格的 IOCP(前摄器设计模式)。 See http://think-async.com/Asio/asio-1.5.3/doc/asio/overview/core/async.htmlhttp://think-async.com/Asio/asio-1.5.3/doc/asio/overview/core/async.html

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,阅读google关于libevent的博文,可以在Unix上使用异步IO实现IOCP语义,但不能直接使用IOCP实现异步IO语义,

http://google-opensource.blogspot.com/2010/01/libevent-20x-like-libevent-14x-only.html 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,有关具有 BSD 套接字 API 的跨平台异步 IO 示例,请查看最近在 LWN.net 上发布的 ZeroMQ,

http://www.zeromq.org/ http://www.zeromq.org/

LWN article, LWN文章,

http://lwn.net/Articles/370307/ http://lwn.net/Articles/370307/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM