fork()

Hrvoje Niksic hniksic at srce.hr
Mon Jun 7 20:48:15 CEST 1999


Arne Mueller <a.mueller at icrf.icnet.uk> writes:

> > If you don't change the dictionary, the memory will not be copied.
[...]
> Huh, that's realy good news! But I don't understand how that works,

It works by MMU (memory management unit) magic.  The OS doesn't really 
"watch" the system activity -- it's more that when the child writes to 
one of the pages in question, a trap is invoked which copies the page
and remaps the child's memory address to point to the copy.

> However my children don't change anything in the dictionary ;-)

Then the memory should be kept.  You can test this by running a script 
like this:

    import os, time

    largestring = "x" * 10000000

    for i in range(50):
      if os.fork() != 0:
        time.sleep(60)
        os._exit(0)

    time.sleep(60)

This creates fifty processes, each of which sleep a minute and exit,
and their parent does the same.  Each process can access a 10M string.
Needless to say, I don't have 500M virtual memory on my Linux box, so
copy-on-write definitely works here.  Also, `free' reports:

{pc-hrvoje}[~]$ free
             total       used       free     shared    buffers     cached
Mem:         63272      62572        700     534736         88       9844
-/+ buffers/cache:      52640      10632
Swap:       120924      22112      98812

Of 534M memory shared between the processes (under the "shared"
column), about 500 comes from the 50 python processes.




More information about the Python-list mailing list