Compare the attributes and behaviors of the text, data, heap, and stack segments during the `fork()` system call. Why are these segments handled differently?
When a process calls fork, the kernel must duplicate the process's address space to ensure that the child process has a separate, but initially identical, memory context. Different segments are handled differently as:-
Text segment is shared between parent and child due to its read-only nature.
The data and heap segments are managed using a copy-on-write mechanism to optimize memory usage and ensure independent modifications.
The stack segment is independently copied for the child process to maintain separate execution contexts.