Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

EPoll issue

161 views
Skip to first unread message

Andreas Roth

unread,
Mar 14, 2004, 10:57:51 AM3/14/04
to
Hi,

i wrote a little program an found a strange effect in the epoll
implementation. The following program simple tests these epoll api. It
create a socket and a epoll instance. If epoll_wait waits for the socket
and only EPOLLOUT is requested, epoll_wait returns and have the EPOLLHUP
flag set in the returned event structure.
I think if i request only the EPOLLOUT event epoll_wait only give me all
EPOLLOUT event and NOT the EPOLLHUP events.

My system:
SuSE Linux 9.0
Kernel 2.6.4 (self compiled)

Does anybody have the same issue?

Thanks in advance,
A. Roth


#include <stdio.h>
#include <sys/epoll.h>
#include <sys/socket.h>

int main(int argc,char ** argv)
{
int sock;
int epfd;
int i;
int res;
struct epoll_event events[8];
struct epoll_event ev;

sock = socket(AF_INET,SOCK_STREAM,0);
printf("Socket descriptor %i\n",sock);

epfd = epoll_create(8);
printf("Epoll descriptor %i\n",epfd);

ev.data.u32 = 1;
ev.events = EPOLLOUT;
res = epoll_ctl(epfd,EPOLL_CTL_ADD,sock,&ev);
printf("epoll_ctl returned %i\n",res);

for(i = 0; i < 5; i++)
{
res = epoll_wait(epfd,events,8,-1);
printf("epoll_wait returned %i\n",res);
for(;res>0;res--)
{
printf("event %X id %i\n",events[res-1].events,events[res-1].data.u32);
}

}

close(epfd);
close(sock);
}

Michael Kerrisk

unread,
Mar 15, 2004, 3:00:17 AM3/15/04
to
On Sun, 14 Mar 2004 16:57:51 +0100, Andreas Roth
<ar...@arsoft-online.com> wrote:

>Hi,
>
>i wrote a little program an found a strange effect in the epoll
>implementation. The following program simple tests these epoll api. It
>create a socket and a epoll instance. If epoll_wait waits for the socket
>and only EPOLLOUT is requested, epoll_wait returns and have the EPOLLHUP
>flag set in the returned event structure.
>I think if i request only the EPOLLOUT event epoll_wait only give me all
>EPOLLOUT event and NOT the EPOLLHUP events.
>
>My system:
>SuSE Linux 9.0
>Kernel 2.6.4 (self compiled)
>
>Does anybody have the same issue?

This is expected behaviour. Precisely the same occurs with poll().
EPOLLHUP and EPOLLERR are only ever set in the returned events mask --
you don't set them in the epoll_ctl() call(s). EPOLLHUP means the
other end of a communication was closed. In this particular case, it
is returned for an unconnected stream socket.

Cheers,

Michael

Andreas Roth

unread,
Mar 15, 2004, 10:40:20 AM3/15/04
to

If this is the 'by design' behaviour it's ok. But how can i wait on a
socket until the socket has connected? If i can epoll_wait i always
returns EPOLLHUP until the socket is connected (so i get a busy loop).
Is there any possiblity to wait for a connect without epoll?

Andreas

Frederik Hertzum

unread,
Mar 15, 2004, 11:00:18 AM3/15/04
to
Have you considered having the socket reading/write procedures running in a
thread out side the rest of the programme?

David Schwartz

unread,
Mar 15, 2004, 12:08:20 PM3/15/04
to

"Andreas Roth" <ar...@arsoft-online.com> wrote in message
news:pan.2004.03.15....@arsoft-online.com...

> If this is the 'by design' behaviour it's ok. But how can i wait on a
> socket until the socket has connected? If i can epoll_wait i always
> returns EPOLLHUP until the socket is connected (so i get a busy loop).
> Is there any possiblity to wait for a connect without epoll?


To wait until a socket has connected, wait only for writability.

DS


Michael Kerrisk

unread,
Mar 17, 2004, 2:33:07 AM3/17/04
to
On Mon, 15 Mar 2004 16:40:20 +0100, Andreas Roth
<ar...@arsoft-online.com> wrote:

This is a somewhat different question than you origianlly asked...
Note the following:

If you do a normal connect(), then it blocks until the connection has
been completed. There's thus no need for poll()/epoll()/select().

If you make the socket non-blocking before you do the connect() (you
don't say this, but I suspect it's the scenario you are thinking of),
then

a) the three-way handshake (assuming TCP) will be initiated.

b) connect() will return immediately with the error EINPROGRESS (may
vary by platform).

c) a later select()/poll()/epoll() with a non-zero timeout will block
until the connection completes, and then indicate the socket as being
writable (writefds / POLLOUT / EPOLLOUT).

Cheers,

Michael

0 new messages