如何看出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

相关文章

Thread如何划分为Warp?

1 .Thread如何划分为Warp? https://jielahou.com/code/cuda/thread-to-warp.html  Thread Index和Thread ID之间有什么关系呢?(线程架构参考这里:CUDA C++ Programming Guide (nvidia.com)open in new window) 1维的Thread Index,其Thread

Unity Post Process Unity后处理学习日志

Unity Post Process Unity后处理学习日志 在现代游戏开发中,后处理(Post Processing)技术已经成为提升游戏画面质量的关键工具。Unity的后处理栈(Post Processing Stack)是一个强大的插件,它允许开发者为游戏场景添加各种视觉效果,如景深、色彩校正、辉光、模糊等。这些效果不仅能够增强游戏的视觉吸引力,还能帮助传达特定的情感和氛围。 文档

RT-Thread(Nano版本)的快速移植(基于NUCLEO-F446RE)

目录 概述 1 RT-Thread 1.1 RT-Thread的版本  1.2 认识Nano版本 2 STM32F446U上移植RT-Thread  2.1 STM32Cube创建工程 2.2 移植RT-Thread 2.2.1 安装RT-Thread Packet  2.2.2 加载RT-Thread 2.2.3 匹配相关接口 2.2.3.1 初次编译代码  2.2.3.

GTK中创建线程函数g_thread_new和g_thread_create的区别

使用GThread函数,需要引用glib.h头文件。 这两个接口的核心区别就是  g_thread_create 是旧的接口,现在已经不使用了,而g_thread_new是新的接口,建议使用。 g_thread_create: g_thread_create has been deprecated since version 2.32 and should not be used in n

基于 rt-thread的I2C操作EEPROM(AT24C02)

一、AT24C02 The AT24C01A/02/04/08A/16A provides 1024/2048/4096/8192/16384 bits of serial electrically erasable and programmable read-only memory (EEPROM) organized as 128/256/512/1024/2048 words of 8 b

[项目][CMP][Thread Cache]详细讲解

目录 1.设计&结构2.申请内存3.释放内存4.框架 1.设计&结构 Thread Cache是哈希桶结构,每个桶是一个按桶位置映射大小的内存块对象的自由链表 每个线程都会有一个Thread Cache对象,这样每个线程在这里获取对象和释放对象时是无锁的 TLS – Thread Local Strorage Linux gcc下TLSWindows vs下TLS

线程池工具类——Thread学习笔记

记录一下线程池工具类: /*** 线程池工具类* @author lixiang* @date 2018年10月10日 - 11:10* @history 2018年10月10日 - 11:10 lixiang create.*/public class ThreadPoolHelper {private static final Logger logger = LoggerFactory.g

模拟线程死锁——Thread学习笔记

记录一下之前写过的一段模拟死锁的代码: /*** 模拟死锁** @author lixiang* @date 2018年10月12日 - 9:51* @history 2018年10月12日 - 9:51 lixiang create.*/public class HoldLockDemo {private static Object[] lock = new Object[10];priv

简单的启动页滑动动画,下面带圆点的切换

一个简单实用的滑动页动画,开始之前我们先认识一个空间,ViewPager,他是google SDk中自带的一个类,可以用来实现屏幕之间的切换。 先上代码,一看就明了!! GlideActivity.ava import java.util.ArrayList;import android.app.Activity;import android.content.Intent

出现 E: Sub-process /usr/bin/dpkg returned an error code (1) 解决方法 (全面分析)

目录 前言1. 问题所示2. 原理分析2.1 第一阶段2.2 第二阶段 3. 解决方法4. 彩蛋4.1 错误不提示,直接卸载4.2 卸载后还是无错误提示 前言 3年前遇到过一个类似的,但是轻松解决,推荐阅读:ubuntu:E: dpkg was interrupted, you must manually run ‘sudo dpkg --configure…解决方法 这回发