Sign in to follow this  

Mem0ry Mapped files?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
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*



Was this helpful? If not, please be more specific.

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by nuclear123
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


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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by nuclear123
So 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 this post


Link to post
Share on other sites
Quote:
Original post by nuclear123 in a PM to me
ok 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".

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this