From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fa528,689ead9f7415a732 X-Google-Attributes: gidfa528,public X-Google-ArrivalTime: 2002-08-30 11:05:00 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!logbridge.uoregon.edu!snoopy.risq.qc.ca!newsfeed.news2me.com!newsfeed2.earthlink.net!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread1.prod.itd.earthlink.net.POSTED!not-for-mail Newsgroups: comp.unix.tru64 Subject: Re: fork() problem References: From: Paul Pluzhnikov Message-ID: User-Agent: Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.4 (Artificial Intelligence) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Fri, 30 Aug 2002 18:04:26 GMT NNTP-Posting-Host: 24.205.78.167 X-Complaints-To: abuse@earthlink.net X-Trace: newsread1.prod.itd.earthlink.net 1030730666 24.205.78.167 (Fri, 30 Aug 2002 11:04:26 PDT) NNTP-Posting-Date: Fri, 30 Aug 2002 11:04:26 PDT Organization: EarthLink Inc. -- http://www.EarthLink.net Xref: archiver1.google.com comp.unix.tru64:8534 Roman Prokhorov writes: > > To understand the purpose of pthread_atfork, recall that > > fork(2) duplicates the whole memory space, including mutexes in > > their current locking state, but only the calling thread: other > > threads are not running in the child process. Thus, if a mutex > > is locked by a thread other than the thread calling fork, that > > mutex will remain locked forever in the child process, possibly > > Yes, but it becomes locked in parent process too !!! It will be unlocked in the parent process as soon as fork() is done. > > > blocking the execution of the child process. To avoid this, > > install handlers with pthread_atfork as follows: the prepare handler > > locks the global mutexes (in locking order), and the parent > > and child handlers unlock them (in reverse order). Alternatively, > > prepare and parent can be set to NULL and child to a function that > > calls pthread_mutex_init on the global mutexes. > > > > Apparently Tru64 choose the first alternative ;-( > > > > Cheers, > > OK, what mutexes are locked by fgets? Are they documented anywhere? It is unlikely that fgets needs other mutexes (it may need malloc mutex at times). It is also unlikely that these mutexes are documented anywhere. Even if they were, you are missing the point: > How to make it to create all the files > _while_ it's waiting for the input? which is that you *can't* reliably fork() multithreaded programs, unless you wrote *all* the code [including libc]. Since you did not write libc [and I hope you don't plan on it ;-], you just can't fork(). Accept this fact, and move on to one of the other solutions. Cheers, -- In order to understand recursion you must first understand recursion.