After conducting a little research, I decided for myself that the most correct way is to
munmap
ftruncate
mmap
Q: Wouldn't it be extremely costly if you need to change the size of a mapped file all the time (transfer data from HDD to RAM back and forth)?
A:
Q: Does the munmap syscall provide writing back to the disk file?
A: Yes, munmap = munmap + msync + MS_ASYNC.
Q: Does
munmap + truncate + mmap
show better performance than
msync + munmap + truncate + mmap ?
A: Only the use of msync + MS_SYNC causes the overhead of excessive writing operations to HDD. To avoid it, you can omit using msync (it will be the same as using msync + MS_ASYNC).