0

Excuse my long post. I would post code so that it is easier to understand the problem I am facing. It seems that if a signaled socket is added to epoll instance, epoll_wait on the epoll instance will not block. The following example has let me believe this:

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h> 
#include <sys/epoll.h>

int MAX_EVENT_COUNT = 10;

int main(int argc, char *argv[])
{

int epollfd = epoll_create( MAX_EVENT_COUNT );
if ( epollfd == -1 )
{
printf("\n Error : epoll_create \n");
return 1;
}

epoll_event ev;
memset(&ev,0,sizeof(ev));
ev.events = EPOLLIN | EPOLLET;

int pipefd[2];
if (pipe(pipefd) == -1) 
{
perror("pipe");
exit(EXIT_FAILURE);
}

char ch = 'a';
write(pipefd[1], &ch, 1);

if (epoll_ctl( epollfd , EPOLL_CTL_ADD, pipefd[0], &ev ) == -1 )
{
printf("\n Error : epoll add result \n");
return 1;
}

epoll_event rawResult [MAX_EVENT_COUNT];
int32_t res = epoll_wait( epollfd, rawResult, MAX_EVENT_COUNT, -1 );

if(res!=1)
{
printf("\n Epoll problem \n");
}
else
{
printf("\n OK \n");
}

}

However the following example is pretty much the same but this time I add a signalled socket to the epoll, this time the epoll_wait call blocks

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h> 
#include <sys/epoll.h>

int MAX_EVENT_COUNT = 10;

int main(int argc, char *argv[])
{
int listenfd = 0;
{
struct sockaddr_in serv_addr; 

char sendBuff[1025];

listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff)); 

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000); 

bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); 

listen(listenfd, 10); 
}


int sockfd = 0, n = 0;
char recvBuff[1024];
struct sockaddr_in serv_addr; 

memset(recvBuff, '0',sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket \n");
return 1;
} 

memset(&serv_addr, '0', sizeof(serv_addr)); 

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000); 

if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
{
printf("\n inet_pton error occured\n");
return 1;
    } 

if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\n Error : Connect Failed \n");
return 1;
} 

int connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); 
int epollfd = epoll_create( MAX_EVENT_COUNT );
if ( epollfd == -1 )
{
printf("\n Error : epoll_create \n");
return 1;
}

epoll_event ev;
memset(&ev,0,sizeof(ev));
ev.events = EPOLLIN | EPOLLET;

if (epoll_ctl( epollfd , EPOLL_CTL_ADD, sockfd, &ev ) == -1 )
{
printf("\n Error : epoll add result \n");
return 1;
}

epoll_event rawResult [MAX_EVENT_COUNT];
int32_t res = epoll_wait( epollfd, rawResult, MAX_EVENT_COUNT, -1 );

}
Yordan Pavlov
  • 1,303
  • 2
  • 13
  • 26

1 Answers1

2

epoll_wait() will block if the file descriptors it is waiting on have no events for it to report about. In your case, I don't believe you have reached epoll_wait() yet. Assuming that your program has progressed passed connect() and accept(), then you have not written any data on the connection (on the sockfd), so epoll_wait() will not detect any events on the connfd.

This is different from your first program, where you write a byte of data on the pipefd[1] before calling epoll_wait() on pipefd[0].

jxh
  • 69,070
  • 8
  • 110
  • 193
  • I understand what you mean but it still does not seem logical to me, since epoll is primitive "below" the tcp layer so it should report the three way handshake as a data. Furthermore some people seem to be using it that way ( http://stackoverflow.com/questions/2875002/non-blocking-tcp-connect-with-epoll ) – Yordan Pavlov Jun 23 '13 at 14:04
  • @YordanPavlov: You are not performing asynchronous `accept()`. If you want tcp `SYN` to trigger an `epoll_wait()` wakeup, you need to be polling on the listening socket, not the socket returned by `accept()`. After `accept()` returns the socket, the 3-way handshake (or at least 2 of the 3 ways) has already completed. – jxh Jun 23 '13 at 19:19
  • Thanks for your reply, you seem to refer to the server side. In my case I am polling on the client socket and I am expecting the SYN-ACK to trigger an epoll_wait(). – Yordan Pavlov Jun 24 '13 at 10:56
  • @YordanPavlov: It won't You are not performing an asynchronous `connect()` either. Please read the [`connect()` man page](http://linux.die.net/man/2/connect), and pay particular attention to the `EINPROGRESS` `errno` value. – jxh Jun 24 '13 at 16:06