Linux 2.6 版本后多线程的变化

        前段时间看了下APUE关于多线程的部分,讲Linux的多线程是通过clone的系统调用实现的,针对这点进行验证发现并非如此,确切说是版本大于2.6的是这样的。

#include <pthread.h>
#include <stdio.h>

void prid(const char *name)
{
        pid_t pid;
        pthread_t tid;

        pid = getpid ();
        tid = pthread_self ();
        printf ("%s: pid = %u, tid = 0x%x\n", name, (unsigned int)pid, (unsigned int)tid);
}
void *foo (void *args)
{
        prid ("new thread");
        pthread_exit ((void *)0);
}

int main(int argc, char const *argv[])
{
        pthread_t tid;
        int err;
        void *ret;
        err = pthread_create (&tid, NULL, foo, NULL);
        if (err != 0)
        {
                printf ("create thread error\n");
        }
        prid ("main thread");
        pthread_join (tid, &ret);
        return 0;
}

编译:

gcc thread.c -lpthread

运行 ./a.out,结果:

main thread: pid = 5714, tid = 0xe7ae4740
new thread: pid = 5714, tid = 0xe72f4700

         可以看见主线程和新创建线程在调用getpid()获取的进程号是一样的,也就是说并非是通过clone实现的。在查找答案的时候发现有网友说是缓存造成的,我只能说想象力很丰富。答案是Linux 2.6 后使用的是Native POSIX Thread Library(NPTL)库,取代了LinuxThreads库,使得Linux下的线程是一种更像线程的线程。

详细的区别请看链接http://www.ibm.com/developerworks/cn/linux/l-threading.html

 

        APUE第三版已经已经没这个问题了。我想知道第三版的修改了哪些东西,于是按书上给的邮件地址[email protected]发了封邮件。

回复:

I summarized the changes in the preface.


Steve

Jobs? It‘s a cold joke!

所以我决定把第三版的前言贴一下,当然其中提到了NPTL。

Changes from the Second Edition

One of the biggest changes to the Single UNIX Specification in POSIX.1-2008 is the demotion of the STREAMS-related interfaces to obsolescent status. This is the first step before these interfaces are removed entirely in a future version of the standard. Because of this, I have reluctantly removed the STREAMS content from this edition of the book. This is an unfortunate change, because the STREAMS interfaces provided a nice contrast to the socket interfaces, and in many ways were more flexible. Admittedly, I am not entirely unbiased when it comes to the STREAMS mechanism, but there is no debating the reduced role it is playing in current systems:

       Linux doesn’t include STREAMS in its base system, although packages (LiS and OpenSS7)are available to add this functionality.

        Although Solaris 10 includes STREAMS, Solaris 11uses a socket implementation that is not built on top of STREAMS.

        Mac OS X doesn’t include support for STREAMS.

        FreeBSD doesn’t include support for STREAMS (and never did).

So with the removal of the STREAMS-related material, an opportunity exists to replace it with new topics, such as POSIX asynchronous I/O.

In the second edition, the Linux version covered was based on the 2.4 version of the source. In this edition, I have updated the version of Linux to 3.2. One of the largest area of differences between these two versions is the threads subsystem. Between Linux 2.4 and Linux 2.6, the threads implementation was changed to the Native POSIX Thread Library (NPTL).NPTL makes threads on Linux behave more like threads on the other systems.

In  total,  this  edition  includes  more than  70  new  interfaces,  including  interfaces  to handle asynchronous I/O, spin locks, barriers, and POSIX semaphores.  Most obsolete interfaces are removed, except for a few ubiquitous ones.

希望对大家有用!

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。