校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > 詳解Linux獲取線程的PID(TID、LWP)的幾種方式

詳解Linux獲取線程的PID(TID、LWP)的幾種方式

熱門標簽:四川保險智能外呼系統 濰坊寒亭400電話辦理多少錢 外呼系統全國 云南電商智能外呼系統哪家好 宜賓銷售外呼系統軟件 地圖標注能更改嗎 高德地圖標注公司需要錢 地圖標注員有發展前景嗎 廈門防封電銷電話卡

在 Linux C/C++ 中通常是通過 pthread 庫進行線程級別的操作。

在 pthread 庫中有函數:

pthread_t pthread_self(void);

它返回一個 pthread_t 類型的變量,指代的是調用 pthread_self 函數的線程的 “ID”。

怎么理解這個“ID”呢?

這個“ID”是 pthread 庫給每個線程定義的進程內唯一標識,是 pthread 庫維持的。

由于每個進程有自己獨立的內存空間,故此“ID”的作用域是進程級而非系統級(內核不認識)。

其實 pthread 庫也是通過內核提供的系統調用(例如clone)來創建線程的,而內核會為每個線程創建系統全局唯一的“ID”來唯一標識這個線程。

這個系統全局唯一的“ID”叫做線程PID(進程ID),或叫做TID(線程ID),也有叫做LWP(輕量級進程=線程)的。

如何查看線程在內核的系統全局唯一“ID”呢?大體分為以下幾種方式。

測試代碼:

main.c

#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

void *start_routine(void *arg) {
 char msg[32] = "";
 snprintf(msg, sizeof(msg)-1, "thd%d: i am thd%d\n", *((int *)arg), *((int *)arg));
 while (1) {
 write(1, msg, strlen(msg));
 sleep(1);
 }
}

int main() {

 int th1 = 1;
 pthread_t tid1;
 pthread_create(&tid1, NULL, start_routine, &th1);

 int th2 = 2;
 pthread_t tid2;
 pthread_create(&tid2, NULL, start_routine, &th2);
 
 int th3 = 3;
 pthread_t tid3;
 pthread_create(&tid3, NULL, start_routine, &th3);

 const char *msg = "main: i am main\n";
 while (1) {
 write(1, msg, strlen(msg));
 sleep(1);
 }

 return 0;
}

在主線程中通過 pthread 庫創建三個線程,不斷輸出 “i am xxx” 的信息。

運行輸出:

[test1280@localhost 20190227]$ gcc -o main main.c -lpthread
[test1280@localhost 20190227]$ ./main
main: i am main
thd2: i am thd2
thd3: i am thd3
thd1: i am thd1
thd2: i am thd2
……

方法一:ps -Lf $pid

[test1280@localhost ~]$ ps -Lf 11029
UID   PID PPID LWP C NLWP STIME TTY  STAT TIME CMD
test1280 11029 9374 11029 0 4 10:58 pts/0 Sl+ 0:00 ./main
test1280 11029 9374 11030 0 4 10:58 pts/0 Sl+ 0:00 ./main
test1280 11029 9374 11031 0 4 10:58 pts/0 Sl+ 0:00 ./main
test1280 11029 9374 11032 0 4 10:58 pts/0 Sl+ 0:00 ./main

11209是待觀察的進程的PID。

輸出中可見此進程包含4個線程,他們的PID都是11209,PPID都是9374,其中LWP即我們要找的線程ID。

我們注意到有一個線程的LWP同進程的PID一致,那個線程就是主線程。

-L Show threads, possibly with LWP and NLWP columns
-f does full-format listing.

方法二:pstree -p $pid

[test1280@localhost ~]$ pstree -p 11029
main(11029)─┬─{main}(11030)
   ├─{main}(11031)
   └─{main}(11032)

方法三:top -Hp $pid

[test1280@localhost ~]$ top -Hp 11029

在top中指定了進程PID,輸出包含四個線程,通過PID字段可獲知每個線程的PID(TID/LWP)。

man top
-H:Threads toggle
Starts top with the last remembered 'H' state reversed.
When this toggle is On, all individual threads will be displayed.
Otherwise, top displays a summation of all threads in a process.
-p:Monitor PIDs

方法四:ls -l /proc/$pid/task/

