<分区>
我是 Linux 新手。
我想像跟踪任何命令或程序一样跟踪进程创建的线程,以了解当前为特定进程运行的线程。
我找到了这个链接 Tracking threads memory and CPU consumption但没有找到我的解决方案。
我不是在问任何工具、库在哪里以及如何实现,而是一些编程观点。这意味着我想知道哪些 API 可用于检查给定进程的线程数、内存和 CPU 消耗。
标签 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/