Hard
A MAP_PRIVATE file mapping and another process's pwrite()
data.bin is two 4 KiB pages, every byte 'a'. Process A maps it MAP_PRIVATE, reads page 0, and writes to page 1. Process B then opens the same file and uses ordinary buffered pwrite() to write 'B' at offsets 0 and 4096. After B finishes, A prints p[0] and p[4096]. What does A print on Linux?
process_a.cpp
#include <sys/mman.h>
#include <fcntl.h>
#include <cstdio>
int main() {
int fd = open("data.bin", O_RDWR); // 8192 bytes of 'a'
char* p = (char*)mmap(nullptr, 8192, PROT_READ | PROT_WRITE,
MAP_PRIVATE, fd, 0);
volatile char r = p[0]; // read page 0
p[4096] = 'P'; // write page 1
(void)r;
wait_for_process_b(); // B: pwrite(fd2, "B", 1, 0); pwrite(fd2, "B", 1, 4096);
printf("%c %c\n", p[0], p[4096]);
}50 of 53 in Operating Systems