3

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.

1
  • Just want some code type help Commented Nov 18, 2013 at 17:52

2 Answers 2

4

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); } 
Sign up to request clarification or add additional context in comments.

Comments

0

The top command in Linux/Unix displays info on active processes: the command that was run, memory usage, CPU usage, thread usage, state, and I/O usage. You can parse this information using shell scripting. Check out grep and | (pipe).

Comments