日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux下获取线程TID的方法——gettid()

發(fā)布時(shí)間:2025/3/17 linux 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux下获取线程TID的方法——gettid() 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

如何獲取進(jìn)程的PID(process ID)?

可以使用:

  • #include?<unistd.h>??
  • pid_t?getpid(void);??
  • 通過查看頭文件說明,可以得到更詳細(xì)的信息:
  • find?/usr/include?-name?unistd.h??
  • ??
  • /usr/include/asm/unistd.h??
  • /usr/include/bits/unistd.h??
  • /usr/include/linux/unistd.h??
  • /usr/include/sys/unistd.h??
  • /usr/include/unistd.h??
  • ??
  • cat?/usr/include/unistd.h?|?grep?getpid??
  • ??
  • /*?Get?the?process?ID?of?the?calling?process.??*/??
  • extern?__pid_t?getpid?(void)?__THROW;??

  • 如何獲取線程的TID(thread ID)?

    通過查看man得到如下描述:
    (1) The gettid() system call first appeared on Linux in kernel 2.4.11.
    (2) gettid() returns the thread ID of the current process. This is equal to the process ID (as returned by getpid(2)), unless the process is part of a thread group (created by specifying the CLONE_THREAD flag to the clone(2) system call). All processes in the same thread group have the same PID, but each one has a unique TID.
    (3) gettid() is Linux specific and should not be used in programs that are intended to be portable. (如果考慮移植性,不應(yīng)該使用此接口)

    但是根據(jù)man的使用說明,測試后發(fā)現(xiàn)會(huì)報(bào)找不到此接口的錯(cuò)誤“error: undefined reference to `gettid'”,通過下面鏈接可以找到更詳細(xì)的說明:
    http://www.kernel.org/doc/man-pages/online/pages/man2/gettid.2.html
    (1) Glibc does not provide a wrapper for this system call; call it using syscall(2).(說明Glibc并沒有提供此接口的聲明,此接口實(shí)際使用的是系統(tǒng)調(diào)用,使用者可以自己創(chuàng)建包裹函數(shù))
    (2) The thread ID returned by this call is not the same thing as a POSIX thread ID (i.e., the opaque value returned by pthread_self(3)).

    然后查看/usr/include/sys/syscall.h(實(shí)際在/usr/include/asm/unistd.h)可以找到我們需要的system call number:
    #define __NR_gettid???? 224

    因此,要獲取某個(gè)線程的TID,最nasty的方式是:

  • #include?<sys/syscall.h>??
  • printf("The?ID?of?this?thread?is:?%ld\n",?(long?int)syscall(224));??
  • 或者比較elegant的方式是:
  • #include?<sys/syscall.h>??
  • #define?gettidv1()?syscall(__NR_gettid)??
  • #define?gettidv2()?syscall(SYS_gettid)??
  • printf("The?ID?of?this?thread?is:?%ld\n",?(long?int)gettidv1());//?最新的方式??
  • printf("The?ID?of?this?thread?is:?%ld\n",?(long?int)gettidv2());//?traditional?form??
  • PS: 在/usr/include/sys/syscall.h中可以看到關(guān)于__NR_<name>和SYS_<name>兩個(gè)宏的區(qū)別,實(shí)際最后使用的都是__NR_<name>。
  • //?/usr/include/bits/syscall.h??
  • #define?SYS_gettid?__NR_gettid??
  • ??
  • #ifndef?_LIBC??
  • /*?The?Linux?kernel?header?file?defines?macros?`__NR_<name>',?but?some??????
  • ???programs?expect?the?traditional?form?`SYS_<name>'.??So?in?building?libc?
  • ???we?scan?the?kernel's?list?and?produce?<bits/syscall.h>?with?macros?for?
  • ???all?the?`SYS_'?names.??*/??
  • #?include?<bits/syscall.h>??
  • #endif??

  • 驗(yàn)證TID是否正確的方法:
    查看進(jìn)程pid
    (1) ps ux | grep prog_name
    (2) pgrep prog_name?
    查看線程tid
    (1) ps -efL | grep prog_name
    (2) ls /proc/pid/task


    from :?http://blog.csdn.net/delphiwcdj/article/details/8476547

    轉(zhuǎn)載于:https://www.cnblogs.com/hehehaha/p/6332386.html

    總結(jié)

    以上是生活随笔為你收集整理的Linux下获取线程TID的方法——gettid()的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。