C++ || Snippet – How To Use Memory Mapped Files
The following is sample code which demonstrates the use of the “mmap”, “munmap” and “getpagesize” function calls on Unix based systems.
A memory mapped file is a segment of virtual memory which has been assigned a direct byte for byte correlation with some portion of a file or file like resource. This resource is typically a file that is physically present on disk, but can also be a device, shared memory object, or other resource that the operating system can reference through a file descriptor.
The primary benefit of memory mapping a file is increasing I/O performance, especially when used on large files. Accessing memory mapped files is faster than using direct read and write operations for two reasons. Firstly, a system call is orders of magnitude slower than a simple change to a program’s local memory. Secondly, in most operating systems the memory region mapped actually is the kernel’s page cache (file cache), meaning that no copies need to be created in user space.
The following example demonstrates the use of the “mmap” to map a file into memory, and display its contents to the screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
// ============================================================================ // Author: K Perkins // Date: Oct 4, 2013 // Taken From: http://programmingnotes.org/ // File: mmap.cpp // Description: Demonstrate the use of memory mapped files // ============================================================================ #include <iostream> #include <cstdlib> #include <unistd.h> #include <sys/mman.h> #include <fcntl.h> using namespace std; int main(int argc, char* argv[]) { // declare variables int fd = -1; int pagesize = -1; char* data = NULL; // check if theres enough commandline args if(argc < 2) { cerr<<"USAGE: "<<argv[0]<<" <FILE NAME>n"; exit(1); } // open the specified file fd = open(argv[1], O_RDWR); // check if the open was successful if(fd < 0) { cerr<<"open error (file not found)n"; exit(1); } // get the page size pagesize = getpagesize(); // map the file into memory data = (char*)mmap((caddr_t)0, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); // did the mapping succeed ? if(!data) { cerr<<"mmap errorn"; exit(1); } // print the whole file character-by-character for(int x = 0; x < pagesize; ++x) { cerr<<data[x]; } // optional: write a string to the file //memcpy(data, "Hello world, this is a testn", sizeof("Hello world, this is a test")); // unmap the shared memory region munmap(data, pagesize); // close the file close(fd); return 0; }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
The following is sample output:
./mmap mmap.cpp
[THE CONTENTS OF "MMAP.CPP" IS DISPLAYED TO THE SCREEN]
Leave a Reply