When using epoll_wait
, it seems to "eat" everything that's written to stdout
and delay the print until after epoll_wait
has received an event, even though I tried to print before calling on anything related to epoll (it could even be at the beginning of my main method, it still won't get printed).
An example of prints that will not show until after epoll_wait
has received an event:
printf("This doesn't get printed. ");
fprintf(stdout, "This doesn't get printed either.");
ev.events = EPOLLIN;
ev.data.fd = some_sock_fd; // Same with STDIN_FILENO
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, some_sock_fd, &ev) == -1) {
perror("epoll_ctl");
exit(EXIT_FAILURE);
}
for (;;) {
rc = epoll_wait(epoll_fd, &ev, 1, -1);
// This is where it gets printed
Writing to stderr
works as normal, but how can I write to stdout
? How do I prevent epoll_wait
from blocking prints to stdout
?
The issue seems to be unrelated to epoll_wait
. Here's a summary of the offending code:
// Since there's no newline, the following stays in the buffer
printf("Some print without newline.");
for (;;) {
// At this point, the buffer has not been flushed,
// and epoll_wait blocks the output
rc = epoll_wait(epoll_fd, &ev, 1, -1);
Using fflush(stdout)
is the solution to this code, since the buffering has nothing to do with epoll_wait, but with how the user-space buffers stdout:
// Since there's no newline, the following stays in the buffer
printf("Some print without newline.");
// Forces a write of user-space buffered data for stdout
fflush(stdout);
for (;;) {
// At this point, the buffer has not been flushed,
// and epoll_wait blocks the output
rc = epoll_wait(epoll_fd, &ev, 1, -1);
To sum it up, this seems to be a case of looking in the wrong place for an issue that should have been obvious.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.