[英]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.