Understanding Pthreads

前端 未结 4 1213
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 20:07

I came across a concept in Advanced Linux Programming. Here\'s a link: refer to 4.5 GNU/Linux Thread Implementation.

I\'m clear wit

4条回答
  •  情书的邮戳
    2020-12-16 21:01

    I get the same results of the book with linux that contains the libc libuClibc-0.9.30.1.so (1).

    root@OpenWrt:~# ./test
    main thread pid is 1151
    child thread pid is 1153
    

    and I tried to run this program with a linux that contains the libc from ubuntu libc6 (2)

    $ ./test
    main thread pid is 2609
    child thread pid is 2609
    

    The libc (1) use linuxthreads implementation of pthread

    And the libc (2) use NPTL ("Native posix thread library") implementation of pthread

    According to the linuxthreads FAQ (in J.3 answer):

    each thread is really a distinct process with a distinct PID, and signals sent to the PID of a thread can only be handled by that thread

    So in the old libc which use linuxthreads implementation, each thread has its distinct PID

    In the new libc version which use NPTL implementation, all threads has the same PID of the main process.

    The NPTL was developed by redhat team. and according to the redhat NPTL document: One of the problems which are solved in the NPTL implementation is:

    (Chapter: Problems with the Existing Implementation, page5)

    Each thread having a different process ID causes compatibility problems with other POSIX thread implementations. This is in part a moot point since signals can'tbe used very well but is still noticeable


    And that explain your issue.

    You are using the new libc version that contains the NPTL ("Native posix thread library") implementation of pthread

    And the Book use an old version of libc that contains linuxthreads implementation of pthread

提交回复
热议问题