5

In reference to this question about read() and write(), I'm wondering if each open file description has its own read and write buffers or if perhaps there's a single read and write buffer for a file when it has been opened multiple times at once. I'm curious because this would have an effect on what exactly happens with overlapping writes to the same file. Perhaps this is something that varies among Unixes?

(To my understanding, "file description" refers to the info/options about an open file, such as the current marker position. "File descriptor", in contrast, refers to just the number used in a process to refer to a description.)

Community
  • 1
  • 1
Jegschemesch
  • 11,414
  • 4
  • 32
  • 37
  • 1
    I think the low level Unix I/O is mostly unbuffered. But if opened as a stream (fopen etc) then a buffer is created for the FILE structure. – seand Mar 05 '11 at 04:02
  • 1
    @seand To my understanding, read/write are buffered by default unless the file is opened with O_DIRECT. The stream-based i/o lib actually adds a second layer of buffering, this time in the process. – Jegschemesch Mar 05 '11 at 06:23

2 Answers2

5

This depends a bit on whether you are talking about sockets or actual files.

Strictly speaking, a descriptor never has its own buffers; it's just a handle to a deeper abstraction.

File system objects have their "own" buffers, at least when they are required. That is, if a program writes less than the file system block size, the kernel has no choice but to read a FS block and merge the write with the existing data.

This buffer is attached to the vnode and at a lower level, possibly an inode. It's owned by the file and not the descriptor. It may be kept around for a long time if memory is available.

In the case of a socket, then a stream, but not specifically a single descriptor, does actually have buffers that it owns.

DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
  • Thanks. As someone else put it in the other question, this may vary between os's, but generally whole blocks of the underlying filesystem get individually buffered, and each buffered block is shared among however many readers/writers might be accessing the file. It sounds kinda like a mini-paging system of its own where individual block buffers get marked as dirty when written to. Good to know about sockets, though I was mainly curious about 'regular' files. – Jegschemesch Mar 05 '11 at 06:13
0

If the files were open in blocking mode then yes there should only be one buffer. I would bet the default is non blocking for performance reasons.

fdo
  • 26
  • 3