#include #include #include #include #include #include #ifndef MADV_PAGEOUT #define MADV_PAGEOUT 21 #endif /* 1G size testing */ static int SIZE = 1024UL*1024*1024; //static int SIZE = 2UL*1024*1024; //static int SIZE = 64*1024; int main(void) { pid_t pid; char *shared_memory = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); if (shared_memory == MAP_FAILED) { perror("mmap failed"); exit(EXIT_FAILURE); } //populate the shmem memset(shared_memory, 0xaa, SIZE); /* create child */ pid = fork(); if (pid < 0) { perror("fork failed"); exit(EXIT_FAILURE); } else if (pid == 0) { printf("Child process sees shared_memory[0x%lx] = %d\n", (unsigned long)shared_memory, *shared_memory); (*shared_memory)++; printf("Child process incremented shared_memory to %d\n", *shared_memory); exit(0); } else { /* parent:wait for child to complete */ wait(NULL); printf("Parent process sees shared_memory = %d\n", *shared_memory); (*shared_memory)++; printf("Parent process incremented shared_memory to %d\n", *shared_memory); } if (madvise(shared_memory, SIZE, MADV_PAGEOUT)) { perror("madvise(MADV_HUGEPAGE)"); exit(1); } if (madvise(shared_memory, SIZE, MADV_PAGEOUT)) { perror("madvise(MADV_HUGEPAGE)"); exit(1); } memset(shared_memory, 0, SIZE); if (munmap(shared_memory, SIZE) == -1) { perror("munmap failed"); exit(EXIT_FAILURE); } return 0; }