Mem0ry Mapped files?

This topic is 2655 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm still a little confused on how a memory mapped file works. If anyone could provide a easy to understand explanation id appreciate it!

Share on other sites
And if the explanations available from google still leave you confused, consider referencing one of them here and ask about the specific parts of it you don't understand. After all, if we just "explain memory mapping", we'll only end up repeating what's already been said — a waste of everyone's time, yours included.

Share on other sites
mmaps go like this:

\$ man mmap*snip*#include <sys/mman.h>       #include <sys/stat.h>       #include <fcntl.h>       #include <stdio.h>       #include <stdlib.h>       #include <unistd.h>       #define handle_error(msg)            do { perror(msg); exit(EXIT_FAILURE); } while (0)       int       main(int argc, char *argv[])       {           char *addr;           int fd;           struct stat sb;           off_t offset, pa_offset;           size_t length;           ssize_t s;           if (argc < 3 || argc > 4) {               fprintf(stderr, "%s file offset [length]\n", argv[0]);               exit(EXIT_FAILURE);           }           fd = open(argv[1], O_RDONLY);           if (fd == -1)               handle_error("open");           if (fstat(fd, &sb) == -1)           /* To obtain file size */               handle_error("fstat");           offset = atoi(argv[2]);           pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) - 1);               /* offset for mmap() must be page aligned */           if (offset >= sb.st_size) {               fprintf(stderr, "offset is past end of file\n");               exit(EXIT_FAILURE);           }           if (argc == 4) {               length = atoi(argv[3]);               if (offset + length > sb.st_size)                   length = sb.st_size - offset;                       /* Can't display bytes past end of file */           } else {    /* No length arg ==> display to end of file */               length = sb.st_size - offset;           }           addr = mmap(NULL, length + offset - pa_offset, PROT_READ,                       MAP_PRIVATE, fd, pa_offset);           if (addr == MAP_FAILED)               handle_error("mmap");           s = write(STDOUT_FILENO, addr + offset - pa_offset, length);           if (s != length) {               if (s == -1)                   handle_error("write");               fprintf(stderr, "partial write");               exit(EXIT_FAILURE);           }           exit(EXIT_SUCCESS);       } /* main */*snip*

Share on other sites
Btw, are you planning on some kind of cracker toolkit?

Share on other sites
if i understand correctly it u can use memorymapping to pretty much edit a file while it's still on the hard drive. Memory mapping creates virtual addresses for the contents of the file in which u can edit/read while it's still on the hard drive? is this correct?

im confused on how memory mapping can be used to load a file to be run like this....im not even sure if i completely understand it -thx

Share on other sites
Why are you using '0' in place of 'o' in your thread titles?

Share on other sites
Quote:
 Original post by nuclear123if i understand correctly it u can use memorymapping to pretty much edit a file while it's still on the hard drive. Memory mapping creates virtual addresses for the contents of the file in which u can edit/read while it's still on the hard drive? is this correct?im confused on how memory mapping can be used to load a file to be run like this....im not even sure if i completely understand it -thx

Your assumption is correct. However I don't understand what you mean by the second statement. Are you refering to executing code or do you want to know how memory mapping is actually implemented?

Share on other sites
"like virtual memory,memory mapped files allow u to reserve a ragion of address space and commit physical storage to the region. the difference is that the physical storage comes from a file that is already on disk instead of the systems paging file. Once the file has been mapped, you can access it as if the while file were loaded in memory" <-- windows via c/c++ book. So in other words there are 2 ways to loading a file to be run??? how does this not use the systems paging file? if the code that your requesting from the file is not in memory, it must be PAGED in from the HD right???

Share on other sites
Quote:
 Original post by nuclear123So in other words there are 2 ways to loading a file to be run???

They're comparing opening it with traditional, stream based methods, and reading the entire thing into memory. In C++ you'd handle a "FILE" or fstream, C# has various Stream objects, etc.

Quote:
 how does this not use the systems paging file?

The paging file is used by the OS to back pages of memory that:
1) Needs to get 'paged out' because we don't have enough RAM to keep everything in real memory
2) The OS can't tell is already backed by something else. If we manually load things into memory, the OS knows little about it -- just that it's important data. If we use memory mapping, the OS can go, oh hey, this memory just represents this file. I can back this memory with that file instead of using the page file.

Note that you can disable your paging file in many/most OSes - it's not a fundamental part of your memory system. It's there so you can pretend you have more memory than you do, basically, which is surprisingly handy.

If you've got enough physical memory, the OS doesn't need to use the page file -- it can just store all the memory pages in actual memory. If you've memory mapped a file, the OS can use that file directly, and again doesn't need to use the page file.

Share on other sites
Quote:
 Original post by nuclear123 in a PM to meok explain if this is correct with manual mapping.

I'm not sure if you're refering to memory mapping or manual file operations here.

Quote:
 u load a file byte for byte into memory....so only one I/O operation needs to be performed...if u were to not do this..everytime u wanted to access a part of the file you would have to perform another I/O operation.

Sounds about right. Please read up on traditional file I/O methods if you're curious about the differences between the two.

Quote:
 Also im still not understanding what u mean by "backed"?

"Supported". "Storing a duplicate copy".