Luna/apps/shmem-test.cpp
apio 7293d47bf0
All checks were successful
continuous-integration/drone/push Build is passing
apps: Add shmem-test
2023-08-02 22:20:05 +02:00

36 lines
589 B
C++

#include <stdio.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
void* address = mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
if (address == MAP_FAILED)
{
perror("mmap");
return 1;
}
int* ptr = (int*)address;
*ptr = 16;
pid_t child = fork();
if (child < 0)
{
perror("fork");
return 1;
}
if (child == 0)
{
*ptr = 32;
_exit(0);
}
waitpid(child, NULL, 0);
printf("the value is %d\n", *ptr);
return 0;
}