Medium
mmap MAP_SHARED across fork()
What does the parent print?
main.cpp
#include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h>
#include <cstdio>
int main() {
int* counter = (int*)mmap(nullptr, sizeof(int), PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
*counter = 0;
if (fork() == 0) {
*counter = 42;
_exit(0);
}
wait(nullptr);
printf("counter = %d\n", *counter);
return 0;
}10 of 53 in Operating Systems