The Linux Programming Interface

(nextflipdebug5) #1
Alternative I/O Models 1363

printf("Ready: %d\n", ready);

/* Deal with returned list of events */

u for (j = 0; j < ready; j++) {
printf(" fd=%d; events: %s%s%s\n", evlist[j].data.fd,
(evlist[j].events & EPOLLIN)? "EPOLLIN " : "",
(evlist[j].events & EPOLLHUP)? "EPOLLHUP " : "",
(evlist[j].events & EPOLLERR)? "EPOLLERR " : "");

i if (evlist[j].events & EPOLLIN) {
s = read(evlist[j].data.fd, buf, MAX_BUF);
if (s == -1)
errExit("read");
printf(" read %d bytes: %.*s\n", s, s, buf);

o } else if (evlist[j].events & (EPOLLHUP | EPOLLERR)) {

/* If EPOLLIN and EPOLLHUP were both set, then there might
be more than MAX_BUF bytes to read. Therefore, we close
the file descriptor only if EPOLLIN was not set.
We'll read further bytes after the next epoll_wait(). */

printf(" closing fd %d\n", evlist[j].data.fd);
a if (close(evlist[j].data.fd) == -1)
errExit("close");
numOpenFds--;
}
}
}

printf("All file descriptors closed; bye\n");
exit(EXIT_SUCCESS);
}
––––––––––––––––––––––––––––––––––––––––––––––––––––––– altio/epoll_input.c

63.4.4 A Closer Look at epoll Semantics


We now look at some subtleties of the interaction of open files, file descriptors, and
epoll. For the purposes of this discussion, it is worth reviewing Figure 5-2 (page 95),
which shows the relationship between file descriptors, open file descriptions, and
the system-wide file i-node table.
When we create an epoll instance using epoll_create(), the kernel creates a new
in-memory i-node and open file description, and allocates a new file descriptor in
the calling process that refers to the open file description. The interest list for an
epoll instance is associated with the open file description, not with the epoll file
descriptor. This has the following consequences:

z If we duplicate an epoll file descriptor using dup() (or similar), then the dupli-
cated descriptor refers to the same epoll interest and ready lists as the original
descriptor. We may modify the interest list by specifying either file descriptor
Free download pdf