When we create a new process
(child process) using fork() system call then new process have its
own process control block(PCB) but shares process context of parent
process(calling process).
Both process(Parent and Child)
shares process context as read only. When any of both process want to
modify any data value then that particular segment split into two
frames one for parent process and other for child process but the
logical address of both segments will be same because in process
context both the segments are on same logical address.
Processor decides which
segment is used for which process.
A local variable stores in stack segment of process context,
process context is sharable between parent to child process, it means when we initialize
a local variable with some value in parent process and after that we initialize
again that variable with some value in child process then value should be over
written, but no! This will not happen,
Actually process context shared between processes in read-mode
and when a process wants to change any value then an exception will occur and OS
will allocate memory for that individual variable at another location for child
process means allot another physical address. It means we are accessing two
different variables in different processes. Now, but logical address will be
the same for both variable.
Now the question is how different variable can return the same
logical address?
Because, at the time of assignment, stack segment split into two
frame. One for parent process and newly created frame for child process and
when we fetch value from variable then, parent process will take value from
parent occupied stack frame and child process will take value from child
occupied stack frame but they placed in same line
Now understand with example suppose there is a matrix where,
consider frame as column and logical address as row and after splitting there
are two columns in single row so that’s why both frames has same logical address.
COPY ON WRITE:
When in code segment of a process context fork() system call is called then
fork() system call creates a duplicate process known as child process.
This child process shares the same process context as of its parent process(from where it generated).It does not have process context of its own.
The child process has its own TS(it contains pid ppid of the child process) as parent process .
fork() system call is executed as the first command of the child process and returns 0 and it simultaneously execute the rest of the code same as the parent process.
For example if the range of pc value is 1000 to 2000 for code segment and at 1500 the fork () system call is called then the range of pc(process context) in code segment for child will be from 1500 to 2000.
The pid(process id) of child will be next if available ie if pid of parent is 100 then pid of child will be 101 and ppid(parent process id) of child will be pid of parent.
For every process a stack is created in stack segment and a stack frame is also created .If the process wants to change value of a variable then, OS allocates another memory at another physical address but there logical address will be same so it seems as if same variable have two different values which is not possible,rather they have different physical address and same logical address (pointer stores logical address therefore if we print address of variable then we get same value of address). This whole process is COPY ON WRITE.
When ever a process is in execution mode, a process context is created. All variables that are
in a scope says LOCAL VARIABLE stores in stack segment of process context. When we create a
new process by using fork() it become the child process of that parent process. Child process share
the same process context of parent process. Parent process has its own PID and PPID so as child
process a has its own PID and PPID. The PPID of parent process would be the PID of child process.
Logical address of both parent and child would be same but the physical address of both parent and
child would be different. When we initialise a local variable in parent process and trying to access the
same variable in child process the value of that local variable won’t be updated because the physical
addresses of both process is different.
Because parent and child process shares the same process context in read-mode so when a
process want
to update the value of local variable OS allots memory for individual variable and
allocate different physical address that means we accessing two different variable from different
process and from different physical address. The pid of the parent process becomes ppid of child process
and child pid becomes the next number if available.
For example
int num = 10; //For parent process
int num = 12; //For child process
When we create a child process using fork() call the value for num would be the same for
parent process that is ‘10’ and ‘12’ for child process as mentioned above.
There is a 99% chances the return value of function fork() is equal to ‘0’ ie fork() == 0 and it is
happen when child process starts execution.
The physical address of the parent(x) and child process(y) is different but the logical(z)
address of both process is different.
So num would be store at different logical address. When ever we try to access or update
num from both process with different physical address then it won’t overwritten by updated value.
Address for parent process = x + z (num = 10)
Address for child process = y + z (num = 12)
So this is how COPY ON WRITE works
It looks like you're new here. If you want to get involved, click one of these buttons!