Reputation: 8515
I have two file descriptors, one read file descriptor on pipe and another socket connection descriptor. Neither of them is non blocking. Both of them are added to epoll context with single EPOLLIN event. Lastly, i call epoll_wait with timeout = -1. Below is the sample code. I have two questions :-
Do pipe and connection descriptors need to be non blocking. This is not edge triggered. If yes, is it good practice or mandatory and if mandatory, why ?
I am setting timeout to -1, yet epoll_wait returns immediately with the return value 0. Why is that happening? with timeout of -1, epoll_wait should only return if there is an event.
int total_fd_ready = -1;
struct epoll_event pipe_event, connection_event, total_events[64];
pipe_event.data.fd = pipe_fd[0];
piple_event.events = EPOLLIN;
connection_event.data.fd = conn_fd;
connection_event.events = EPOLLIN;
total_fd_ready = Epoll_wait(epoll_fd, total_events, 64, -1);
printf("%d\n", total_fd_ready);
Epoll_wait is defined as
int Epoll_wait(int e_fd, struct epoll_event *events, int max_events, int timeout)
{
#ifdef DBG
printf("Epoll_wait called on epoll_fd: %d with max_events: %d and timeout: %d\n", e_fd, max_events, timeout);
#endif
int result = -1;
if(result = (epoll_wait(e_fd, events, max_events, timeout)) < 0)
if(errno != EINTR)
err_sys("epoll_wait error with epoll fd: %d and timeout : %d\n", e_fd, timeout);
#ifdef DBG
else
printf("epoll_wait was interrupted\n");
#endif
return result;
}
UPDATE: found the issue, though i cannot explain why result is set to 0. i needed bracers in the following if statement
if((result = (epoll_wait(e_fd, events, max_events, timeout))) < 0)
Upvotes: 0
Views: 3267
Reputation: 409442
The answer is that the comparison operator <
has higher precedence than assignment, which means that result
gets assigned the result of the expression (epoll_wait(e_fd, events, max_events, timeout)) < 0
.
Upvotes: 4