读取行缓冲流可以产生多行吗?

Can reading line buffered streams yield multiple lines?

给定以下设置:

看到流是如何设置为行缓冲的,我们是否可以安全地假设只有一行要读取,或者是否存在缓冲区中有多行等待的可能情况,这意味着我们有调用 fgets()getline() 直到他们达到 EOF(或 EAGAIN / EWOULDBLOCK 对于 non-blocking 流)?

can we safely assume that there will only ever be one single line to read, or are there possible scenarios where there are multiple lines waiting in the buffer, meaning we have to call fgets() or getline() until they hit EOF (or EAGAIN / EWOULDBLOCK for non-blocking streams)?

我从 中窃取了相关部分(这又链接到 C11 标准):

When a stream is line buffered, characters are intended to be transmitted to or from the host environment as a block when a new-line character is encountered.

因此,行缓冲并不意味着所有数据都缓冲在一行中,而是一有新行就发送数据。

因此,您的问题的答案是多行等待读取的可能情况(当然,取决于您的子进程实际发送的内容)。