Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

According to wikipedia (which could be wrong)

When a fork() system call is issued, a copy of all the pages corresponding to the parent process is created, loaded into a separate memory location by the OS for the child process. But this is not needed in certain cases. Consider the case when a child executes an "exec" system call (which is used to execute any executable file from within a C program) or exits very soon after the fork(). When the child is needed just to execute a command for the parent process, there is no need for copying the parent process' pages, since exec replaces the address space of the process which invoked it with the command to be executed.

In such cases, a technique called copy-on-write (COW) is used. With this technique, when a fork occurs, the parent process's pages are not copied for the child process. Instead, the pages are shared between the child and the parent process. Whenever a process (parent or child) modifies a page, a separate copy of that particular page alone is made for that process (parent or child) which performed the modification. This process will then use the newly copied page rather than the shared one in all future references. The other process (the one which did not modify the shared page) continues to use the original copy of the page (which is now no longer shared). This technique is called copy-on-write since the page is copied when some process writes to it.

It seems that when either of the processes tries to write to the page. A new copy of the page gets allocated and assigned to the process that generated the page fault. The original page gets marked writable afterwards.

My question is: what happens if the fork gets called multiple times before any of the processes made an attempt to write to a shared page?

Thanks a lot

share|improve this question
    
Wikipedia is right in this case, just more high level. –  David Kohen Dec 11 '12 at 13:10
add comment

2 Answers

up vote 3 down vote accepted

Nothing particular happens. All processes are sharing the same set of pages and each one gets its own private copy when it wants to modify a page.

share|improve this answer
    
Right. The point is, it's the child process that is special, that has the job of copying if it tries to write to the shared page. Neither the parent nor the other children need know about the change if it is done correctly. –  Charles Stewart Dec 11 '12 at 9:20
1  
The child process is not that special. Both the child and the parent processes have the same set of pages read-only after the fork. As far as these pages are concerned, page handling is symmetrical. –  jlliagre Dec 11 '12 at 12:22
add comment

When the system preforms a fork, usually (this may depend on implementation) it also marks the pages as read-only, and marks the parent process as the master of these pages.
When trying to write to these pages, a page fault occurs and the OS takes over, copying the entire list of pages or just the changed ones (again, dependent on implementation), so the writing process will have a writable copy.
When there are multiple processes forked from the same one, when the "master" process writes to it's memory, the other processes get their equivalent pages copied.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.