如何看出process下面有幾個thread and thread name

在linux環境下,如何看出一個process有幾個thread and thread name

$pstree -p

init(1)-+-media_mediaserv(1257)
|-platform_servic(941)
|-portmap(723)
|-sh(722)---pstree(1496)
|-telnetd(721)
|-udhcpc(693)
|-udhcpc(705)
|-udhcpc(710)
|-udhcpc(713)
`-udhcpc(754)

我們以 pid 1257為例

ps -eLf

UID    PID   PPID  LWP  C     NLWP     STIME    TTY    TIME              CMD

root   1257     1     1257   0       31           00:15    ttyS0  00:00:00   media_mediaservice --
root   1257   1  1262   0       31           00:15    ttyS0  00:00:00   media_mediaservice --
root 1257 1 1263 0 31 00:15 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1264 0 31 00:15 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1265 0 31 00:15 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1266 0 31 00:15 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1267 0 31 00:15 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1268 0 31 00:15 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1269 0 31 00:15 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1270 0 31 00:15 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1271 0 31 00:15 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1272 0 31 00:15 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1273 0 31 00:15 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1274 0 31 00:15 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1275 0 31 00:15 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1276 0 31 00:15 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1277 0 31 00:15 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1278 0 31 00:15 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1279 0 31 00:15 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1280 0 31 00:15 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1281 0 31 00:15 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1319 0 31 00:16 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1321 0 31 00:16 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1323 0 31 00:16 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1324 0 31 00:16 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1325 0 31 00:16 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1359 0 31 00:16 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1360 0 31 00:16 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1361 0 31 00:16 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1364 0 31 00:16 ttyS0 00:00:00 media_mediaservice --
root 1257 1 1365 0 31 00:16 ttyS0 00:00:00 media_mediaservice --

從上面可以看出共有31個threads

the NLWP (number of threads) and LWP (thread ID) 

而main thread 是PID 1257 LWP 1257的thread

 

故我們可以經由proc下面的資訊,去得到更多

$ cat /proc/1257/status

Name: media_mediaserv
State: S (sleeping)
Tgid: 1257
Pid: 1257
PPid: 1
TracerPid: 0
Uid: 0 0 0 0
Gid: 0 0 0 0
FDSize: 256
Groups:
VmPeak: 622524 kB
VmSize: 622524 kB
VmLck: 0 kB
VmPin: 0 kB
VmHWM: 7380 kB
VmRSS: 7380 kB
VmData: 16204 kB
VmStk: 136 kB
VmExe: 6816 kB
VmLib: 9048 kB
VmPTE: 610 kB
VmSwap: 0 kB
Threads: 31
SigQ: 0/1725
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000000006
SigCgt: 00000001800044e8
CapInh: 0000000000000000
CapPrm: 0000001fffffffff
CapEff: 0000001fffffffff
CapBnd: 0000001fffffffff
Cpus_allowed: 3
Cpus_allowed_list: 0-1
voluntary_ctxt_switches: 50
nonvoluntary_ctxt_switches: 23

 

# ls /proc/1257/task
1257 1264 1267 1270 1273 1276 1279 1319 1324 1360 1365
1262 1265 1268 1271 1274 1277 1280 1321 1325 1361
1263 1266 1269 1272 1275 1278 1281 1323 1359 1364

 

ref:man proc

/proc/[pid]/task (since Linux 2.6.0-test6)
This is a directory that contains one subdirectory for each thread in the process. The name of each subdirectory is the numerical thread ID ([tid]) of the thread (see gettid(2)). Within each
of these subdirectories, there is a set of files with the same names and contents as under the /proc/[pid] directories. For attributes that are shared by all threads, the contents for each of
the files under the task/[tid] subdirectories will be the same as in the corresponding file in the parent /proc/[pid] directory (e.g., in a multithreaded process, all of the task/[tid]/cwd
files will have the same value as the /proc/[pid]/cwd file in the parent directory, since all of the threads in a process share a working directory). For attributes that are distinct for each
thread, the corresponding files under task/[tid] may have different values (e.g., various fields in each of the task/[tid]/status files may be different for each thread).

In a multithreaded process, the contents of the /proc/[pid]/task directory are not available if the main thread has already terminated (typically by calling pthread_exit(3)).

 

# cat /proc/1257/task/1273/comm 
nxcallback
# cat /proc/1257/task/1319/comm 
ActorMediaStore
# cat /proc/1257/task/1274/comm 
nxcallback
# cat /proc/1257/task/1359/comm 
ActorMediaManag

# cat /proc/1257/task/*/comm
media_mediaserv
Actor
Actor1257
Actorbroker
SocketIPCServer
nx_sched
nxcallback
nxcallback
nxcallback
nxcallback
nxcallback
nxcallback
nxcallback
nxcallback
nx_io_worker0
nx_io_worker1
nx_sched_low
nx_sched_high
SocketIPCClient
SocketIPCServer
ActorMediaManag
ActorMediaStore
ActorMediaManag
ActorMediaManag
UsbMonitor
ActorMediaManag
ActorMediaManag
ActorMediaManag
ActorMediaManag
ActorMediaRende
ActorNotifier

 

ref:

很奇怪,在我的主機上面打man proc竟然看不到,下面網址上面對於 /proc/[pid]/task/[tid]/comm 的解釋

http://man7.org/linux/man-pages/man5/proc.5.html

 /proc/[pid]/comm (since Linux 2.6.33)
              This file exposes the process's comm value—that is, the
              command name associated with the process.  Different threads
              in the same process may have different comm values, accessible
              via /proc/[pid]/task/[tid]/comm.  A thread may modify its comm
              value, or that of any of other thread in the same thread group
              (see the discussion of CLONE_THREAD in clone(2)), by writing
              to the file /proc/self/task/[tid]/comm.  Strings longer than
              TASK_COMM_LEN (16) characters are silently truncated.

              This file provides a superset of the prctl(2) PR_SET_NAME and
              PR_GET_NAME operations, and is employed by
              pthread_setname_np(3) when used to rename threads other than
              the caller.

 

 

 

ref:

man proc

man ps

man pstree


==============================================

以下code來自man pthread_setname_np

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
 
#define NAMELEN 16
 
#define errExitEN(en, msg) \
             do  errno  = en;  perror (msg);  exit (EXIT_FAILURE); \
         while  (0)
 
static  void  *
threadfunc( void  *parm)
{
     sleep(5);           // allow main program to set the thread name
     return  NULL;
}
 
int
main( int  argc,  char  **argv)
{
     pthread_t  thread ;
     int  rc;
     char  thread_name[NAMELEN];
 
     rc = pthread_create(& thread , NULL, threadfunc, NULL);
     if  (rc != 0)
         errExitEN(rc,  "pthread_create" );
 
     rc = pthread_getname_np( thread , thread_name, NAMELEN);
     if  (rc != 0)
         errExitEN(rc,  "pthread_getname_np" );
 
     printf ( "Created a thread. Default name is: %s\n" , thread_name);
     rc = pthread_setname_np( thread , (argc > 1) ? argv[1] :  "THREADFOO" );
     if  (rc != 0)
         errExitEN(rc,  "pthread_setname_np" );
 
     sleep(2);
 
     rc = pthread_getname_np( thread , thread_name,
                             (argc > 2) ?  atoi (argv[1]) : NAMELEN);
     if  (rc != 0)
         errExitEN(rc,  "pthread_getname_np" );
     printf ( "The thread name after setting it is %s.\n" , thread_name);
 
     rc = pthread_join( thread , NULL);
     if  (rc != 0)
         errExitEN(rc,  "pthread_join" );
       printf ( "Done\n" );
     exit (EXIT_SUCCESS);
}

  $gcc test.c -lpthread

$./a.out 
Created a thread. Default name is: a.out
The thread name after setting it is THREADFOO.
^Z
[1]+ Stopped ./a.out
$ ps H -C a.out -o 'pid tid cmd comm'
PID TID CMD COMMAND
26922 26922 ./a.out a.out
26922 26923 ./a.out THREADFOO

 

由上面可知ps H -C execfilename -o 'pid tid cmd comm'

可以得到pthread name


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值