For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > May 2004 > How does fork handle Memory?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How does fork handle Memory?
Kok Fong

2004-05-17, 2:31 am

Hi,

I'm new to linux programming and would like to ask some question. I'm using
the fork api and discovered that when I do a fork on unix in genereal the
memory address is not shared between the parent and the child. Take the code
below for instance:

int *testVar;

int main (void) {
int pid;
testVar = (int *) malloc (sizeof (int));
*testVar = 0;
printf ("%d [%d] Main \n", *testVar, testVar);

if (pid == 0) {
*testVar = 10;
printf ("%d [%d] Child \n", *testVar, testVar);
return 1;
}
while (r_wait(NULL) > 0) ; /* Just wait for the child to complete */

printf ("%d [%d] Parent \n", *testVar, testVar);
exit (0);
}


What I get on the platforms that I tested are that though the memory address
is the same. However the pointer in the parent and child are pointing to
different areas in memory.

For instance I get

Main 0 [133904]
Child 10 [133904]
Parent 10 [133904]

This implies that in the parent and child though the pointer is has value
[133904] they are not pointing to the same location in memory. Is there an
explanation for this?

My other question is that let's say I have a lot of memory structure that I
have allocated dynamically using the malloc. After those structures were
allocated, I forked out some child and use the execve function to execute
some shell or binary files. Do I need to free up the memory structures using
free in both child and parent?


mikegw

2004-05-17, 2:31 am


"Kok Fong" <jammerie@o12.org> wrote in message
news:40a848db$1@news.starhub.net.sg...
> Hi,
>
> I'm new to linux programming and would like to ask some question. I'm

using
> the fork api and discovered that when I do a fork on unix in genereal the
> memory address is not shared between the parent and the child. Take the

code
> below for instance:
>

{snip}
> This implies that in the parent and child though the pointer is has value
> [133904] they are not pointing to the same location in memory. Is there an
> explanation for this?


Upon the execution of a fork, the memory is a copy on write. This means that
until either process tries to write they use the same memory, it is done
this way to speed things up, you don't need to copy pages of memory until
you have to.

Each new forked process will then occupy its own memory space. The child
will get a copy of the parent, but after the fork neither process can affect
the others memory space. To achieve shared memory there are a few options
depending on how much data you need to shift about the place. The all fall
under the title of IPC inter-process communication. For a small amount of
data you can use a fifo pipe, larger amounts you might have to use sysV
shared memory.

Hope this helps

Mike


Darko M.

2004-05-17, 8:31 am

"mikegw" <mikegw20@hotmail.spammers.must.die.com> wrote in message news:<c89ife$evn$1@tomahawk.unsw.edu.au>...
> "Kok Fong" <jammerie@o12.org> wrote in message
> news:40a848db$1@news.starhub.net.sg...
> using
> code
> {snip}
They share the same program text, so the pointer values are the same.
However, you don't have access to some sort of "global" memory, but
every process has it's own memory space. After you do fork(), the
process duplicates, so the new process gets it's own memory space. As
told above, since nothing is copied until you use it, you don't have
to free (delete) any heap space if you immediately do execXX function.
[color=darkred]
>
> Mike


If you need to share the same memory, you probably need threads (eg.
pthreads), rather than fork() + shared memory.
Kok Fong

2004-05-18, 12:31 am

Hi Mike, Darko,

thanks for your answers. It really clear up a lot of questions.

> If you need to share the same memory, you probably need threads (eg.
> pthreads), rather than fork() + shared memory.


I tried to create five pthreads and use exec() in them. However only one
thread managed to exec (). I suspect that each process can exec () once.
Hence I must use fork to create a new process. This is just something I
suspect.

Thanks for your help once again.


Barry Margolin

2004-05-18, 2:31 am

In article <40a982cd$1@news.starhub.net.sg>,
"Kok Fong" <jammerie@o12.org> wrote:

> Hi Mike, Darko,
>
> thanks for your answers. It really clear up a lot of questions.
>
>
> I tried to create five pthreads and use exec() in them. However only one
> thread managed to exec (). I suspect that each process can exec () once.
> Hence I must use fork to create a new process. This is just something I
> suspect.


Exec() overwrites the memory of the current process with the new program
specified. Since all threads are part of the same original process,
they're all overwritten.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
rohit

2004-05-18, 3:31 am

> My other question is that let's say I have a lot of memory structure that I
> have allocated dynamically using the malloc. After those structures were
> allocated, I forked out some child and use the execve function to execute
> some shell or binary files. Do I need to free up the memory structures using
> free in both child and parent?


After a fork the dynamically allocated memory are present as separate
distinct copies in both the parent and the child.If te child dosent
free it is a memory leak until the child terminates or calls exec.The
same is true for the parent too.

As in your case you do a execve after the fork in the child process,
so you dont need to explicitly free up the memory.

regards
rohit
Pramod Sharma

2004-05-18, 5:31 am

"Kok Fong" <jammerie@o12.org> wrote in message news:<40a848db$1@news.starhub.net.sg>...
> Hi,
>
> I'm new to linux programming and would like to ask some question. I'm using
> the fork api and discovered that when I do a fork on unix in genereal the
> memory address is not shared between the parent and the child. Take the code
> below for instance:
>
> int *testVar;
>
> int main (void) {
> int pid;
> testVar = (int *) malloc (sizeof (int));
> *testVar = 0;
> printf ("%d [%d] Main \n", *testVar, testVar);
>


BUT WHERE IS THE FORK()????????????????????
oviously they will be pointing to same memory and same value as there is no
Parent child relationship.....

> if (pid == 0) {
> *testVar = 10;
> printf ("%d [%d] Child \n", *testVar, testVar);
> return 1;
> }
> while (r_wait(NULL) > 0) ; /* Just wait for the child to complete */
>
> printf ("%d [%d] Parent \n", *testVar, testVar);
> exit (0);
> }
>
>
> What I get on the platforms that I tested are that though the memory address
> is the same. However the pointer in the parent and child are pointing to
> different areas in memory.
>
> For instance I get
>
> Main 0 [133904]
> Child 10 [133904]
> Parent 10 [133904]
>
> This implies that in the parent and child though the pointer is has value
> [133904] they are not pointing to the same location in memory. Is there an
> explanation for this?
>
> My other question is that let's say I have a lot of memory structure that I
> have allocated dynamically using the malloc. After those structures were
> allocated, I forked out some child and use the execve function to execute
> some shell or binary files. Do I need to free up the memory structures using
> free in both child and parent?

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com