首页 文章

我已经考虑了解Linux系统和getpid的结果

提问于
浏览
0

pid:2394 tid:2399

pid:2394 tid:2398

pid:2394 tid:2397

pid:2394 tid:2395

  • this is using getpid() __NR_gettid*

pid:2529 tid:1811777280

pid:2529 tid:1820169984

pid:2529 tid:1828562688

pid:2529 tid:1836955392

pid:2529 tid:1845348096

  • this is using pthread_self()*

我正在使用Fedora17 . 我想知道为什么getpid()和pthread_self()之间的tid结果有所不同?

哪一个是对的?我的教授 . 说pthread使用posix而不是linux系统 .

并且在多线程条件下pid是一样的吗?我的结果是否正确?

1 回答

  • 3

    gettid 返回线程ID, pthread_self 返回POSIX线程ID . 两者都不同 .

    这是默认行为 . 请参见 pthread_self() 的linux手册页 . 这三个条款将澄清它 .

    线程标识符应被视为不透明:任何使用除pthreads调用之外的线程ID的尝试都是不可移植的,并且可能导致未指定的结果 . 线程ID仅保证在进程中是唯一的 . 在已加入已终止的线程或已分离的线程已终止之后,可以重用线程ID . pthread_self()返回的线程ID与调用gettid(2)返回的内核线程ID不同 .

    这也来自 gettid 手册页

    此调用返回的线程ID与POSIX线程ID(即pthread_self(3)返回的不透明值)不同

相关问题