[test1280@localhost ~]$ ls -l /proc/11029/task/
total 0
dr-xr-xr-x. 6 test1280 test1280 0 Feb 27 10:58 11029
dr-xr-xr-x. 6 test1280 test1280 0 Feb 27 10:58 11030
dr-xr-xr-x. 6 test1280 test1280 0 Feb 27 10:58 11031
dr-xr-xr-x. 6 test1280 test1280 0 Feb 27 10:58 11032

方法五:pidstat -t -p $pid

[test1280@localhost ~]$ pidstat -t -p 11029
Linux 2.6.32-642.el6.x86_64 (localhost.localdomain) 02/27/2019 _x86_64_ (4 CPU)

11:20:39 AM  TGID  TID %usr %system %guest %CPU CPU Command
11:20:39 AM  11029   - 0.00 0.00 0.00 0.00  1 main
11:20:39 AM   -  11029 0.00 0.00 0.00 0.00  1 |__main
11:20:39 AM   -  11030 0.00 0.00 0.00 0.00  1 |__main
11:20:39 AM   -  11031 0.00 0.00 0.00 0.00  0 |__main
11:20:39 AM   -  11032 0.00 0.00 0.00 0.00  3 |__main

TGID是線程組ID,主線程的TID等同于主線程的線程組ID等同于主線程所在進程的進程ID。

man pidstat
-t Also display statistics for threads associated with selected tasks.
 This option adds the following values to the reports:
 TGID:The identification number of the thread group leader.
 TID:The identification number of the thread being monitored.

方法六:源碼級獲取

main.c

#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/syscall.h>

pid_t gettid() {
 return syscall(SYS_gettid);
}

void *start_routine(void *arg) {
 pid_t pid = gettid();
 pthread_t tid = pthread_self();
 printf("thd%d: pid=%d, tid=%lu\n", *((int *)arg), pid, tid);

 char msg[32] = "";
 snprintf(msg, sizeof(msg)-1, "thd%d: i am thd%d\n", *((int *)arg), *((int *)arg));
 while (1) {
 write(1, msg, strlen(msg));
 sleep(1);
 }
}

int main() {

 pid_t pid = gettid();
 pthread_t tid = pthread_self();
 printf("main: pid=%d, tid=%lu\n", pid, tid);

 int th1 = 1;
 pthread_t tid1;
 pthread_create(&tid1, NULL, start_routine, &th1);

 int th2 = 2;
 pthread_t tid2;
 pthread_create(&tid2, NULL, start_routine, &th2);
 
 int th3 = 3;
 pthread_t tid3;
 pthread_create(&tid3, NULL, start_routine, &th3);

 const char *msg = "main: i am main\n";
 while (1) {
 write(1, msg, strlen(msg));
 sleep(1);
 }

 return 0;
}

syscall(SYS_gettid) 系統調用返回一個 pid_t 類型值,即線程在內核中的ID。

[test1280@localhost 20190227]$ gcc -o main main.c -lpthread
[test1280@localhost 20190227]$ ./main
main: pid=11278, tid=140429854775040
main: i am main
thd3: pid=11281, tid=140429833787136
thd3: i am thd3
thd2: pid=11280, tid=140429844276992
thd2: i am thd2
thd1: pid=11279, tid=140429854766848
thd1: i am thd1
……

線程的PID(TID、LWP)有什么價值?

很多命令參數的 PID 實際指代內核中線程的ID,例如 taskset、strace 等命令。

例如 taskset 命令,可以將進程綁定到某個指定的CPU核心上。

如果進程是多線程模式,直接使用 taskset 將僅僅把主線程綁定,其他線程無法被綁定生效。

example:

# 將 11282 進程綁定到CPU第0核心
[test1280@localhost ~]$ ps -Lf 11282
UID   PID PPID LWP C NLWP STIME TTY  STAT TIME CMD
test1280 11282 9374 11282 0 4 11:33 pts/0 Sl+ 0:00 ./main
test1280 11282 9374 11283 0 4 11:33 pts/0 Sl+ 0:00 ./main
test1280 11282 9374 11284 0 4 11:33 pts/0 Sl+ 0:00 ./main
test1280 11282 9374 11285 0 4 11:33 pts/0 Sl+ 0:00 ./main
[test1280@localhost ~]$ taskset -pc 0 11282
pid 11282's current affinity list: 0-3
pid 11282's new affinity list: 0

