繁体   English   中英

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

[英]epoll_wait() blocks prints to stdout

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

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

写入stderr可以正常工作,但是如何写入stdout呢? 如何防止epoll_wait阻止打印到stdout

该问题似乎与epoll_wait无关。 以下是违规代码的摘要:

// 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);

使用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);

综上所述,这似乎是在错误的地方寻找本应显而易见的问题的情况。

暂无
暂无

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

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