I am trying to use mmap to read a file using chunks of 1024.
Here is a code snippit....
numberOfBuffers = filesize / buffersize;
if (filesize % buffersize)
{
numberOfBuffers++;
}
for (i = 0; i < numberOfBuffers; i++) {
if((map = mmap(NULL, buffersize, PROT_READ, MAP_PRIVATE, fd, i * buffersize)) == MAP_FAILED) {
perror("map failed");
}
if(munmap(map, buffersize) == -1) {
perror("unmap failed");
}
}
I am getting an 'illegal argument' error on the second iteration.
I'm looking to understand mmap and for help on how to iterate over mmap with a defined buffer size.
From the mmap man page:
offset must be a multiple of the page size as returned by sysconf(_SC_PAGE_SIZE).
Page size on a lot if systems (notably x86) is 4k (4096 bytes). So the first call will succeed (with offset 0), but the second call will fail (offset 1024 is not valid).
Try changing your chunk size to 4096 (or whatever the page size is on your platform, or better, use sysconf to get that information).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With