views:

584

answers:

5

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?

A: 

I don't fully understand what you mean by "completion ports". All I can say is that you can use sockets in non-blocking mode, which means that calls return immediately.

This reference is quite old but is comprehensive in a sense that it covers select() etc.: http://rhoden.id.au/doc/sockets2.html

And here is the GNU manual on sockets: http://www.gnu.org/software/libc/manual/html_node/Sockets.html

ypnos
http://msdn.microsoft.com/en-us/library/aa365198%28VS.85%29.aspx
someguy
I just want to know if linux has something similar to winsock's iocp, and if so, I would like some examples of the usage. Also, non-blocking operations do not return "immediately". If there is data to process, it will be handled synchronously.
someguy
@someguy: non-blocking sockets *are* asynchronous. I think you'll have to explain more of what you want in your question if that doesn't do it for you.
Zan Lynx
@Zan Lynx: I mean the actual operation is asynchronous, i.e. even if there is data to process, the call will return immediately and the operation will be executed by another background thread. With non-blocking sockets, the call will return immediately if there is no data to process, but if there is, it will be executed synchronously (i.e. by the calling thread).
someguy
+4  A: 

IOCP is pronounced "asynchronous I/O" on various UNIX platforms:

D.Shawley
Thanks. Any chance you could provide me with some sample codes? I've looked into POSIX AIO before, but I couldn't find anything related to sockets.
someguy
I haven't used POSIX AIO before. I just know that it has been discussed in various forums. Personally, I run BSD-based systems so I have used Kqueue for this. I would look into using Boost or some MPI implementation for concurrent I/O if that is what you are after.
D.Shawley
I've looked into POSIX AIO and Kernel AIO, and both seem to imply that there is no true support for sockets, which is a shame. I'm going to assume that all the documents I've read are outdated, as Java 7 is apparently going to support asynchronous I/O, so linux has to have it, or will have it, right? Maybe I'll try and have a look at what Sun/Oracle have done so far.
someguy
It does look like POSIX AIO has gotten short shrift. You might want to read some of references on _"Fast UNIX Servers"_ (http://dank.qemfd.net/dankwiki/index.php/Fast_UNIX_Servers). There seems to be a lot of good information there.
D.Shawley
A: 

Besides everything cited by @D.Shawley, I recommend libevent.

This may help you with samples. However, it's a comprehensive reference on the libevent API, not on the AIO mechanisms (poll, epoll, kqueue, event ports, select).

jweyrich
+2  A: 

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/

Steve-o
I don't think libevent supports asynchronous I/O, and it even says so on the linked page that it's for non-blocking sockets. I guess I'll look into it deeper later. 0MQ is also interesting, but I don't think it's what I'm looking for. It seems to be a message-passing library (similar to erlang actors?)
someguy
@someguy asynchronous I/O comprehends/includes non-blocking sockets.
jweyrich
+3  A: 

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.

BlackAura
This is the answer I needed. Thanks :)
someguy
Any synchronous I/O will block so long as you're writing more data than the buffer can hold. And if the data you're writing has been paged out, your process will block on the page fault.
Gabe