Skip to content

Instantly share code, notes, and snippets.

@notcome
Created May 3, 2017 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notcome/90a268294ed81f0b01de42b5eace5bca to your computer and use it in GitHub Desktop.
Save notcome/90a268294ed81f0b01de42b5eace5bca to your computer and use it in GitHub Desktop.
Play mmap
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
int main() {
int fd = open("./test_file", O_RDWR);
if (fd == -1) {
std::cerr << "Failed to open file. Abort." << std::endl;
return -1;
}
char *buffer = new char[4096];
const char *filler = "Hello!!\n";
for (int i = 0; i < 4096; i += 8)
for (int j = 0; j < 8; j ++)
buffer[i + j] = filler[j];
buffer[4095] = 0;
write(fd, (const void *)buffer, 4096);
delete [] buffer;
buffer = (char *)mmap(NULL, 0x2000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
std::cout << buffer << std::endl;
buffer[0] = 'J';
close(fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment