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

2024-05-28 06:58

本文主要是介绍如何看出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, thecommand name associated with the process.  Different threadsin the same process may have different comm values, accessiblevia /proc/[pid]/task/[tid]/comm.  A thread may modify its commvalue, or that of any of other thread in the same thread group(see the discussion of CLONE_THREAD in clone(2)), by writingto the file /proc/self/task/[tid]/comm.  Strings longer thanTASK_COMM_LEN (16) characters are silently truncated.This file provides a superset of the prctl(2) PR_SET_NAME andPR_GET_NAME operations, and is employed bypthread_setname_np(3) when used to rename threads other thanthe 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


这篇关于如何看出process下面有幾個thread and thread name的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1009850

相关文章

jmeter之Thread Group(线程组)

Thread Group(线程组) 1.线程组,或者可以叫用户组,进行性能测试时的用户资源池。 2.是任何一个测试计划执行的开始点。 3.上一篇提到的“控制器”和“HTTP请求”(采集器)必须在线程组内;监听器等其他组件,可以直接放在测试计划下。 线程组设置参数的意义 我们以下图为例,进行详细说明。见下图:  区域1(在取样器错误后要执行的动作) 这个区域的主要作用很明显,在线程内

Exception processing async thread queue Exception processing async thread queue

错误信息描述: eclipse 在debug中弹出异常信息框 Exception processing async thread queue Exception processing async thread queue 解决方法: eclipse 中有一个Expressions窗口,里面添加的 expression 没有正确执行,并且没有删除,只要 Remove All Expressio

no thread-bound request found:are you referring to request

问题描述: 通过webservice接口调用程序时,发现在执行查询的时候一直报一个错误,错误信息如下: java.lang.IllegalStateExceptino:No thread-bound request found:are you referring to request attributes outside of an actual web request,or processi

FFplay源码分析-video_thread

《FFmpeg原理》的社群来了,想加入社群的朋友请购买 VIP 版,VIP 版有更高级的内容与答疑服务。 本系列 以 ffmpeg4.2 源码为准,下载地址:链接:百度网盘 提取码:g3k8 FFplay 源码分析系列以一条简单的命令开始,ffplay -i a.mp4。a.mp4下载链接:百度网盘,提取码:nl0s 。 上一篇文章已经讲解完了 audio_thread() 音频解码

FFplay源码分析-read_thread

《FFmpeg原理》的社群来了,想加入社群的朋友请购买 VIP 版,VIP 版有更高级的内容与答疑服务。 本系列 以 ffmpeg4.2 源码为准,下载地址:链接:百度网盘 提取码:g3k8 FFplay 源码分析系列以一条简单的命令开始,ffplay -i a.mp4。a.mp4下载链接:百度网盘,提取码:nl0s 。 如下图所示,本文主要讲解 read_thread() 函数的内

「生信Debug」OpenBLAS blas_thread_init: pthread_create: Resource temporarily unavailable

BLAS(Basic Linear Algebra Subprograms),翻译为基础线性代数子程序库,里面拥有大量已经编写好的关于线性代数运算的程序。OpenBLAS是其中一个实现了相关运算的开源程序库,其他软件在开发的时候就不需要额外造轮子,直接调用相关的API即可。 之前在使用OrthoFinder遇到了类似的问题,见https://github.com/davidemms/OrthoF

列出某个目录下面所有的文件与目录

import java.io.File; public class wenjian {     /**      * 遍历该对应对应的数组      */     public static void main(String[] args) {              File file=new File("d:/某银行新规面资料");         if(file.exists()){

关于使用SetPriorityClass将进程设置为PROCESS_MODE_BACKGROUND_BEGIN的一点总结

一、背景 早上在B站看到了下面这个视频 【Win系统旧代码导致CPU干冒烟?谷歌程序员惨背锅】 然后想起自己上一年处理了公司某个项目的同样的问题,于是就来总结一下使用SetPriorityClass将进程设置为PROCESS_MODE_BACKGROUND_BEGIN后的相关问题。 二、代码 下面是一个demo代码,我们先来看下代码的正常运行情况下在procexp下的表现。procexp

std :: this_thread

this_thread 包装了一组可以访问当前线程信息的函数 functions 1、get_id()      获取当前线程的id。 // thread::get_id / this_thread::get_id#include <iostream> // std::cout#include <thread> // std::thread, std

std :: thread

头文件<thread>中包含两个类,thread和this_thread 在讲thread之前,首先要了解线程的一个状态:joinable 一个正规初始化后的线程是一个可执行线程,这种线程joinable为true,并且有一个独一无二的线程id。 相反,一个默认构造函数构造出的thread(thread(),没有参数,未初始化),joinable为false,且线程id通常和其他所有的non