Sobes.tech
Middle

What happens to the process memory when we create its copy using the fork system call in the Linux operating system?

sobes.tech AI

Answer from AI

When fork() is called, the operating system kernel creates a new process, a child process, which is almost an exact copy of the parent process.

In terms of memory, the following occurs:

  1. Virtual Address Space: The child process receives its own, separate virtual address space, identical to the parent at the moment of fork(). This means the child process "sees" the same address ranges and has the same segment structure (code, data, stack, heap) as the parent.

  2. Physical Memory and Copy-on-Write (CoW): Initially, the child and parent processes share the same physical memory pages. This optimization is known as "Copy-on-Write" (CoW). Physical memory is only copied when one of the processes attempts to modify the contents of a page.

    • If the parent or child process writes data to a page that was previously shared, the kernel creates a new copy of that physical memory page for the writing process. The other process continues to use the old version of the page.
    • If the processes only read data, they continue to share the same physical memory page.
  3. File Descriptors: Open file descriptors are inherited by the child process. Both processes share pointers to file offsets.

  4. Other Resources: Other resources, such as signal handlers, current working directory, etc., are also copied or inherited.

Here is a simplified illustration:

Before fork():

+-------------------+
| Parent process |
| +--------------+ |
| | Virtual Memory |
| | (Stack, Heap, ...) |
| +--------------+ |
|      |            |
|      V            |
| +--------------+ |
| | Physical Memory |
| +--------------+ |
+-------------------+

Immediately after fork() (CoW):

+-----------------------+   +-----------------------+
| Parent process        |   | Child process         |
| +-------------------+ |   | +-------------------+ |
| | Virtual Memory     | |   | | Virtual Memory     | |
| | (Same structure)   | |   | | (Same structure)   | |
| +-------------------+ |   | +-------------------+ |
|        |             |   |        |             |
|        V             |   |        V             |
| +-------------------+ |   | +-------------------+ |
| | Physical Memory    | |   | | Physical Memory    | |
| | (Some pages shared) | |   | | (Some pages shared) | |
| +-------------------+ |   | +-------------------+ |
+-----------------------+   +-----------------------+
          |                            |
          +----------------------------+
        Initial shared pages of physical memory

After writing to one of the pages (CoW triggered):

+-----------------------+   +-----------------------+
| Parent process        |   | Child process         |
| +-------------------+ |   | +-------------------+ |
| | Virtual Memory     | |   | | Virtual Memory     | |
| |                   | |   | |                   | |
| +-------------------+ |   | +-------------------+ |
|        |             |   |        |             |
|        V             |   |        V             |
| +-------------------+ |   | +-------------------+ |
| | Physical Memory    | |   | | Physical Memory    | |
| | (Separate pages)    | |   | | (Separate pages)    | |
| +-------------------+ |   | +-------------------+ |
+-----------------------+   +-----------------------+

It is important to understand that despite having identical virtual address spaces immediately after fork(), any changes in memory in one process (variable writes, heap allocation/deallocation) do not affect the other process's memory thanks to the CoW mechanism. These are fully independent processes.