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/ipc.h> | |
#include <sys/shm.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/mman.h> | |
#define MAP_HUGE_2MB (21 << MAP_HUGE_SHIFT) | |
int main() | |
{ | |
int shm_id; | |
key_t mem_key; | |
int *shm_ptr; | |
int *map; | |
mem_key = ftok(".", 'a'); | |
/* | |
* To fix ENOMEM on this one, do: | |
* echo 10 > /proc/sys/vm/nr_overcommit_hugepages | |
*/ | |
shm_id = shmget(mem_key, sizeof(int), IPC_CREAT | SHM_HUGETLB | 0666); | |
if (shm_id < 0) { | |
perror("shmget"); | |
return 1; | |
} | |
shm_ptr = (int *) shmat(shm_id, NULL, 0); /* attach */ | |
if (shm_ptr == (void *)-1) { | |
perror("shmat"); | |
return 1; | |
} | |
printf("shm_ptr = %p\n", shm_ptr); | |
map = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | | |
MAP_ANONYMOUS, 0, 0); | |
if (map == MAP_FAILED) { | |
perror("mmap"); | |
return 1; | |
} | |
printf("map = %p\n", map); | |
map = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | | |
MAP_ANONYMOUS | MAP_HUGETLB | | |
MAP_HUGE_2MB, 0, 0); | |
if (map == MAP_FAILED) { | |
perror("mmap"); | |
return 1; | |
} | |
printf("map2m = %p\n", map); | |
scanf("%*s"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment