为什么只有在写端关闭的情况下,管道的读端才读取EOF? [英] Why does the read end of a pipe read EOF only if the write end is closed?

查看:204
本文介绍了为什么只有在写端关闭的情况下,管道的读端才读取EOF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不明白关闭管道的写端"和不向管道写任何东西"之间的区别.如果我不向管道写入任何内容并且管道为空,为什么读取端只是被阻塞而不是读取EOF?与关闭写入结束有何不同?

解决方案

从管道(或其他任何东西)读取EOF表示不再有输入,并且将来也不会有任何输入. /p>

如果目前没有输入可用,但尚未关闭管道,则读取器(默认情况下)将阻塞等待输入;如果写入者随后将其写入管道,则该数据将可供读取者使用.一个EOF会告诉读者停止尝试读取更多数据.

I don't really understand the difference between "closing the write end of the pipe" and "not writing anything to the pipe". If I don't write anything to the pipe and the pipe is empty, why is the read end simply blocked rather than reading an EOF? How is that different from closing the write end?

解决方案

Reading an EOF from a pipe (or from anything) indicates that there's no more input, and that there won't be any more input in the future.

If there's no input available at the moment, but the pipe hasn't been closed, then the reader will (by default) block waiting for input; if the writer then writes to the pipe, that data will become available to the reader. An EOF would tell the reader to stop trying to read any more data.

这篇关于为什么只有在写端关闭的情况下,管道的读端才读取EOF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