为何线程有PID?Linux上查看某个进程内存占用情况

在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.

关于线程继承关系图如下:

 
  1. USER VIEW
  2. <-- PID 43 --> <----------------- PID 42 ----------------->
  3. +---------+
  4. | process |
  5. _| pid=42 |_
  6. _/ | tgid=42 | \_ (new thread) _
  7. _ (fork) _/ +---------+ \
  8. / +---------+
  9. +---------+ | process |
  10. | process | | pid=44 |
  11. | pid=43 | | tgid=42 |
  12. | tgid=43 | +---------+
  13. +---------+
  14. <-- PID 43 --> <--------- PID 42 --------> <--- PID 44 --->
  15. 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()方法可以打印进程的PID
gettid()方法可以打印线程的PID

void * 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));  
}  
 
Linux 上进行开发和运营维护的时候,免不了要查看某一个程序所占用内存的情况。有很多个命令都可以达到我们的需求,这里给大家列举几个:

 

1:cat /proc/pid/status

在这里插入图片描述

2:ps -aux | grep pid

在这里插入图片描述

3:top -p pid 查看程序的情况

PID:进程的ID
USER:进程所有者
PR:进程的优先级别,越小越优先被执行
NInice:值
VIRT:进程占用的虚拟内存
RES:进程占用的物理内存
SHR:进程使用的共享内存
S:进程的状态。S表示休眠,R表示正在运行,Z表示僵死状态,N表示该进程优先值为负数
%CPU:进程占用CPU的使用率
%MEM:进程使用的物理内存和总内存的百分比
TIME+:该进程启动后占用的总的CPU时间,即占用CPU使用时间的累加值。
COMMAND:进程启动命令名称
在这里插入图片描述

这里会打印出当前进程详细的情况,其中,占用内存是 VmRSS 127884kb。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章