output buffer
Java ProcessBuilder往stdout/stderr写东西时,经常有些默名的异常。今天看到linux setbuf()函数说明时才找到root case。
Linux三种buffer:a.un-buffered;b.block buffer;c.line buffer。
默认是block buffer,达到block大小(可能是512bytes)才输出。
函数指出当输出与终端挂钩时,为line buffer,碰到\n或者用户输入\n时输出,如stdout。
stderr通常为无un-buffered。
ProcessBuilder 往outputstream写时,writeLine()没问题,write()时,就得手工调用flush()了。写stderr()完全没问题。
Refer: man setbuffer
setbuffer(), setlinebuf(): _BSD_SOURCE
DESCRIPTION
The three types of buffering available are unbuffered, block buffered, and line buffered. When an output stream is unbuffered, informa\u2010
tion appears on the destination file or terminal as soon as written; when it is block buffered many characters are saved up and written as
a block; when it is line buffered characters are saved up until a newline is output or input is read from any stream attached to a termi\u2010
nal device (typically stdin). The function fflush(3) may be used to force the block out early. (See fclose(3).) Normally all files are
block buffered. When the first I/O operation occurs on a file, malloc(3) is called, and a buffer is obtained. If a stream refers to a
terminal (as stdout normally does) it is line buffered. The standard error stream stderr is always unbuffered by default.