亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2020年05月11日 10:48:30   作者:test1280  
這篇文章主要介紹了詳解Linux獲取線程的PID(TID、LWP)的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

在 Linux C/C++ 中通常是通過(guò) pthread 庫(kù)進(jìn)行線程級(jí)別的操作。

在 pthread 庫(kù)中有函數(shù):

pthread_t pthread_self(void);

它返回一個(gè) pthread_t 類型的變量,指代的是調(diào)用 pthread_self 函數(shù)的線程的 “ID”。

怎么理解這個(gè)“ID”呢?

這個(gè)“ID”是 pthread 庫(kù)給每個(gè)線程定義的進(jìn)程內(nèi)唯一標(biāo)識(shí),是 pthread 庫(kù)維持的。

由于每個(gè)進(jìn)程有自己獨(dú)立的內(nèi)存空間,故此“ID”的作用域是進(jìn)程級(jí)而非系統(tǒng)級(jí)(內(nèi)核不認(rèn)識(shí))。

其實(shí) pthread 庫(kù)也是通過(guò)內(nèi)核提供的系統(tǒng)調(diào)用(例如clone)來(lái)創(chuàng)建線程的,而內(nèi)核會(huì)為每個(gè)線程創(chuàng)建系統(tǒng)全局唯一的“ID”來(lái)唯一標(biāo)識(shí)這個(gè)線程。

這個(gè)系統(tǒng)全局唯一的“ID”叫做線程PID(進(jìn)程ID),或叫做TID(線程ID),也有叫做LWP(輕量級(jí)進(jìn)程=線程)的。

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

測(cè)試代碼:

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;
}

在主線程中通過(guò) pthread 庫(kù)創(chuàng)建三個(gè)線程,不斷輸出 “i am xxx” 的信息。

運(yùn)行輸出:

[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是待觀察的進(jìn)程的PID。

輸出中可見(jiàn)此進(jìn)程包含4個(gè)線程,他們的PID都是11209,PPID都是9374,其中LWP即我們要找的線程ID。

我們注意到有一個(gè)線程的LWP同進(jìn)程的PID一致,那個(gè)線程就是主線程。

-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中指定了進(jìn)程PID,輸出包含四個(gè)線程,通過(guò)PID字段可獲知每個(gè)線程的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等同于主線程所在進(jìn)程的進(jìn)程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.

方法六:源碼級(jí)獲取

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) 系統(tǒng)調(diào)用返回一個(gè) pid_t 類型值,即線程在內(nèi)核中的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)有什么價(jià)值?

很多命令參數(shù)的 PID 實(shí)際指代內(nèi)核中線程的ID,例如 taskset、strace 等命令。

例如 taskset 命令,可以將進(jìn)程綁定到某個(gè)指定的CPU核心上。

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

example:

# 將 11282 進(jìn)程綁定到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
# 此時(shí)實(shí)際只綁定主線程到CPU第0核心

# 將其他四個(gè)線程一并綁定到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
# 此時(shí),進(jìn)程PID=11282的進(jìn)程所有線程都將僅在CPU第0核心中運(yùn)行

strace 同理,可以指定線程PID,追蹤某個(gè)線程執(zhí)行的系統(tǒng)調(diào)用以及信號(hào)。

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

相關(guān)文章

  • ubuntu16.04下vim安裝失敗的原因分析及解決方案

    ubuntu16.04下vim安裝失敗的原因分析及解決方案

    重裝了ubuntu系統(tǒng),安裝vim出現(xiàn)了很多奇葩問(wèn)題,今天百度查閱資料才順利解決。今天小編特此把解決思路分享到腳本之家平臺(tái),需要的朋友參考下吧
    2016-11-11
  • Apache下禁止特定目錄執(zhí)行PHP 提高服務(wù)器安全性

    Apache下禁止特定目錄執(zhí)行PHP 提高服務(wù)器安全性

    之前在博文從PHP安全講DedeCms的安全加固中說(shuō)過(guò)在PHP安全中保護(hù)“可寫(xiě)目錄下的文件不允許被訪問(wèn)到的重要性,還提出了改名文件夾的方式來(lái)保護(hù)該目錄。
    2009-11-11
  • Apache?Linkis?中間件架構(gòu)及快速安裝步驟

    Apache?Linkis?中間件架構(gòu)及快速安裝步驟

    作為計(jì)算中間件,Linkis 提供了強(qiáng)大的連通、復(fù)用、編排、擴(kuò)展和治理管控能力,通過(guò)計(jì)算中間件將應(yīng)用層和引擎層解耦,簡(jiǎn)化了復(fù)雜的網(wǎng)絡(luò)調(diào)用關(guān)系,本文給大家介紹Apache?Linkis?中間件架構(gòu)的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2022-03-03
  • Linux強(qiáng)制釋放占用端口以及Linux防火墻端口開(kāi)放方法詳解

    Linux強(qiáng)制釋放占用端口以及Linux防火墻端口開(kāi)放方法詳解

    在linux系統(tǒng)中安裝一些軟件時(shí)有時(shí)遇到端口被占用的情況,本文為大家介紹了遇到這種情況我們應(yīng)該怎么處理
    2018-10-10
  • shell腳本如何啟動(dòng)springboot項(xiàng)目

    shell腳本如何啟動(dòng)springboot項(xiàng)目

    這篇文章主要介紹了shell腳本如何啟動(dòng)springboot項(xiàng)目問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • CentOS 7 中firewall-cmd命令詳細(xì)介紹

    CentOS 7 中firewall-cmd命令詳細(xì)介紹

    這篇文章主要介紹了 CentOS 7 中firewall-cmd命令詳細(xì)介紹的相關(guān)資料,這里對(duì)CentOS 7的firewall-cmd命令一一介紹,希望能幫助開(kāi)始使用的朋友,需要的朋友可以參考下
    2016-11-11
  • centos 6.5 oracle開(kāi)機(jī)自啟動(dòng)的環(huán)境配置詳解

    centos 6.5 oracle開(kāi)機(jī)自啟動(dòng)的環(huán)境配置詳解

    這篇文章主要介紹了centos 6.5 oracle開(kāi)機(jī)自啟動(dòng)的環(huán)境配置詳解的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • 詳解如何在Linux上一次性批量重命名一組文件

    詳解如何在Linux上一次性批量重命名一組文件

    這篇文章主要介紹了詳解如何在Linux上一次性批量重命名一組文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Linux md5sum命令的使用方法

    Linux md5sum命令的使用方法

    這篇文章主要介紹了Linux md5sum命令的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • LINUX啟動(dòng)/重啟/停上MYSQL的命令(詳解)

    LINUX啟動(dòng)/重啟/停上MYSQL的命令(詳解)

    下面小編就為大家?guī)?lái)一篇LINUX啟動(dòng)/重啟/停上MYSQL的命令(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-11-11

最新評(píng)論