ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I wrote one program to find out how the memory is allocated for parent and child process.
my program as follows:-
#include <stdio.h>
#include <sys/types.h>
#define MAX_COUNT 200
void ChildProcess(void); /* child process prototype */
void ParentProcess(void); /* parent process prototype */
void main(void)
{
pid_t pid;
int a = 7;
printf( " value of &a a ,%p,%d \n",&a,a);
int *p = NULL;
p = (int*)malloc(10);
pid = fork();
if (pid == 0) {
ChildProcess();
printf( "value of &a a ,%p,%d \n",&a,a);
++a;
printf( "value of &a a ,%p,%d \n",&a,a);
printf( "child value of &a a ,%p,%d ->%p\n",&a,a,p);
*p = 10;
printf("child value for *p = %d\n",*p);
sleep(1);
printf("child value for p %p->*p = %d\n",p,*p);
}
else{
ParentProcess();
printf( "value of &a a ,%p,%d \n",&a,a);
--a;
printf( "vlaue of &a a ,%p,%d \n",&a,a);
printf(" parrent value of a %p %d->%p\n",&a,a,p);
*p = 100;
printf("parent value for p %p---> *p = %d\n",&a,*p);
sleep(1);
printf("parent value for p %p-->*p = %d\n",p,*p);
}
}
void ChildProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++);
printf(" *** Child process is done ***\n");
}
void ParentProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++);
printf("*** Parent is done ***\n");
}
output of this prg:-
value of &a a ,0xbfe43404,7
*** Parent is done ***
value of &a a ,0xbfe43404,7
value of &a a ,0xbfe43404,6
parrent value of a 0xbfe43404 6->0x804b008
parent value for p 0xbfe43404---> *p = 100
*** Child process is done ***
value of &a a ,0xbfe43404,7
value of &a a ,0xbfe43404,8
child value of &a a ,0xbfe43404,8 ->0x804b008
child value for *p = 10
parent value for p 0x804b008-->*p = 100
child value for p0x804b008->*p = 10
My question is how the parent and child are having the same address,
irrespective of value are changes and how come the values are overwritten?
Please read below, I think this would help you understand.
A feature known as “copy-on-write” is frequently used in modern OSes like Linux.
The mappings between virtual and physical memory are duplicated for the new process,
but the new mappings are marked as read-only. When the process tries to write to these
memory blocks, the exception handler allocates a new block of memory, copies the data
to the new block, changes the mapping to point to the new block with write access, and
then resumes the execution of the program. This feature reduces the overhead of forking
a new process.
So according to the feature(COW) when we tries to access the data(i.e writing any data to that variable) two copies will be created ( for child as well as parent).
But when i tried to access the data i.e incrementing the variable in child and decrementing the same variable in parent, it worked properly .So i printed the address of the same variable before and after my access,its printing the same address.
So my question is, how it could be able to print the same address before and after my access,how the complier differentiate the variables ?
The COW is just an optimization. It makes what sounds like an expensive behavior less expensive. But the original question was about the behavior itself.
Every process has its own address space, so an address in one process is generally not the same physical memory as that same address in another process.
When you fork, the defined behavior is to create an entirely new address space for the child, which begins as a copy of the parent's address space. So any reasonable test you might construct should act the same as it would have acted if Linux had actually created that entire copy at the moment of the fork.
so after i access the variable it should create a new address space for child.But when i printed the address of the variable and content of the variable its printing the same address for both the variable and printing the relevant content value for the variables.
How come parent and child variable having the same address after i access the variable ?(how its differentiated).
Every process always has a layer of mapping between it and physical memory.
Any address you use in your program is a virtual address. The mapping translates each virtual address into a physical address each time it is used.
Each process has its own mapping tables, so a virtual address in one process is generally not the same physical memory as the same virtual address in a different process.
Please correct me if i am wrong,Same physical memory can have two virtual address(this can take place in Page table entry),but how same virtual address will map to two different Physical memory.if so, where this transition takes place ?
how same virtual address will map to two different Physical memory
Every process has its own page tables.
When the kernel switches control from one process to another, it switches the hardware register that points to the root top of the page table structure.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.