Search code examples
csystem-callssys

(How) is it true that the write() syscall stores data in the kernel?


In this stackoverflow question answer it is said that after sys call returns the data is in kernel. But isn't data output to the display and no more in kernel? Can someone explain in more detail? I can't comment in mentioned article because I don't have enough reputation.

From: what is the different of using fflush(stdout) and not using it

When the syscall returns, the data is in your kernel. But modern operating systems buffer this data as well. This is used to reduce the number of disk writes, reduce latency and other things. This buffer is completely independent of the FILE buffer inside your program.


Solution

  • For currently active interactive devices, such as displays, the kernel will quickly display the data.1 The text you refer to in the other answer is about data being written to storage devices. The kernel may hold that data briefly, to combine it with other write operations. What is important here is that your program no longer has responsibility for the data. Even if your process exits or crashes after executing a fflush, the kernel will still write the data to storage. (The operating system can still crash, and data can be lost that way.)

    Footnote

    1 Depending on the operating system, it may be possible to stop terminal output by various means such as pressing Control-S, or a window may be currently covered by other windows and hence is not being updated.