top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How does copy-on-write and fork() works??

+1 vote
339 views

In the early versions of Unix system implementation, when a process called the fork() system call. It creates a child process, which is a copy of the parent process and has the copy of parent data space, heap and stack in the separate memory and there is no sharing except the code or text segment between the child and parent process.

But in current implementations, when fork() system called by a process; it does not performs the complete copy of data space, heap and stack. Instead, a technique called copy-on-write is used. In which both parent and child share these same region but the protection have changed read only by the kernel. when child tries to write, a page fault error is generated (does not show in the user space). which is handled by the kernel and a copy of page copied into the child process address space by the kernel and marked as writable.

Now my doubt is, when a child process called fork() system call and grand children try to write or modify, how will it work??

posted Aug 12, 2014 by Prakash Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

As you pointed out when fork happens, OS marks the pages as read-only, and marks the parent process as the master of these pages.

When a child process try to write any if these pages, a page fault occurs and OS copies the entire or just page on demand so the writing process will have a writable copy.

Now coming to when grandchild is created, the method is same grandfather is the one who has the writable copy and grandchild will get the copy only when it tries to write and page fault occurs.

Comment if I have not understood your problem correctly.

answer Aug 13, 2014 by Salil Agrawal
...