36 lines
589 B
C++
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;
|
||
|
}
|