I am writing a program to read and write a file at the same time. More specifically, all write operations are appending new data to the end of the file and all read operations are reading random positions of the file.
I am thinking of creating memory-mapped file (using mmap
) to achieve efficient read while writing via append (mode a
in open
). However, I don't think this will work because the memory-mapped file cannot change in size*, unless I munmap
and then mmap
it.
While "munmap
and then mmap
the file again" works, it has many downsides. Not only I need to perform 2 syscalls after every write (or before every read), which hurts performance, the base address returned from the next mmap
call after munmap
could be different from the previous one. Since I am planning to have other in-memory data structure storing pointers to specific offset of this memory mapped file, it could be very inconvenient.
Are there more elegant and efficient ways of doing this? The program will be mostly running on Linux (but solutions with portability to other POSIX systems are preferred). I have read through the following posts, but none of them seems to give a definitive answer.
How to portably extend a file accessed using mmap()
Can the OS automatically grow an mmap backed file?
My intuition is to use mmap
to "reserve" the file with a size that is large enough to accommodate the growth of file, say a few hundred of GiB (that is a very reasonable assumption in my use case). And then somehow reflect the change of file size in this mapped memory without invalidating it with munmap
. However, I am aware that accessing data beyond the real file boundary could result in a bus error. And the documentation isn't clear about whether changes in file size will get reflected.
*I am not 100% sure about this, but I couldn't find any source of elegantly changing the size of memory-mapped file.