Subj : Re: pthreads and fork To : comp.programming.threads From : Marcin 'Qrczak' Kowalczyk Date : Sat Feb 19 2005 06:44 pm Alexander Terekhov writes: >> The threads are under my control, as are fork calls, and I can >> synchronize whatever I need. But in order to implement anything which >> calls fork while more than one thread is running, I must know what >> happens with resources associated with pthread_t in the child process. > > Child process shall be an exact copy of the parent process except... > (see the spec). No user stuff cleanup (apart from the stuff you may > have in atfork() handlers) or any modifications affecting *user's* > state (apart from "except...") is done be the implementation. I don't mean *user* resources, because I control that. I mean the resources which are released directly by pthread_join (excluding cleanup handlers and TLS descructors, which I know are not run). The only way to free them, if they should be fred explicitly, is to call pthread_join. I don't know whether calling pthread_join on a thread which died because of fork is legal. Single Unix Spec should not have been silent about this. It should either say that pthread_join must be used to free resources, or that pthread_join must not be used, or that this interaction is not supported at all and there is no way to prevent a memory leak (unless the program calls exec soon, and thus the memory leak is temporary and doesn't matter, or had only one thread, and thus there is nothing to leak). What happens with detached threads? There are only two choices here: either their resources are properly deallocated (excluding cleanup handlers and TLS descructors), or there is a memory leak. Making pthread_join allowed would be logical, because data associated with threads which have already terminated is probably not touched by fork at all. Since pthread_join on Linux doesn't crash, and it returns 0 if and only if it executes an internal call of __free_tcb(), maybe the situation is supported at least on Linux. Maybe if I check at configure time whether pthread_join on such thread doesn't crash and succeeds, I can use it. Are there systems where it works sometimes but is not generally safe? -- __("< Marcin Kowalczyk \__/ qrczak@knm.org.pl ^^ http://qrnik.knm.org.pl/~qrczak/ .