I have the following questions related to handling files and mapping them (mmap
):
- We know that if we create a file, and write to that file, then either ways we are writing to the memory. Then why map that file to memory using
mmap
and then write? - If it is because of protection that we are achieving using
mmap
-PROT_NONE
,PROT_READ
,PROT_WRITE
, then the same level of protection can also be achieved using files.O_RDONLY
,O_RDWR
etc. Then whymmap
? - Is there any special advantage we get on mapping files to memory, and then using it? Rather than just creating a file and writing to it?
- Finally, suppose we
mmap
a file to memory, if we write to that memory location returned by mmap, does it also simultaneously write to that file as well?
Edit: sharing files between threads
As far as I know, if we share a file between two threads (not process) then it is advisable to mmap
it into memory and then use it, rather than directly using the file.
But we know that using a file means, it is surely in main memory, then why again the threads needs to be mmaped?