# 查看其他線程是否真的綁定到CPU第0核心
[test1280@localhost ~]$ taskset -pc 11283
pid 11283's current affinity list: 0-3
[test1280@localhost ~]$ taskset -pc 11284
pid 11284's current affinity list: 0-3
[test1280@localhost ~]$ taskset -pc 11285
pid 11285's current affinity list: 0-3
[test1280@localhost ~]$ taskset -pc 11282
pid 11282's current affinity list: 0
# 此時實際只綁定主線程到CPU第0核心

# 將其他四個線程一并綁定到CPU第0核心
[test1280@localhost ~]$ taskset -pc 0 11283
pid 11283's current affinity list: 0-3
pid 11283's new affinity list: 0
[test1280@localhost ~]$ taskset -pc 0 11284
pid 11284's current affinity list: 0-3
pid 11284's new affinity list: 0
[test1280@localhost ~]$ taskset -pc 0 11285
pid 11285's current affinity list: 0-3
pid 11285's new affinity list: 0
# 此時,進程PID=11282的進程所有線程都將僅在CPU第0核心中運行

strace 同理,可以指定線程PID,追蹤某個線程執行的系統調用以及信號。

到此這篇關于詳解Linux獲取線程的PID(TID、LWP)的幾種方式的文章就介紹到這了,更多相關Linux獲取線程的PID內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

標簽:廊坊 德州 回訪 巴彥淖爾 紅河 滁州 湛江 廣安

