国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

為何線程有PID?

2019-11-08 02:42:59
字體:
來源:轉載
供稿:網友
linux下用 top -H -p <pid> 查詢某個進程的線程按理說,都是某個進程下的線程, 應該進程id PID一樣啊,但實際卻都不一樣

實際是被PID的名字給弄混了,線程進程都會有自己的ID,這個ID就叫做PID,PID是不特指進程ID,線程ID也可以叫做PID。

pthread庫里的每一個線程都對應一個內核線程,都是有單獨的pid。

The four threads will have the same PID but only when viewed from above. What you (as a user) call a PID is not what the kernel (looking from below) calls a PID.

In the kernel, each thread has it's own ID, called a PID (although it would possibly make more sense to call this a TID, or thread ID) and they also have a TGID (thread group ID) which is the PID of the thread that started the whole PRocess.

Simplistically, when a new process is created, it appears as a thread where both the PID and TGID are the same (new) number.

When a thread starts another thread, that started thread gets its own PID (so the scheduler can schedule it independently) but it inherits the TGID from the original thread.

That way, the kernel can happily schedule threads independent of what process they belong to, while processes (thread group IDs) are reported to you.

關于線程繼承關系圖如下:

               USER VIEW <-- PID 43 --> <----------------- PID 42 ----------------->                     +---------+                     | process |                    _| pid=42  |_                  _/ | tgid=42 | /_ (new thread) _       _ (fork) _/   +---------+                  /      /                                        +---------++---------+                                    | process || process |                                    | pid=44  || pid=43  |                                    | tgid=42 || tgid=43 |                                    +---------++---------+ <-- PID 43 --> <--------- PID 42 --------> <--- PID 44 --->                     KERNEL VIEW

在這里你可以清晰的看到,創建一個新的進程會給一個新的PID和TGID,并且2個值相同,當創建一個新的線程的時候,會給你一個新的PID,并且TGID和之前開始的進程一致。

Linux通過進程查看線程的方法 1).htop按t(顯示進程線程嵌套關系)和H(顯示線程) ,然后F4過濾進程名。2).ps -eLf | grep java(快照,帶線程命令,e是顯示全部進程,L是顯示線程,f全格式輸出) 3).pstree -p <pid>(顯示進程樹,不加pid顯示所有) 4).top -Hp <pid> (實時) 5).ps -T -p <pid>(快照) 推薦程度按數字從小到大。

打印線程的PID的方法如下:getpid()方法可以打印進程的PIDgettid()方法可以打印線程的PIDvoid * thread_start(void *arg)  {      printf("Process ID: %d, thread ID %d/n", getpid(), gettid());  }  由于gettid()在glibc中沒有包含Note: There is no glibc wrapper for this system call; see NOTES. 所以用如下syscall函數在用戶空間替代gettid()的功能syscall(__NR_gettid))   或者  syscall(SYS_gettid)在文件 /usr/include/bits/syscall.h里, 有一行:#define SYS_gettid __NR_gettid  可見二者是一樣的。__NR_gettid是系統調用號#include <pthread.h>  #include <stdio.h>  #include <sys/types.h>  #include <sys/syscall.h>  #include <unistd.h>  void * thread_start(void *arg)  {      printf("[1] Process ID: %d, thread ID %d/n", getpid(), syscall(__NR_gettid));      printf("[2] Process ID: %d, thread ID %d/n", getpid(), syscall(SYS_gettid));  }  
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 佛冈县| 乐陵市| 德格县| 芜湖县| 陇西县| 盘锦市| 海晏县| 若尔盖县| 鄂托克前旗| 南城县| 昌宁县| 武川县| 陇川县| 衡山县| 普定县| 桃园县| 鄂尔多斯市| 乌审旗| 苗栗县| 大关县| 石楼县| 民乐县| 县级市| 湘潭市| 凌源市| 星子县| 比如县| 景宁| 乌兰县| 盐津县| 宜川县| 乳山市| 石泉县| 绥棱县| 鹤峰县| 通海县| 桐城市| 定兴县| 尼玛县| 高淳县| 海城市|