Copy on Write

The kernel uses the copy-on-write technique (COW) to prevent all data of the parent process from being copied when fork is executed. This technique exploits the fact that processes normally use only a fraction of their pages in memory.8 When fork is called, the kernel would usually create an identical copy of each memory page of the parent process for the child process. This has two very negative effects:

1. A large amount of RAM, a scarce resource, is used.

2. The copy operation takes a long time.

The negative impact is even greater if the application loads a new program using exec immediately after process duplication. This means, in effect, that the preceding copy operation was totally superfluous as the process address space is reinitialized and the data copied are no longer needed.

The kernel can get around this problem by using a trick. Not the entire address space of the process but only its page tables are copied. These establish the link between virtual address space and physical pages as described briefly in Chapter 1 and at length in Chapters 3 and 4. The address spaces of parent and child processes then point to the same physical pages.

Of course, parent and child processes must not be allowed to modify each other's pages,9 which is why the page tables of both processes indicate that only read access is allowed to the pages — even though they could be written to in normal circumstances.

Providing that both processes have only read access to their pages in memory, data sharing between the two is not a problem because no changes can be made.

As soon as one of the processes attempts to write to the copied pages, the processor reports an access error to the kernel (errors of this kind are called page faults). The kernel then references additional memory management data structures (see Chapter 4) to check whether the page can be accessed in Read and Write mode or in Read mode only — if the latter is true, a segmentation fault must be reported to the

8The pages most frequently accessed by the process are called the working set.

9With the exception of pages explicitly shared by both processes.

process. As you see in Chapter 4, the actual implementation of the page fault handler is more complicated because other aspects, such as swapped-out pages, must also be taken into account.

The condition in which a page table entry indicates that a page is ''Read Only'' although normally it would be writable allows the kernel to recognize that the page is, in fact, a COW page. It therefore creates a copy of the page that is assigned exclusively to the process — and may therefore also be used for write operations. How the copy operation is implemented is not discussed until Chapter 4 because extensive background knowledge of memory management is required.

The COW mechanism enables the kernel to delay copying of memory pages for as long as possible and — more importantly — to make copying unnecessary in many cases. This saves a great deal of time.

Continue reading here: Executing System Calls

Was this article helpful?

+3 0