<br><br><div class="gmail_quote">2009/2/26 Török Edwin <span dir="ltr"><<a href="mailto:edwintorok@gmail.com">edwintorok@gmail.com</a>></span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
What do you do if the program is multi-threaded? fork()-ing a<br>
multithreaded program can lead to problems ...<br>
The functions you can call after forking is very limited according to<br>
POSIX: "If a multi-threaded process calls /fork/(), the new process<br>
shall contain a replica of the calling thread and its entire address<br>
space, possibly including the states of mutexes and other resources.<br>
Consequently, to avoid errors, the child process may only execute<br>
async-signal-safe operations until such time as one of the /exec<br>
<<a href="http://www.opengroup.org/onlinepubs/9699919799/functions/exec.html" target="_blank">http://www.opengroup.org/onlinepubs/9699919799/functions/exec.html</a>>/<br>
functions is called."</blockquote><div><br>Well, I have alread thought a little bit about this problem.<br>If I remember right only the thread calling fork will remain active in the child process. Thats exactly what we want.<br>
In pseudocode it could look like this.<br><br>Garbage collection thread:<br> shared_mem = create_shared_memory()<br>
loop:<br> wait_for_garbage_collection_signal() # wait until a garbage collection is requeted<br> pause_all_other_threads() # stop the world<br> root_pointers = get_root_pointers_from_all_threads()<br>
resume_all_other_threads()<br> child_pid = fork()<br> if(!child_pid):<br> # child process - all threads except of the current one has been stopped by fork()<br> collect_garbage(root_pointers, shared_memory) # collect unreferenced memory blocks, put the result in shared_memory<br>
exit()<br> else:<br>
wait_for_process_exit(child_pid)<br> free_memory_blocks(shared_mem) # free all memory blocks the GC has found<br> <br>=> There is probably no problem with mutexes and other synchronization primitieves, because we simply do not care in the garbage collection thread<br>
<br>But may be I'm missing something.<br><br>Regards,<br>Ralf<br></div></div>