Copy-on-Write in Operating System

In this tutorial, we will be covering the Copy on Write which is one of the resource management techniques.

Copy-on-Write(CoW) is mainly a resource management technique that allows the parent and child process to share the same pages of the memory initially. If any process either parent or child modifies the shared page, only then the page is copied.

The CoW is basically a technique of efficiently copying the data resources in the computer system. In this case, if a unit of data is copied but is not modified then "copy" can mainly exist as a reference to the original data.

But when the copied data is modified, then at that time its copy is created(where new bytes are actually written )as suggested from the name of the technique.

The main use of this technique is in the implementation of the fork system call in which it shares the virtual memory/pages of the Operating system.

Recall in the UNIX(OS), the fork() system call is used to create a duplicate process of the parent process which is known as the child process.

  • The CoW technique is used by several Operating systems like Linux, Solaris, and Windows XP.

  • The CoW technique is an efficient process creation technique as only the pages that are modified are copied.

Free pages in this technique are allocated from a pool of zeroed-out pages.

The Copy on Write(CoW) Technique

The main intention behind the CoW technique is that whenever a parent process creates a child process both parent and child process initially will share the same pages in the memory.

  • These shared pages between parent and child process will be marked as copy-on-write which means that if the parent or child process will attempt to modify the shared pages then a copy of these pages will be created and the modifications will be done only on the copy of pages by that process and it will not affect other processes.

Now its time to take a look at the basic example of this technique:

Let us take an example where Process A creates a new process that is Process B, initially both these processes will share the same pages of the memory.

Figure: Above figure indicates parent and child process sharing the same pages

Now, let us assume that process A wants to modify a page in the memory. When the Copy-on-write(CoW) technique is used, only those pages that are modified by either process are copied; all the unmodified pages can be easily shared by the parent and child process.

Figure: After Page Z is modified by Process A

Whenever it is determined that a page is going to be duplicated using the copy-on-write technique, then it is important to note the location from where the free pages will be allocated. There is a pool of free pages for such requests; provided by many operating systems.

And these free pages are allocated typically when the stack/heap for a process must expand or when there are copy-on-write pages to manage.

These pages are typically allocated using the technique that is known as Zero-fill-on-demand. And the Zero-fill-on-demand pages are zeroed-out before being allocated and thus erasing the previous content.