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-29 17:04:54 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!logbridge.uoregon.edu!nntp-relay.ihug.net!ihug.co.nz!west.cox.net!cox.net!newsfeed1.earthlink.net!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread2.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 00:05:00 GMT NNTP-Posting-Host: 24.205.78.167 X-Complaints-To: abuse@earthlink.net X-Trace: newsread2.prod.itd.earthlink.net 1030665900 24.205.78.167 (Thu, 29 Aug 2002 17:05:00 PDT) NNTP-Posting-Date: Thu, 29 Aug 2002 17:05:00 PDT Organization: EarthLink Inc. -- http://www.EarthLink.net Xref: archiver1.google.com comp.unix.tru64:8522 Roman Prokhorov writes: > Thanks for the tip about async safe functions. However it didn't help. > > What's wrong now? I don't use printf in the thread at all, but fork() > still doesn't invoke unless I call sleep(). Which is why fork()ing multithreaded executables is a Bad Idea (TM). The second thread is stuck, trying to acquire the stdin lock: >0 0x3ff805b0400 in pthread_mutex_tryrec(...) in /usr/shlib/libpthread.so #1 0x3ff805afe1c in __pthread_mutex_trylock(...) in /usr/shlib/libpthread.so #2 0x3ff801bf1b4 in __flockall(...) in /usr/shlib/libc.so #3 0x3ff801467ec in __libc_locks_reinit(...) in /usr/shlib/libc.so #4 0x3ff800cf740 in __fork(...) in /usr/shlib/libc.so #5 0x120001978 in func(pData=0x0) "forkthread.c":10 #6 0x3ff805c7c78 in __thdBase(...) in /usr/shlib/libpthread.so Who is holding the stdio lock? But the initial thread, of course: >0 0x3ff800cc9c8 in __read(...) in /usr/shlib/libc.so #1 0x3ff8014287c in __read_nc(...) in /usr/shlib/libc.so #2 0x3ff800d05cc in __filbuf(...) in /usr/shlib/libc.so #3 0x3ff800d0780 in fgets(...) in /usr/shlib/libc.so ## this is where the lock was ## most likely acquired #4 0x120001a70 in main() "forkthread.c":30 #5 0x120001888 in __start(...) in a.out So, your choices are: - avoid forking [save yourself and future maintainers some grief] - postpone doing anything in the main thread until you know that the other thread *has already forked* [this is easy to do in your test program, using condition variable; but I suspect in your real app you just want to be able to fork, while other threads are fread()ing] - fork a "slave process" before creating any threads, open a pipe to it. When the main process wants to fork(), have it write a message to the pipe to slave instead. The slave [which does not create any threads ever], can fork any time it wants to. Cheers, -- In order to understand recursion you must first understand recursion.