#include #include #include #include #include #include #ifndef MADV_PAGEOUT #define MADV_PAGEOUT 21 #endif /* 1G size testing */ static unsigned long SIZE = 10UL*1024*1024*1024; //static int SIZE = 2UL*1024*1024; //static int SIZE = 64*1024; static void child_swapin_shmem(char *shared_memory) { char *second_memory; int i; //swap in shmem without aligned 64k second_memory = shared_memory + 4096 * 2; for (i = 0; i < SIZE; i += 4096 * 16) { *(second_memory + i) = (char)i; *(second_memory + i + 4096 * 4) = (char)i; *(second_memory + i + 4096 * 9) = (char)i; } } int main(void) { pid_t pid; char *second_memory; int i; 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); /* swapout all shmem */ 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); } /* 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)++; child_swapin_shmem(shared_memory); printf("Child process incremented shared_memory to %d\n", *shared_memory); exit(0); } else { printf("Parent process sees shared_memory = %d\n", *shared_memory); (*shared_memory)++; printf("Parent process incremented shared_memory to %d\n", *shared_memory); } //swap in shmem without aligned 64k second_memory = shared_memory + 4096 * 3; for (i = 0; i < SIZE; i += 4096 * 16) { *(second_memory + i) = (char)i; *(second_memory + i + 4096 * 3) = (char)i; *(second_memory + i + 4096 * 10) = (char)i; } /* parent:wait for child to complete */ wait(NULL); if (munmap(shared_memory, SIZE) == -1) { perror("munmap failed"); exit(EXIT_FAILURE); } return 0; }