[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Forking with Threads



> From: "Greg Cannon" <gcannon@xxxxxxx.net>
> Date: Thu, 13 Sep 2001 11:34:50 -0400

> I am having difficulty getting a fork call to work within a thread.
> The code compiles and works fine on development host system.
> The code cross compiles, but fails to run on target system.
> 
> Can anyone explain this to me?

Yes.  It's a simple and stupid bug.  Sorry, both for the delay
and the bug.  The problem is that libc is linked in before
libpthread, and so the functions in libpthread that are supposed
to override the libc ones, don't get called.  The reason there
are functions in libpthread overriding ones in libc is that they
need to provide an thread-exclusive region around kernel calls
and access to process-specific data, so that only one thread at
a time pokes into it.  (Yes, that was a simplification, but
let's keep it simple.)  This matters when multiple threads call
standard functions, such as in your example.  Though unless such
functions are called frequently, you'd be unlucky to get bit by
it.  The fork-overrider (glibc/linuxthreads/ptfork.c) performs
complicated enough lock-handling that it always gets bit by this
bug.  With the link order fixed, I now get the expected:

HAPPY CHILD
WAITING PARENT
HAPPY PARENT
HAPPY THREAD
WAITING PARENT
HAPPY CHILD
HAPPY PARENT
HAPPY MAIN

When comparing against host output, note that you get slightly
different output (ordering and multiplicity) depending on libc
version and whether you redirect output to a file and such.

While you're waiting for the new release, I suggest applying the
following patch, which fixes the problem.  It just changes the
ordering of -lc and -lpthread.  Expect a new release within
days; I'll commence release work immediately.

--- specs	Wed Oct 24 00:29:48 2001
+++ /usr/local/cris/lib/gcc-lib/cris/2.96/specs	Wed Oct 24 00:30:52 2001
@@ -20,7 +20,7 @@
 %{sim2:%{!T*:-Tdata 0x4000000 -Tbss 0x8000000}}  %{melinux:-Ur -d %{!shlib:%{!symbolic:-Bstatic}}}  %{melinux-stacksize=*:-defsym __Stacksize=%*}  %{!mlinux:%{!melinux:%{!maout|melf: -mcriself}}%{melinux|maout: -mcrisaout}}  %{mlinux:-mcrislinux -rpath-link linux%s     %{shared} %{static}    %{!shared:%{!static:%{rdynamic:-export-dynamic}}}}  %{O2|O3:%{!maout|melf:%{!melinux: --gc-sections}}}  %{symbolic:-Bdynamic} %{shlib:-Bdynamic} %{static:-Bstatic}  %{v:--verbose}
 
 *lib:
-%{melinux:-lc -lsyslinux -lc -lsyslinux -lic}  %{mlinux:-lc %{!shared:%{pthread:-lpthread}}}  %{!melinux:   %{!mlinux:%{sim*:-lc -lsyssim -lc -lsyssim}    %{!sim*:%{g*:-lg}      %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p} -lbsp}    -lnosys}}
+%{melinux:-lc -lsyslinux -lc -lsyslinux -lic}  %{mlinux:%{!shared:%{pthread:-lpthread}} -lc}  %{!melinux:   %{!mlinux:%{sim*:-lc -lsyssim -lc -lsyssim}    %{!sim*:%{g*:-lg}      %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p} -lbsp}    -lnosys}}
 
 *libgcc:
 -lgcc

brgds, H-P