巨人網絡通訊聲明:本文標題《詳解Linux獲取線程的PID(TID、LWP)的幾種方式》,本文關鍵詞  詳解,Linux,獲取,線程,的,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《詳解Linux獲取線程的PID(TID、LWP)的幾種方式》相關的同類信息!
  • 本頁收集關于詳解Linux獲取線程的PID(TID、LWP)的幾種方式的相關信息資訊供網民參考!
  • 推薦文章
    校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃
    国产日韩欧美一区二区三区综合| 日韩欧美在线观看一区二区三区| 在线成人av影院| 国产精品成人午夜| 一本在线高清不卡dvd| 一区二区三区免费看视频| 91视视频在线观看入口直接观看www | 综合色中文字幕| 日韩欧美成人激情| 成人免费视频一区二区| 一区二区三区日本| 91精品国产手机| 福利一区二区在线| 亚洲福利一区二区三区| 精品剧情v国产在线观看在线| 高清久久久久久| 日韩免费看的电影| 精品在线视频一区| 久久国产免费看| 国产精品日韩成人| 51精品视频一区二区三区| 国产成人精品一区二区三区四区| 亚洲人精品午夜| 欧美一区二区三级| 91蝌蚪porny| 狠狠色伊人亚洲综合成人| 自拍偷拍欧美激情| 欧美va在线播放| 欧洲中文字幕精品| 国产成人亚洲综合色影视| 午夜成人在线视频| 亚洲欧洲av在线| 国产欧美精品一区aⅴ影院| 9191成人精品久久| 在线亚洲+欧美+日本专区| 国产精华液一区二区三区| 五月天一区二区三区| 亚洲欧洲国产专区| 国产精品丝袜91| 日韩一区二区视频在线观看| 欧美性大战久久久久久久蜜臀| 国产麻豆日韩欧美久久| 蜜臀va亚洲va欧美va天堂| 午夜影院久久久| 亚洲一区二区视频| 亚洲激情图片一区| 中文字幕一区二区三区不卡在线| 日韩欧美的一区| 日韩午夜三级在线| 日韩精品资源二区在线| 777午夜精品免费视频| 欧亚一区二区三区| 91黄视频在线| 欧美视频一区二区三区四区| 在线观看视频一区| 在线观看免费亚洲| 欧美日韩精品一二三区| 欧美日韩国产色站一区二区三区| 欧美三级资源在线| 欧美色老头old∨ideo| 欧美福利视频导航| 精品欧美久久久| 久久精品视频免费| 国产精品久久久久毛片软件| 欧美激情一区二区三区不卡| 国产日产精品一区| 中文字幕一区二区三区乱码在线| 欧美美女网站色| 日韩一区二区三免费高清| 91精品在线观看入口| 久久综合精品国产一区二区三区| 久久综合久色欧美综合狠狠| 国产精品污网站| 一区二区欧美国产| 男人的j进女人的j一区| 成人性视频免费网站| 色94色欧美sute亚洲线路一ni| 欧美亚洲国产一区在线观看网站 | 国产大片一区二区| 99久久99久久免费精品蜜臀| 在线观看国产日韩| 精品乱码亚洲一区二区不卡| 国产免费观看久久| 亚洲一区免费视频| 国产一区视频导航| 91视视频在线观看入口直接观看www| 欧美老肥妇做.爰bbww| 国产亚洲欧美一区在线观看| 亚洲精品视频在线看| 老司机精品视频一区二区三区| 高潮精品一区videoshd| 欧美人狂配大交3d怪物一区| 久久久99精品久久| 亚洲成a人片在线观看中文| 狠狠色综合播放一区二区| 色国产精品一区在线观看| 精品国产乱码久久久久久图片| 亚洲欧洲精品一区二区精品久久久 | 国产一区二区三区免费在线观看| 一本大道久久精品懂色aⅴ| 日韩写真欧美这视频| 亚洲精品中文字幕乱码三区| 国产麻豆视频一区| 日韩一区二区在线观看| 亚洲综合一区二区三区| 国产精品白丝jk白祙喷水网站| 在线观看日韩精品| 国产欧美一区二区精品婷婷 | 国产综合色视频| 欧美性大战久久| 亚洲日本va午夜在线电影| 国产一区二区三区免费| 777奇米四色成人影色区| 欧美一二三区在线| 亚洲国产视频直播| 91蜜桃传媒精品久久久一区二区| 久久久久久日产精品| 久久精品国产亚洲高清剧情介绍 | 日韩1区2区日韩1区2区| 色诱视频网站一区| 成人禁用看黄a在线| 久久影院视频免费| 久久99精品久久久久久国产越南 | 91国偷自产一区二区三区成为亚洲经典| 久久人人爽爽爽人久久久| 婷婷久久综合九色国产成人| 欧美日韩一级二级三级| 最新久久zyz资源站| 成人精品一区二区三区四区| 精品99一区二区| 国模少妇一区二区三区| 精品国产污污免费网站入口 | 另类小说欧美激情| 91精品国产综合久久久蜜臀图片| 亚洲综合激情另类小说区| 在线免费观看日本欧美| 亚洲一二三四在线| 欧美亚洲国产一区二区三区va | 精品日韩欧美一区二区| 麻豆91精品视频| 精品日产卡一卡二卡麻豆| 国产高清在线精品| 综合激情网...| 欧美乱妇一区二区三区不卡视频| 日韩国产欧美视频| 精品久久人人做人人爰| 国产盗摄视频一区二区三区| 亚洲天堂免费看| 在线国产电影不卡| 奇米综合一区二区三区精品视频| 日韩免费高清av| 国产成人亚洲精品狼色在线| 亚洲欧洲日韩一区二区三区| 欧美三级日韩三级| 看电影不卡的网站| 欧美激情艳妇裸体舞| 91麻豆免费视频| 秋霞影院一区二区| 国产欧美日韩视频在线观看| 91啪在线观看| 蜜臀av国产精品久久久久| 欧美日韩亚洲国产综合| 精品中文字幕一区二区| 国产精品青草久久| 欧美视频一区二| 成人av片在线观看| 日韩极品在线观看| 欧美激情中文不卡| 制服丝袜亚洲色图| 成人av中文字幕| 天天综合天天综合色| 日本一区二区高清| 91精品国产综合久久久久| 成人免费视频视频在线观看免费| 亚洲狠狠爱一区二区三区| 国产偷国产偷亚洲高清人白洁| 在线观看亚洲a| 成人黄色大片在线观看| 日韩avvvv在线播放| 亚洲精品视频免费看| 日韩经典中文字幕一区| 国产乱一区二区| 一区二区免费看| 日本一区二区三区dvd视频在线| 欧美性猛片aaaaaaa做受| a美女胸又www黄视频久久| 国产亚洲精品精华液| 日韩欧美一区二区免费| 91福利在线播放| 99久久99久久免费精品蜜臀| 国产一二精品视频| 日韩经典中文字幕一区| 午夜不卡av在线| 亚洲国产成人高清精品| 亚洲精品五月天| 亚洲综合久久av| 香蕉成人啪国产精品视频综合网| 亚洲国产一区二区在线播放| 一区二区三区欧美视频| 一区二区三区在线影院|