3 ms·
> Doesn't linux do shared-read-only mapping of pages on read(2) and only copy if you modify? No, I don't think that's how it works for the plain read() system
by elihu 6y ago
> Doesn't linux do shared-read-only mapping of pages on read(2) and only copy if you modify?
No, I don't think that's how it works for the plain read() system call. (The kernel will have its own page cache / buffer cache entries that would be the same for both processes, but read() can't map those directly, it has to make a copy.) Though it should work the way you describe if multiple processes mmap the same file, in which case the page cache pages are exactly the same pages that are mapped by the processes.
However, if you use read() to copy data into a buffer and then fork() the process, I believe the child process should end up with a copy of the buffer that is backed by the same physical pages with copy-on-write semantics.