I was reading a textbook which talks about how fork() work with virtual memory as:

When the fork function is called by the current process, the kernel creates various data structures for the new process and assigns it a unique PID. To create the virtual memory for the new process, it creates exact copies of the current process’s mm_struct, area structs, and page tables. It flags each page in both processes as read-only, and flags each area struct in both processes as private copy-on-write

I don't understand why it needs to flag each page in both processes as read-only.If each page in the parent process is read-only then the parent process will never be able to modify some uninitialised global variables(in .bss section), then how is the program going to work?


If the parent’s page mapping were left writable, then any change made by the parent process would be applied directly, and since the physical page is shared, it would also be visible from the child. Making both mappings read-only ensures that changes made by any process remain private.

Not the answer you're looking for? Browse other questions tagged or ask your own question.