On Wed, Feb 28, 2001 at 12:23:35PM -0800, Living Dead <[EMAIL PROTECTED]> wrote:
| I have the following code: 
| 
| #define BUFFSIZE 32 * 1024 * 1024 
| 
| int main() 
| { 
|   char *a; 
|   a = (char *)malloc(BUFFSIZE); 
|   if (fork()) { 
|     // Parent 
|    free(a); 
|   } else { 
|     // Child 
|   } 
| } 
| 
| The question is: 
| Is there a risk to run out of memory? 
| 
| To put it in another way: 
| If I allocate 32Mb of memory in the parent process,
| then I call fork(), the amount of memory needed for
| both parent and child is 64Mb? 

Potentially.

Initially, no. Fork() makes the child an _exactly_ duplicate of the parent
except for its pid and ppid.

Since they're identical, all the child pages are marked copy-on-write
in the memory subsystem. Which means you initially on have 32Mb of real
memory in use. In either process modifies memory, that page is copied by
the subsystem and only then do parent and child have two pages (one each).

Cheers,
-- 
Cameron Simpson, DoD#743        [EMAIL PROTECTED]    http://www.zip.com.au/~cs/

You are just paranoid, and all your friends think so too.
        - James Joseph Dominguez <[EMAIL PROTECTED]>



_______________________________________________
Redhat-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to