读取行缓冲流可以产生多行吗?
Can reading line buffered streams yield multiple lines?
给定以下设置:
- child 进程已打开以供读取(例如通过
popen()
)
- child 的
stdout
已设置为 行缓冲 (_IOLBF
)
- parent 监控 child 的
stdout
(select()
、poll()
或 epoll()
) 上的数据
一旦数据可用(fgets()
或 getline()
),- parents 从 child 的
stdout
中读取
看到流是如何设置为行缓冲的,我们是否可以安全地假设只有一行要读取,或者是否存在缓冲区中有多行等待的可能情况,这意味着我们有调用 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.
因此,行缓冲并不意味着所有数据都缓冲在一行中,而是一有新行就发送数据。
因此,您的问题的答案是多行等待读取的可能情况(当然,取决于您的子进程实际发送的内容)。
给定以下设置:
- child 进程已打开以供读取(例如通过
popen()
) - child 的
stdout
已设置为 行缓冲 (_IOLBF
) - parent 监控 child 的
stdout
(select()
、poll()
或epoll()
) 上的数据
一旦数据可用( - parents 从 child 的
stdout
中读取
fgets()
或 getline()
),看到流是如何设置为行缓冲的,我们是否可以安全地假设只有一行要读取,或者是否存在缓冲区中有多行等待的可能情况,这意味着我们有调用 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()
orgetline()
until they hitEOF
(orEAGAIN
/EWOULDBLOCK
for non-blocking streams)?
我从
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.
因此,行缓冲并不意味着所有数据都缓冲在一行中,而是一有新行就发送数据。
因此,您的问题的答案是多行等待读取的可能情况(当然,取决于您的子进程实际发送的内容)。