Separate write()
calls are processed separately, not as a single atomic write transaction, and interleaving is entirely possible when multiple processes/threads are writing to the same file. The order of the actual writes is determined by the schedulers (both kernel process scheduler, and for "green" threads the thread library's scheduler).
Unless you specify otherwise (O_DIRECT
open
flag or similar, if supported), read()
and write()
operate on kernel buffers and read()
will use a loaded buffer in preference to reading the disk again.
Note that this may be complicated by local file buffering; for example, stdio
and iostreams
will read file data by blocks into a buffer in the process which is independent of kernel buffers, so a write()
from elsewhere to data that are already buffered in stdio
won't be seen. Likewise, with output buffering there won't be any actual kernel-level output until the output buffer is flushed, either automatically because it has filled up or manually due to fflush()
or C++'s endl
(which implicitly flushes the output buffer).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…