Re: Using getpid() often, another way? [was Re: clone() <-> getpid() bug in 2.6?]

From: Linus Torvalds (torvalds_at_osdl.org)
Date: 06/06/04

  • Next message: Patrick J. LoPresti: "Re: clone() <-> getpid() bug in 2.6?"
    Date:	Sun, 6 Jun 2004 10:19:12 -0700 (PDT)
    To: Russell Leighton <russ@elegant-software.com>
    
    

    On Sun, 6 Jun 2004, Russell Leighton wrote:
    >
    > Linus said he could not imagine a program using getpid() more than a
    > handful of times...

    No, I said that I could not imagine using it more than a handful of times
    _except_ for the cases of:

     - thread identification without a native thread area
     - benchmarking.

    (and in both of these cases it is _wrong_ to cache the pid value)

    > 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().

    Your problem falls under the thread ID thing. It's fine and understandable
    to use getpid() for that, although you could probably do it faster if you
    are willing to use the support that modern kernels give you and that glibc
    uses: the "TLS" aka Thread Local Storage support.

    Thread-local storage involved having a user-mode register that points some
    way to a special part of the address space. On x86, where the general
    register set is very limited and stealing a general reg would thus be bad,
    it uses a segment and loads the TLS pointer into a segment register
    (segment registers are registers too - and since nobody sane uses them for
    anything else these days, both %gs and %fs are freely usable).

    > Would gettid() be any better?

    You'd avoid this particular glibc bug with gettid.

                    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 message: Patrick J. LoPresti: "Re: clone() <-> getpid() bug in 2.6?"

    Relevant Pages

    • Re: Using getpid() often, another way? [was Re: clone() <-> getpid() bug in 2.6?]
      ... >>handful of times... ... I said that I could not imagine using it more than a handful of times ... I found this getpid() bug with just ... >it uses a segment and loads the TLS pointer into a segment register ...
      (Linux-Kernel)
    • Re: On Bill of Rights Day, Imagine the D.C. Gun Law in 1775
      ... Imagine the D.C. Gun Law in 1775 ... Bill of Rights Day, but District of Columbia residents are second-class citizens when it comes to the Second Amendment. ... Imagine that instead he reads to the assembled colonists the following decree by British Commander-in-Chief General Thomas Gage, modeled of course after the newly-minted 2008 District of Columbia gun law: ... Imagine that the colonists reverently surrendered their "assault weapons" and sought to register their non-banned arms and pay the Chief his "reasonable fee." ...
      (talk.politics.guns)
    • On Bill of Rights Day, Imagine the D.C. Gun Law in 1775
      ... Imagine the D.C. Gun Law in 1775 ... Bill of Rights Day, but District of Columbia residents are second-class citizens when it comes to the Second Amendment. ... Bans "assault weapons," defined by a long list of various rifles, pistols, and shotguns, and concludes with the catch-all: "Any firearm that the Chief may designate as an assault weapon by rule." ... Imagine that the colonists reverently surrendered their "assault weapons" and sought to register their non-banned arms and pay the Chief his "reasonable fee." ...
      (talk.politics.guns)
    • Re: Kellys Eye
      ... >>> is being blown up out of all proportion to the dangers teachers pose ... But he got a caution for *inciting* to download, ... Do you believe then that any and everyone on the SO register should be ... Imagine this teacher had continued in post and there ...
      (uk.politics.misc)
    • Re: national bike registry
      ... I haven't heard of anyone recovering their bike with it. ... you're at the UW, I imagine you could register with the campus police, ...
      (rec.bicycles.misc)