I need help input a file in chunks into mmap
I am trying to use mmap to read a file using chunks of 1024.
Here is a code snippit...开发者_JAVA技巧.
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).
精彩评论