Private Means Private

Two processes run this on the same file to bump a shared counter. Both exit cleanly, msync returns 0, and the counter in the file is unchanged.

int fd = open("counts.bin", O_RDWR);
uint64_t *m = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
                   MAP_PRIVATE, fd, 0);
m[0] += 1;
msync(m, 4096, MS_SYNC);
munmap(m, 4096);
close(fd);

Which single change makes the increment reach the file?

A

Add MAP_POPULATE to the mmap flags

B

Replace MAP_PRIVATE with MAP_SHARED

C

Add MS_INVALIDATE to the msync flags

D

Open the file with O_RDWR | O_SYNC