Hard
When copy-on-write actually copies
At the instant fork() returns, how much of the ~4MB big vector's backing memory has actually been physically duplicated? And what does the parent print afterward?
main.cpp
#include <unistd.h>
#include <sys/wait.h>
#include <cstdio>
#include <vector>
int main() {
std::vector<int> big(1000000, 7); // ~4MB, already resident
pid_t pid = fork();
if (pid == 0) {
big[0] = 99; // child modifies one element
_exit(0);
}
wait(nullptr);
printf("%d\n", big[0]);
return 0;
}11 of 53 in Operating Systems