Created
November 7, 2022 11:16
-
-
Save OMGZui/f9bf7637ac6cfcbeba1f5bd0222bbb8e 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 <iostream> | |
#include <sys/mman.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <cstdio> | |
#include <cstdlib> | |
int main() { | |
int fd = ::shm_open("/test", O_RDWR | O_CREAT | O_EXCL, 0600); | |
if (fd < 0) { | |
shm_unlink("/test"); | |
perror("shm open failed"); | |
return 0; | |
} | |
size_t size = 1 * 1024 * 1024 * 1024; | |
::ftruncate(fd, size); | |
int prot = PROT_READ | PROT_WRITE; | |
uint32_t *p1 = (uint32_t *) (mmap(nullptr, size, prot, MAP_SHARED, fd, 0)); | |
uint32_t *p2 = (uint32_t *) (mmap(nullptr, size, prot, MAP_SHARED, fd, 0)); | |
uint32_t *p3 = (uint32_t *) (mmap(nullptr, size, prot, MAP_SHARED, fd, 0)); | |
::close(fd); | |
*p1 = 0xcafebabe; | |
::printf("Address of addr1: %p, value is 0x%x\n", p1, *p1); | |
::printf("Address of addr2: %p, value is 0x%x\n", p2, *p2); | |
::printf("Address of addr3: %p, value is 0x%x\n", p3, *p3); | |
::getchar(); | |
*p2 = 0xcafebaba; | |
::printf("Address of addr1: %p, value is 0x%x\n", p1, *p1); | |
::printf("Address of addr2: %p, value is 0x%x\n", p2, *p2); | |
::printf("Address of addr3: %p, value is 0x%x\n", p3, *p3); | |
::getchar(); | |
munmap(p1, size); | |
munmap(p2, size); | |
munmap(p3, size); | |
shm_unlink("/test"); | |
std::cout << "hello" << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment