fork(2) makes an _entire_ copy of the parent - data elements and all. 
This is very inconvenient for things that use alot of memory.

For things like daemons and whatnot the general practice is to fork the
children before the data space is allocated.  If you need to share the
same data elements between the parent and the child procs, try using
shared mem (for your example).

hope this helps


On Fri, 10 Nov 2000 15:28:33 +0700 (ICT) Anon Sricharoenchai
<[EMAIL PROTECTED]> writes:
> 
> I have tested the "fork" and so called "copy-on-write" operation by
> invoking the following command (as a normal user, not superuser).
> 
> perl -e '$| = 1; sub s1() { system("free -t | tail -1") } s1(); for 
> ($i =
> 0; $i<10_000_000; $i++) { $a[$i] = 0 } s1(); if (\!fork()) { s1(); 
> for ($i
> = 0; $i<scalar(@a); $i+=300) { $a[$i] = 0 } s1() } else { wait() }'
> 
> First of all, suppose that my machine has 300M of free memory.
> Then I allocate array of 10,000,000 integer elements, that will eat
> memory about 200M.  Now, the amount of free memory is 100M.
> Then I fork() 1 time, and let the child process to write on the 
> allocated
> array to force the "copy-on-write" operation.
> But the free memory is not enough to "copy-on-write" the whole of
> allocated array.  So, the system will run out of memory and crash.
> And I can't find any method to recover my system without reseting 
> the
> hardware.
> 
> How can I protect this crash ?
> 
> 
> ps. The testing time may be so long.  So, the "swapoff -a" command 
> may be
>     used to short the time.
> 
> 

-----BEGIN GEEK CODE BLOCK-----
Version 3.12  (see http://www.geekcode.com/geek.html for details)
GCS d- s: a C++$ ULAHS++++$ P+++$ L+++>++++$ E++>++++ W+(-) N+ o K? w---
O- !M-- V- PS+ PE+ Y+ PGP t 5- X- R- tv-->! b++ DI++ D+ G+ e++>++++ h* r
y++++
------END GEEK CODE BLOCK------

Reply via email to