How to track the threads created for a process in linux?
linux
Solution
On Terminal if you know the process-id you can run the command
"ps -e -T | grep<pid-no>" It will show all the threads for the process(pid-n0)
Or you can write a sample program
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);
}
Problem
I am newbie to linux. I want to track the threads created by the process like any command or program to know the present threads running for a particular process. I found this link Tracking threads memory and CPU consumption but didn't find my solution. I am not asking about any tool, library where and how it is implemented but indeed some programming point of view. This means I'd like to know what APIs are available to inspect a given process's thread count, memory and CPU consumption.