Created
March 16, 2020 09:57
-
-
Save GitBubble/782e1c25b33ad4cabd9c02a48f497a31 to your computer and use it in GitHub Desktop.
shm_get
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 <stdio.h> | |
#include <string.h> | |
#include <sys/mman.h> | |
#include <unistd.h> | |
#include <thread> | |
#include <sys/mman.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
int main(void) { | |
size_t pagesize = getpagesize(); | |
int hnd = shm_open("arthur", O_CREAT | O_RDWR , 0777); | |
if(hnd){ | |
printf(" return value , %d\n",hnd); | |
//hnd = shm_open("arthur", O_CREAT| O_RDWR , S_IRWXO); | |
//printf("error occured , %d\n",hnd); | |
} | |
printf("System page size: %zu bytes\n", pagesize); | |
char * region = (char*)mmap( | |
//(void*) (pagesize * (1 << 20)), // Map from the start of the 2^20th page | |
(void*)0x1234000000, | |
pagesize, // for one page length | |
PROT_READ|PROT_WRITE|PROT_EXEC, | |
MAP_SHARED, // to a private block of hardware memory | |
hnd, | |
0 | |
); | |
if (region == MAP_FAILED) { | |
perror("Could not mmap"); | |
return 1; | |
} | |
//std::thread loop = std::thread( [region](){ while(1) { strcpy(region, "Hello, poftut.com"); } } ); | |
std::thread loop2 = std::thread( [hnd, region](){ | |
while(1) { | |
//strcpy(region, "thread2: [email protected]"); | |
printf("Contents of region: %s\n", region); | |
printf("thread2: address is %p, hnd = %d\n" , region,hnd); } } ); | |
//loop.join(); | |
loop2.join(); | |
int unmap_result = munmap(region, 1 << 10); | |
if (unmap_result != 0) { | |
perror("Could not munmap"); | |
return 1; | |
} | |
// getpagesize | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment