I am having trouble understanding this from the epoll documentation: Say I have pipe(fd)
so we have fd[0]
for reading and fd[1]
for writing. We write some stuff to fd[1]
and then close it:
write(fd[1], "abc", 3);
write(fd[1], "def", 3);
close(fd[1]);
Potentially simultaneously, we create an epoll instance, tell it to wait until it can read from fd[0]
, and read 3 bytes once it does.
int epoll_fd = epoll_create(10);
struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.fd = fd[0];
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd[0], &ev);
struct epoll_event event;
epoll_wait(epoll_fd, &event, 1, -1);
char buffer[100];
read(fd[0], buffer, 3);
Now we call epoll_wait
again. Even though pipe is closed, the kernel should still have 3 bytes buffered for us available to be read from fd[0]
, yes? So I'd expect the following to work (ie, not block):
epoll_wait(epoll_fd, &event, 1, -1);
read(fd[0], buffer, 3); // buffer should start with "def" now
Now there's nothing left in the pipe and the write end is closed. What should happen if I epoll_wait
a third time?
epoll_wait(epoll_fd, &event, 1, -1); // will this block indefinitely?
read(fd[0], buffer, 3);
I have two different pieces of code both seeming to indicate different answers here, so I'd like to know what should be happening to better decipher what's going on.