【linux】查看 Linux 线程详细信息的方法

For Windows, I think shows you all the threads under a process.

Is there a similar command line utility for Linux that can show me details about all the threads a particular process is spawning?

I think I should have made myself more clear. I do not want to see the process hierarcy, but a list of all the threads spawned by a particular process

See this screenshot

How can this be achieved in Linux?Thanks!

答案一:

The classical tool top shows processes by default but can be told to show threads with the H key press or -H command line option. There is also , which is similar to top but has scrolling and colors; it shows all threads by default (but this can be turned off). ps also has a few options to show threads, especially H and -L.

There are also GUI tools that can show information about threads, for example (a simple GUI wrapper around ps) or (a system monitor with lots of configuration options).

For each process, a lot of information is available in /proc/12345 where 12345 is the process ID. Information on each thread is available in /proc/12345/task/67890 where 67890 is the kernel thread ID. This is where ps, top and other tools get their information.

答案二:

, a curses version of top, has a display option for showing all the threads for each process in a tree view. Starting htop and pressing F5 will result in:

答案三:

Listing threads under LinuxCurrent provide answers

Hello Lazer, I would like to make it clear that each answer here is providing you with exactly what you have specified, a list of all threads associated with a process, this maybe be unobvious in htop as it by defaults lists all threads on the system not just the process, but top -H -p works better for example:

top -00:03:29 up 3 days,14:49,5 users, load average:0.76,0.33,0.18Tasks:18 total,0 running,18 sleeping,0 stopped,0 zombieCpu(s):22.6%us,5.7%sy,4.2%ni,66.2%id,0.8%wa,0.5%hi,0.1%si,0.0%stMem:2063948k total,1937744k used,126204k free,528256k buffersSwap:1052220k total,11628k used,1040592k free,539684k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 30170 daniel 200371m140m107m S 10.07.00:31.37 source:src 30066 daniel -900371m140m107m S 2.07.00:07.87 clementine 30046 daniel 200371m140m107m S 0.07.00:32.05 clementine 30049 daniel 200371m140m107m S 0.07.00:00.03 clementine 30050 daniel 200371m140m107m S 0.07.00:00.31 clementine 30051 daniel 200371m140m107m S 0.07.00:00.00 clementine 30052 daniel 200371m140m107m S 0.07.00:00.00 clementine 30053 daniel 200371m140m107m S 0.07.00:00.00 clementine 30054 daniel 200371m140m107m S 0.07.00:00.03 clementine 30055 daniel 200371m140m107m S 0.07.00:00.00 clementine 30056 daniel 200371m140m107m S 0.07.00:00.00 clementine 30057 daniel 200371m140m107m S 0.07.00:00.04 clementine 30058 daniel 200371m140m107m S 0.07.00:00.00 clementine 30060 daniel 200371m140m107m S 0.07.00:00.16 clementine 30061 daniel 200371m140m107m S 0.07.00:00.00 clementine 30062 daniel 200371m140m107m S 0.07.00:00.00 clementine 30064 daniel 200371m140m107m S 0.07.00:00.00 clementine 30065 daniel 200371m140m107m S 0.07.00:00.00 clementine

as a side note the thread with -90 is actually a realtime thread

but

There’s also another option which is true CLI ps -e -T | grep

-e show’s all processes
-T lists all threads
| pipes the output to the next command
grep this filters the contents
here’s an example:

$ ps -e -T | grep clementine PID SPID TTY TIME CMD # this is here for clarity3004630046 pts/200:00:17 clementine3004630049 pts/200:00:00 clementine3004630050 pts/200:00:00 clementine3004630051 pts/200:00:00 clementine3004630052 pts/200:00:00 clementine3004630053 pts/200:00:00 clementine3004630054 pts/200:00:00 clementine3004630055 pts/200:00:00 clementine3004630056 pts/200:00:00 clementine3004630057 pts/200:00:00 clementine3004630058 pts/200:00:00 clementine3004630060 pts/200:00:00 clementine3004630061 pts/200:00:00 clementine3004630062 pts/200:00:00 clementine3004630064 pts/200:00:00 clementine3004630065 pts/200:00:00 clementine3004630066 pts/200:00:03 clementine $

each of these has the same PID so you know they are in the same process

please don’t hesitate to ask more questions

答案四:

ou may try to use:

/usr/bin/pstree $PID

For example:

pstree -p pidof iceweaseliceweasel(3630)─┬─{iceweasel}(3662)├─{iceweasel}(3663)├─{iceweasel}(3664)├─{iceweasel}(3665)├─{iceweasel}(3666)├─{iceweasel}(3674)├─{iceweasel}(3675)├─{iceweasel}(3676)├─{iceweasel}(3677)├─{iceweasel}(3681)├─{iceweasel}(3682)…

Each thread has its own PID.

答案五:

The two standard tools to show process informations are ps and top (and htop which is similar/improved).

Notes:

Many program change the apparent name of the threads to something meaningful, the tools below can either display the binary name or that apparent name (check PID 1086 in the examples below).
In the examples below, I have removed most process to keep the answer short.
The command arguments example below are common ones. check the manpage for alternate options (ps -m, ps m, ps H…)
Realtime view of all or process, using top -H

top -16:24:42 up 3:49,3 users, load average:0.23,0.29,0.31Threads:503 total,2 running,501 sleeping,0 stopped,0 zombie%Cpu(s):9.7 us,1.6 sy,0.0 ni,88.5 id,0.2 wa,0.0 hi,0.0 si,0.0 stKiBMem:3938668 total,2148708 used,1789960 free,133524 buffersKiBSwap:3903484 total,0 used,3903484 free.822904 cached Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1054 root 20025866435242692 S 0.00.10:00.00 rsyslogd 1086 root 20025866435242692 S 0.00.10:00.03in:imuxsock 1087 root 20025866435242692 S 0.00.10:00.00in:imklog 1090 root 20025866435242692 S 0.00.10:00.05 rs:main Q:Reg2452 fpiat 2002529275203580 S 0.00.20:00.69 bash 2495 fpiat 2002508869883256 S 0.00.20:00.05 bash

Instant view of all process and threads, using ps -eLf

$ ps -eLf UID PID PPID LWP C NLWP STIME TTY TIME CMD root 1054110540412:34?00:00:00/usr/sbin/rsyslogd -n root 1054110860412:34?00:00:00/usr/sbin/rsyslogd -n root 1054110870412:34?00:00:00/usr/sbin/rsyslogd -n root 1054110900412:34?00:00:00/usr/sbin/rsyslogd -n franklin 2452244824520112:35 pts/000:00:00/bin/bash franklin 2495244824950112:36 pts/100:00:00/bin/bash

Threads information of a process, using ps -T

ps -T -C rsyslogd PID SPID TTY TIME CMD 10541054?00:00:00 rsyslogd 10541086?00:00:00in:imuxsock 10541087?00:00:00in:imklog 10541090?00:00:00 rs:main Q:Reg

(note: use either option -C command, or -p PID to select the process)

Details threads information of a process, using custom ps

$ ps -L -o pid,lwp,pri,nice,start,stat,bsdtime,cmd,comm -C rsyslogd PID LWP PRI NI STARTED STAT TIME CMD COMMAND 1054105419012:34:53Ssl0:00/usr/sbin/rsyslogd -n rsyslogd 1054108619012:34:53Ssl0:00/usr/sbin/rsyslogd -n in:imuxsock 1054108719012:34:53Ssl0:00/usr/sbin/rsyslogd -n in:imklog 1054109019012:34:53Ssl0:00/usr/sbin/rsyslogd -n rs:main Q:Reg返回搜狐,查看更多

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值