Question about D, garbage collection and fork()

Steven Schveighoffer schveiguy at yahoo.com
Thu Mar 10 11:48:46 PST 2011


On Thu, 10 Mar 2011 14:44:40 -0500, Jerry Quinn <jlquinn at optonline.net>  
wrote:

> Steven Schveighoffer Wrote:
>
>> Do you know what causes the OS to regard that memory as read-only?   
>> Since
>> fork() is a C system call, and D gets its heap memory the same as any
>> other unix process (brk()), I can't see why it wouldn't work.  As long  
>> as
>> you do the same thing you do in C, I think it will work.
>
> It's not that the OS considers the memory actually read-only.  It uses  
> copy-on-write so the pages will be shared between the processes until  
> one or the other attempts to write to the page.
>
> So if the garbage collector moves things around, it will cause the pages  
> to be copied and unshared.  So my question is really probably whether  
> the garbage collector will tend to dirty shared pages or not.

Some pages are made of bins of smaller blocks.  For example, a page may be  
a set of 16-byte blocks.  In this case, it's entirely possible that both  
process-local and process-shared data can be in the same page.

To get around this, allocate blocks of more than PAGESIZE/2 size.  Then  
use those to contain your read-only data.

The GC stores its metadata in separate pages than the actual data, so you  
don't have to worry about pages being dirtied by the GC (for example  
during garbage collection) even though the data is static.

You also always have the ability to use C malloc if you prefer to avoid GC  
involvement.

-Steve


More information about the Digitalmars-d mailing list