From: Russell Leighton <russ@elegant-software.com>
To: Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Using getpid() often, another way? [was Re: clone() <-> getpid() bug in 2.6?]
Date: Sun, 06 Jun 2004 11:38:44 -0400 [thread overview]
Message-ID: <40C33A84.4060405@elegant-software.com> (raw)
In-Reply-To: <40C32A44.6050101@elegant-software.com>
Linus said he could not imagine a program using getpid() more than a
handful of times...
well, (I am ashamed to admit it) I found this getpid() bug with just
such a program.
Could someone can suggest an alternative to using getpid() for me?
Here's the problem...
I have a library that creates 2 threads using clone().
[NOTE: I can't use pthreads for a variety of reasons, mostly due
to the wacky signal handling rules...it turns out that using clone() is
cleaner for me anyway.]
Some of the code can ONLY be executed in one of the threads
called the 'handler' thread. The reason for that restriction is that
there are
performance, locking and synchronization issues that demand this
constraint.
Entry points to this code are exported for programmers to use.
I currently have a check in these functions similar to:
if ( !INHANDLERCONTEXT(mon) ) barf();
where:
#define INHANDLERCONTEXT(mon) ( (mon)->handler_q.thread->pid == getpid() )
So the questions are:
Given a code library with some exported functions which CAN be
executed outside a particular thread and others that MUST be executed in
a particular thread, how can I efficiently prevent or trap using of
these functions outside the proper execution context?
Would gettid() be any better?
Thx
Russ
Russell Leighton wrote:
>
> I have read the discussion on this issue and I wanted to get
> confirmation of my understanding...
>
> Issue:
> It seems glibc is caching getpid() which is wrong and breaks
> programs like mine.
>
> You are using an older version of glibc than I and that is why you
> could run the test program.
>
> Given the above, that means that:
> Upgrading my kernel on the FedoraCore2 system won't help because
> the bug is in glibc
>
> Assuming that this is a "new" feature of glibc, any others
> upgrading would then starting seeing this bug
>
> Linus Torvalds wrote:
>
>> On Sat, 5 Jun 2004, Russell Leighton wrote:
>>
>>
>>> I have a test program (see attached) that shows what looks like a
>>> bug in 2.6.5-1.358 (FedoraCore2)...and breaks my program :(
>>>
>>> In summary, I am doing:
>>>
>>> clone(run_thread, stack + sizeof(stack) -1,
>>> CLONE_FS|CLONE_FILES|CLONE_VM|SIGCHLD, NULL))
>>>
>>> According to the man page the child process should have its own pid
>>> as returned by getpid()...much like fork().
>>>
>>> In 2.6 the child receives the parent's pid from getpid(), while 2.4
>>> works as documented:
>>>
>>> In 2.4 the test program does:
>>> parent pid: 26647
>>> clone returned pid: 26648
>>> thread reported pid: 26648
>>>
>>> In 2.6 the test program does:
>>> parent pid: 16665
>>> thread reported pid: 16665
>>> clone returned pid: 16666
>>>
>>
>>
>> Hmm.. The above is the correct behaviour if you use CLONE_THREAD
>> ("getpid()" will then return the _thread_ ID), but it shouldn't
>> happen without that. And clearly you don't have it set.
>>
>> And indeed, it doesn't happen for me on my system:
>>
>> parent pid: 13552
>> thread reported pid: 13553
>> clone returned pid: 13553
>>
>> so I wonder if either the Fedora libc always adds that CLONE_THREAD
>> thing
>> to the clone() calls, or whether the FC2 kernel is buggy.
>>
>> Arjan?
>>
>> Linus
>>
>>
>>
>>
>
> -
> To unsubscribe from this list: send the line "unsubscribe
> linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
>
>
next prev parent reply other threads:[~2004-06-06 15:34 UTC|newest]
Thread overview: 54+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-06-05 15:28 clone() <-> getpid() bug in 2.6? Russell Leighton
2004-06-05 20:45 ` Linus Torvalds
2004-06-05 20:55 ` Arjan van de Ven
2004-06-05 21:13 ` Linus Torvalds
2004-06-05 21:48 ` Robert Love
2004-06-05 22:44 ` Linus Torvalds
2004-06-05 21:53 ` Chris Wedgwood
2004-06-05 22:47 ` Robert Love
2004-06-05 22:57 ` David S. Miller
2004-06-05 23:01 ` Linus Torvalds
2004-06-05 23:07 ` Davide Libenzi
2004-06-05 23:18 ` Linus Torvalds
2004-06-05 23:26 ` Davide Libenzi
2004-06-06 5:08 ` Kalin KOZHUHAROV
2004-06-06 5:13 ` Chris Wedgwood
2004-06-06 5:34 ` Kalin KOZHUHAROV
2004-06-06 6:07 ` Linus Torvalds
2004-06-06 6:43 ` Kalin KOZHUHAROV
2004-06-06 7:57 ` Erik Andersen
2004-06-06 16:57 ` Linus Torvalds
2004-06-06 18:53 ` Simon Kirby
2004-06-06 19:00 ` Linus Torvalds
2004-06-06 9:52 ` Bernd Eckenfels
2004-06-06 13:07 ` Paul Rolland
2004-06-06 17:20 ` Patrick J. LoPresti
2004-06-06 17:31 ` Paul Rolland
2004-06-06 17:43 ` Davide Libenzi
2004-06-06 18:17 ` Rik van Riel
2004-06-06 18:37 ` Patrick J. LoPresti
2004-06-06 16:33 ` chris
[not found] ` <200406062022.54320.vda@port.imtp.ilyichevsk.odessa.ua>
2004-06-06 17:55 ` Linus Torvalds
2004-06-07 18:20 ` Bruce Guenter
2004-06-08 11:06 ` Kalin KOZHUHAROV
2004-06-05 23:19 ` Robert Love
2004-06-06 14:29 ` Russell Leighton
2004-06-06 15:38 ` Russell Leighton [this message]
2004-06-06 15:44 ` Using getpid() often, another way? [was Re: clone() <-> getpid() bug in 2.6?] Robert Love
2004-06-07 0:20 ` Russell Leighton
2004-06-06 15:58 ` Arjan van de Ven
2004-06-06 23:49 ` Russell Leighton
2004-06-07 12:13 ` Arjan van de Ven
2004-06-07 13:48 ` Sean Neakums
2004-06-07 14:00 ` Christoph Hellwig
2004-06-07 14:10 ` Sean Neakums
2004-06-07 18:42 ` David Mosberger
2004-06-07 23:02 ` Russell Leighton
2004-06-07 23:27 ` David Mosberger
2004-06-08 6:01 ` Arjan van de Ven
2004-06-08 9:48 ` Eric W. Biederman
2004-06-07 0:09 ` Russell Leighton
2004-06-07 12:20 ` Arjan van de Ven
2004-06-06 17:19 ` Linus Torvalds
2004-06-12 9:15 ` Dominik Straßer
2004-06-12 13:47 ` Linus Torvalds
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=40C33A84.4060405@elegant-software.com \
--to=russ@elegant-software.com \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox