Reputation: 7334
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?
Upvotes: 49
Views: 31306
Reputation: 1209
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 as read()
or write()
using struct io_uring_sqe
and get notifications via struct 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 called liburing
which makes access to the kernel APIs a lot easier.
Upvotes: 1
Reputation: 7144
So, on to my question... does linux support completion ports or even asynchronous I/O for sockets?
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).
Upvotes: 8
Reputation: 59553
IOCP is pronounced "asynchronous I/O" on various UNIX platforms:
Upvotes: 24
Reputation: 11
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
Upvotes: 0
Reputation: 20445
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.
Upvotes: 10
Reputation: 3208
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.
Upvotes: 93
Reputation: 12866
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,
LWN article,
http://lwn.net/Articles/370307/
Upvotes: 0