003 Linux System ProgrammingIssues, queries and suggestions related to Linux, Linux Programming, IPC, Interprocess Communication, Synchronization, Semaphore, System Programming, Linux Software Development.
A.During fork() system call,the child process don't immediately gets separate memory space,it shares memory (global and local variables) with the parent process.But if the child process tries to overwrite the memory, it gets separate memory page which is exact replica of parent process and thus modyfying the contents doesn't affect the parent process memory.This is called copy-on-write.
MMU concept->The process of getting separate page for the child process is done by kernel page fault handler, after getting page fault signal generated by CPU when child tries to overwrite the memory which is not yet mapped.
Initially both the parent process and the child process share same process context. Even if a parent process has 'n' number of child processes ,each child process will have a pointer to that same block of memory(Process context of the parent). So from process point of view each has its own memory. As soon as a process overwrites instructions (like through 'exec' family of system calls), kernel transparently creates a copy of that memory and gives it to that process.
This technique is quite useful since it prevents unnecessary coping of process context of parent for child process.(Since both are same) .This copying happens only when there is alteration in data i.e instructions in memory are overwritten by child process( like using 'execl' system call that loads a binary program image into the memory). Thus after executing system calls like 'execl' both parent and child processes have different process contexts.
Also if child process tries to alter some variables then also a copy is created and it appears to us that a single variable is holding two different values(one for parent and other for child).