简体   繁体   English

epoll_wait()阻止打印到标准输出

[英]epoll_wait() blocks prints to stdout

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). 使用epoll_wait ,即使我在调用与epoll相关的任何内容之前尝试打印,它似乎epoll_wait “吃掉”所有已写入stdout并延迟打印,直到epoll_wait接收到一个事件为止(甚至可能在我的开始时主要方法,但仍无法打印)。

An example of prints that will not show until after epoll_wait has received an event: epoll_wait收到事件之后epoll_wait显示的打印示例:

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 ? 写入stderr可以正常工作,但是如何写入stdout呢? How do I prevent epoll_wait from blocking prints to stdout ? 如何防止epoll_wait阻止打印到stdout

The issue seems to be unrelated to epoll_wait . 该问题似乎与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: 使用fflush(stdout) 此代码的解决方案,因为缓冲与epoll_wait无关,但与用户空间缓冲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. 综上所述,这似乎是在错误的地方寻找本应显而易见的问题的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM