Created
May 3, 2017 19:51
-
-
Save notcome/90a268294ed81f0b01de42b5eace5bca to your computer and use it in GitHub Desktop.
Play mmap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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