Question about D, garbage collection and fork()

Lionello Lunesu lio at lunesu.remove.com
Thu Mar 10 06:13:16 PST 2011


On 10-3-2011 6:56, Jerry Quinn wrote:
> Where I work, we find it very useful to start a process, load data, then fork() to parallelize.  Our data is large, such that we'd run out of memory  trying to run a complete copy on each core.  Once the process is loaded, we don't need that much writable memory, so fork is appealing to share the loaded pages.  It's possible to use mmap for some of the data, but inconvenient for other data, even though it's read-only at runtime.
>
> So here's my question:  In D, if I create a lot of data in the garbage-collected heap that will be read-only, then fork the process, will I get the benefit of the operating system's copy-on-write and only use a small amount of additional memory per process?
>
> In case you're wondering why I wouldn't use threading, one argument is that if you have a bug and the process crashes, you only lose one process instead of N threads.  That's actually useful for robustness.
>
> Thoughts?
>
>

D's try-catch will catch all errors, even access violations and stack 
overflow:

import std.stdio;
void so() {
   so();
}
void main() {
   try {
     so();
   }
   catch {
   }
   writeln("graceful exit");
}

By wrapping each thread's code in try-catch you can handle each thread 
going down. Of course, a thread can still corrupt the memory of another 
thread. To share memory between processes you'd have to use an OS 
specific API. On Windows you'd use a file mapping.

L.


More information about the Digitalmars-d mailing list