Reputation: 706
I did not know binary files could be read with mmap()
. I used to think mmap()
could only be used for IPC(interprocess communication) in Linux to exchange data between unrelated process.
Can someone explain how files are read with mmap()
? I heard it has huge advantage when binary files are randomly accessed.
Upvotes: 0
Views: 1965
Reputation: 134046
Well, mmap
ping a file is done the same way as it is done for the IPC or mapping anonymous memory. In the case of mapping anonymous memory the parts that have not been written to will be filled with zeroed pages on demand.
In case of a mapped file, the pages that correspond to file contents are read upon access (and upon writes too) from the file / or the buffer cache. Reading or writing outside the size of the file will result in SIGBUS. Essentially the pointer returned by mmap
can be considered in the similar manner as the pointer returned by malloc
, except that up to the size of mapping / up to the end-of-file bytes within the mapping are automatically read from / and possibly written to the backing file transparently.
Example:
fd = open("input.txt", O_RDWR, 0666);
fstat(fd, &stat_result);
char* contents = mmap(0, stat_result->st_size,
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
(error checking omitted)
After executing that you can consider contents
as pointing to the first byte of a character array of stat_result->st_size
characters, and you can use it just like an ordinary array, and the operating system will transparently write back the changes into the file.
With mmap
the operating system will have a better view about which parts of the file should be kept in memory / buffer cache and which shouldn't.
Upvotes: 1