Created
July 6, 2017 08:41
-
-
Save hajimehoshi/c6de436f6c2ca4e6023df208c286800f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <fcntl.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <sys/mman.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#define PAGE_SIZE (4096) | |
static const int size = 100 * PAGE_SIZE; | |
void runParent(int fd) { | |
sleep(1); | |
printf("begin mincore\n"); | |
void* memory = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | |
int page_count = (size + PAGE_SIZE - 1) / PAGE_SIZE; | |
char* vec = (char*)malloc(page_count); | |
mincore(memory, size, vec); | |
int resident_page_count = 0; | |
int i = 0; | |
for (i = 0; i < page_count; i++) { | |
resident_page_count += vec[i] & 1; | |
} | |
printf("end mincore\n"); | |
printf("resident_page_count: %d\n", resident_page_count); | |
} | |
void runChild(int fd) { | |
printf("begin mmap\n"); | |
char* memory = (char*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | |
int i = 0; | |
// Access pages to use physical memory: | |
/*for (i = 0; i < size; i++) { | |
memory[i] = 0x01; | |
}*/ | |
printf("end mmap: %p\n", memory); | |
sleep(3); | |
} | |
int main() { | |
int fd = open("./sharedmemory", O_CREAT | O_TRUNC | O_RDWR, 0666); | |
printf("fd: %d\n", fd); | |
ftruncate(fd, size); | |
pid_t pid = fork(); | |
if (pid == 0) { | |
runChild(fd); | |
return 0; | |
} | |
runParent(fd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment