在Linux上显示正在运行的进程的线程ID

在上Linux,“ ps -T”可以显示正在运行的进程的线程信息:

# ps -T 2739
PID SPID TTY STAT TIME COMMAND
2739 2739 pts/0 Sl 0:00 ./spawn_threads
2739 2740 pts/0 Sl 0:00 ./spawn_threads
2739 2741 pts/0 Sl 0:00 ./spawn_threads

proc伪文件系统上,有一个task目录来记录线程信息:

# ls -lt /proc/2739/task
total 0
dr-xr-xr-x 7 root root 0 Jun 28 14:55 2739
dr-xr-xr-x 7 root root 0 Jun 28 14:55 2740
dr-xr-xr-x 7 root root 0 Jun 28 14:55 2741

由于C++17,有一个文件系统库可用于访问文件系统,因此我利用该库遍历/proc/$pid/task文件夹以获取进程的线程ID:

    ......
std::filesystem::path p{"/proc"};
p /= argv[1];
p /= "task";
......
uint64_t thread_num{};
std::vector<std::string> thread_id; std::filesystem::directory_iterator d_it(p);
for (const auto& it : d_it)
{
thread_num++;
thread_id.push_back(it.path().filename().string());
} std::cout << "Process ID (" << argv[1] << ") has " << thread_num << " threads, and ids are:\n";
for (const auto& v : thread_id)
{
std::cout << v << '\n';
}
......

生成并运行它:

# ./show_thread_ids 2739
Process ID (2739) has 3 threads, and ids are:
2739
2740
2741

编程资料 https://www.cppentry.com

在Linux上显示正在运行的进程的线程ID的更多相关文章

  1. Windows 和 Linux 上Redis的安装守护进程配置

    # Windows 和 Linux 上Redis的安装守护进程配置 Redis 简介 ​ Redis是目前最常用的非关系型数据库(NOSql)之一,常以Key-Value的形式存储.Redis读写速度 ...

  2. Linux查看非root运行的进程

    Linux查看非root运行的进程 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ ps -U root -u root -N PID TTY TIME CMD ...

  3. Linux查看当前正在运行的进程

    Linux查看当前正在运行的进程 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ ps PID TTY TIME CMD 2576 pts/0 00:00:00 ...

  4. XVFB实现selenium在linux上无界面运行安装篇

    selenium在linux上无界面运行,其实是非常简单的.具体的方法有使用HtmlUnitDriver或者PhantomJSDriver,有时间我会写写关于这两个东东的文章,其实基本和ChromeD ...

  5. 在Linux上rpm安装运行Redis 3.0.4

    http://www.rpmfind.net搜索redis,找到redis3.0.4的rpm源选做 wget ftp://fr2.rpmfind.net/linux/remi/enterprise/6 ...

  6. [转帖]Linux系统进程的知识总结,进程与线程之间的纠葛...

    Linux系统进程的知识总结,进程与线程之间的纠葛... https://cloud.tencent.com/developer/article/1500509 当一个程序开始执行后,在开始执行到执行 ...

  7. 在Linux上显示某个进程的线程的几种方式

    方法一:PS 在ps命令中,"-T"选项可以开启线程查看.下面的命令列出了由进程号为的进程创建的所有线程. 1.$ ps -T -p 方法二: Top top命令可以实时显示各个线 ...

  8. 在 linux 上部署并运行 JavaFX 项目

    环境 redhat 6.4.eclipse安装JavaFX插件 项目详情及代码参见 在linux上配置并创建JavaFX项目 ,该部署即此文章中项目的部署 配置build.fxbuild 生成buil ...

  9. Linux nohup和&后台运行,进程查看及终止,进程信息输出,控制台信息输出

    nohup和&后台运行,进程查看及终止   1.nohup 用途:不挂断地运行命令. 语法:nohup Command [ Arg … ] [ & ] 无论是否将 nohup 命令的输 ...

随机推荐

  1. C/C++ memcpy函数的用法

    功能 memcpy指的是c和c++使用的内存拷贝函数,memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中 头文件 所在头文件 <s ...

  2. SVN一直清理解决

    svn作为我们经常使用的版本管理服务器,在使用过程中经常需要通过clean up操作来完成本地文件与服务器文件信息及版本信息同步,然而有时会在执行清理命令时提示“清理失败,请执行清理”,并且提示的中文 ...

  3. Android之布局RelativeLayout

    线性布局的weight属性在等比例分配时比较方便,但是对复杂的界面,嵌套多层LinearLayout布局会导致渲染变慢,占用更多系统资源:而使用RelativeLayout的话,可能仅仅需要一层就可以 ...

  4. git clone 拉取github上面的代码报错:fatal: Authentication failed for xxx解决

    1.打开git bash,输入密码:git config --system --unset credential.helper2.结果报错:error: could not lock config f ...

  5. [HNOI2019]序列(单调栈+二分)

    通过打表证明发现答案就是把序列划分成若干段,每段的b都是这一段a的平均数.50分做法比较显然,就是单调栈维护,每次将新元素当成一个区间插入末尾,若b值不满足单调不降,则将这个区间与单调栈前一个区间合并 ...

  6. sm4 加解密示例

    int do_crypt(char *outfile) { unsigned ]; int outlen, tmplen; /* Bogus key and IV: we'd normally set ...

  7. eclipse web 项目配置 运行

  8. F. Rhyme scheme

    题: https://nanti.jisuanke.com/t/41414 #include<bits/stdc++.h> using namespace std; typedef __i ...

  9. HQL语句简单介绍

    HQL查询:Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查询特性,因此 Hib ...

  10. [LC] 298. Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...