LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Memory allocation for fork (https://www.linuxquestions.org/questions/programming-9/memory-allocation-for-fork-789458/)

karthikg4u 02-16-2010 06:39 AM

Memory allocation for fork
 
HI All,

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?

hostmaster 02-16-2010 08:25 AM

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.

karthikg4u 02-16-2010 09:58 AM

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 ?

johnsfine 02-16-2010 10:09 AM

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.

Quakeboy02 02-16-2010 11:34 AM

Sorry. Never mind.

jschiwal 02-16-2010 11:51 AM

Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.

karthikg4u 02-16-2010 10:32 PM

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).

johnsfine 02-17-2010 09:41 AM

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.

karthikg4u 02-17-2010 10:42 PM

can i have a small program for above comments,so that the parent and child (variables) will be holding a different address.

johnsfine 02-18-2010 06:51 AM

Quote:

Originally Posted by karthikg4u (Post 3867647)
can i have a small program for above comments,so that the parent and child (variables) will be holding a different address.

Why?

I'm not sure what you want and I suspect you still have some basic misunderstanding about virtual memory that is motivating that request.

The physical memory is different as soon as you write to the variable. Why do you want the virtual address to be different?

karthikg4u 02-18-2010 09:17 AM

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 ?

johnsfine 02-18-2010 09:22 AM

Quote:

Originally Posted by karthikg4u (Post 3868182)
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.


All times are GMT -5. The time now is 05:28 AM.