Usenet comp.unix.tru64 (2000-2013)
 help / color / mirror / Atom feed
From: Paul Pluzhnikov <ppluzhnikov@earthlink.net>
Subject: Re: fork() problem
Date: Fri, 30 Aug 2002 00:05:00 GMT	[thread overview]
Message-ID: <m33csxmeu0.fsf@salmon.parasoft.com> (raw)
In-Reply-To: roma-25B57C.19000129082002@news.gamma.ru

Roman Prokhorov <roma@stalker.gamma.ru> 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.


  reply	other threads:[~2002-08-30  0:05 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-28 23:13 fork() problem Roman Prokhorov
2002-08-29  4:04 ` Paul Pluzhnikov
2002-08-29 15:00   ` Roman Prokhorov
2002-08-30  0:05     ` Paul Pluzhnikov [this message]
2002-08-30 12:28       ` Roman Prokhorov
2002-08-30 15:04         ` Paul Pluzhnikov
2002-08-30 16:41           ` Roman Prokhorov
2002-08-30 18:04             ` Paul Pluzhnikov
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox