linux - 如何跟踪在 Linux 中为进程创建的线程?

标签 linux

<分区>

我是 Linux 新手。

我想像跟踪任何命令或程序一样跟踪进程创建的线程,以了解当前为特定进程运行的线程。

我找到了这个链接 Tracking threads memory and CPU consumption但没有找到我的解决方案。

我不是在问任何工具、库在哪里以及如何实现,而是一些编程观点。这意味着我想知道哪些 API 可用于检查给定进程的线程数、内存和 CPU 消耗。

最佳答案

如果您知道进程 ID,则可以在终端上运行命令

 "ps -e -T | grep<pid-no>"  It will show all the threads for the process(pid-n0)

或者你可以写一个示例程序

pthread_t ntid;
void printids(const char *s)
{
    pid_t pid;
    pthread_t tid;

    pid=getpid();
    tid=pthread_self();
    printf("%s pid %u tid %u(0x%x)\n",s,(unsigned int)pid, (unsigned int)tid, (unsigned   
       int)tid);
 }
void *thr_fn(void *arg)
{
    printids("new thread");
    return ((void *)0);
}
int main(int argc, char *argv[])
{
    int err;
    err=pthread_create(&ntid,NULL,thr_fn,NULL);
    if(err!=0)
            cout<<"can't create thread "<<strerror(err);
    printids("main thread");
    sleep(1);
    exit(0);
}

关于linux - 如何跟踪在 Linux 中为进程创建的线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20054393/

相关文章:

linux - 找不到子文件夹中的 header

linux - EDF算法的替代方案

linux - 远程访问 glassfish

linux - Lazarus 编译器的 Synaser 串行通信工具是否有任何替代品?

python - 如何提交调用我的 python 脚本的 SGE 作业

linux - 如何在每个月的第一个星期四安排一个 cron

c++ - g++ 不产生调试符号

java - 如何解决在 Java 中找不到符号?

linux - Shell 在 jarsigner 中传递 keystore 路径参数

python - 为什么我更新了.py文件后还要重新运行python?