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 05:29:02 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!newsfeed.gamma.ru!srcc!Gamma.RU!roma From: Roman Prokhorov Newsgroups: comp.unix.tru64 Subject: Re: fork() problem Date: Fri, 30 Aug 2002 16:28:59 +0400 Organization: Gamma NNTP server Moscow Russia Message-ID: References: NNTP-Posting-Host: roma.ttk.ru X-Trace: news.gamma.ru 1030710539 681 195.170.229.76 (30 Aug 2002 12:28:59 GMT) X-Complaints-To: abuse@gamma.ru NNTP-Posting-Date: Fri, 30 Aug 2002 12:28:59 +0000 (UTC) Keywords: 318629621 Posted-And-Mailed: yes User-Agent: MT-NewsWatcher/3.1 (PPC) Xref: archiver1.google.com comp.unix.tru64:8526 In article , Paul Pluzhnikov wrote: > 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 Still don't beleive it. Why fork() checks the stdio locks? It doesn't print anything by itself. > #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. My initial task was launching an external program from a thread while the main thread stays in fgets(). I uses system() call but it didn't work under Tru64. So I tried o emulate its behaviour via fork/exec and found that fork() is the source of the problem. -- Roman