LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-16-2010, 06:39 AM   #1
karthikg4u
LQ Newbie
 
Registered: Feb 2010
Posts: 5

Rep: Reputation: 0
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?
 
Old 02-16-2010, 08:25 AM   #2
hostmaster
Member
 
Registered: Feb 2007
Posts: 55

Rep: Reputation: 17
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.
 
Old 02-16-2010, 09:58 AM   #3
karthikg4u
LQ Newbie
 
Registered: Feb 2010
Posts: 5

Original Poster
Rep: Reputation: 0
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 ?
 
Old 02-16-2010, 10:09 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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.
 
Old 02-16-2010, 11:34 AM   #5
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Linux 11 (Bullseye)
Posts: 3,407

Rep: Reputation: 141Reputation: 141
Sorry. Never mind.

Last edited by Quakeboy02; 02-16-2010 at 11:35 AM.
 
Old 02-16-2010, 11:51 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 02-16-2010, 10:32 PM   #7
karthikg4u
LQ Newbie
 
Registered: Feb 2010
Posts: 5

Original Poster
Rep: Reputation: 0
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).
 
Old 02-17-2010, 09:41 AM   #8
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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.
 
Old 02-17-2010, 10:42 PM   #9
karthikg4u
LQ Newbie
 
Registered: Feb 2010
Posts: 5

Original Poster
Rep: Reputation: 0
can i have a small program for above comments,so that the parent and child (variables) will be holding a different address.
 
Old 02-18-2010, 06:51 AM   #10
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by karthikg4u View Post
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?

Last edited by johnsfine; 02-18-2010 at 08:18 AM.
 
Old 02-18-2010, 09:17 AM   #11
karthikg4u
LQ Newbie
 
Registered: Feb 2010
Posts: 5

Original Poster
Rep: Reputation: 0
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 ?
 
Old 02-18-2010, 09:22 AM   #12
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by karthikg4u View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory allocation morfeus80 Linux - Newbie 5 01-28-2008 02:51 PM
Help - memory allocation in C zaichik Programming 3 09-04-2005 10:16 AM
memory allocation docGonzo2000 Linux - General 1 05-16-2003 09:24 PM
memory allocation docGonzo2000 Linux - General 1 05-16-2003 09:22 PM
memory allocation. raven Programming 5 09-08-2002 01:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration