LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-27-2012, 11:30 PM   #1
utkarshrawat
Member
 
Registered: Jul 2007
Posts: 120

Rep: Reputation: 15
Command for thread


Hi
I have a C program that consist of 5 to 6 threads ,is there any command where I can see which thread has run the most.
 
Old 06-28-2012, 12:06 AM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &timespec) reports the CPU time the current thread has used. For example, you could set up a (process-wide) signal handler (say for SIGRTMIN+1). The handler would call pthread_self() , locate the matching thread identifier in an array, then call clock_gettime() with the corresponding timespec entry in the array, to update the CPU time used by the thread. Any thread in the process can then use pthread_kill() to make each thread transparently update the CPU time they've used. (Other processing in that thread will not be affected, except that blocking syscalls may return EINTR. But you should always handle that correctly anyway, since it may happen whenever a signal is delivered anyway. And a lot of libraries use signals internally, so EINTR just do happen.)

Another approach is to read /proc/PID/task/TID/stat. The twelfth field after the close parenthesis tells the time spent by the thread in userspace, and the thirteenth the time spent by the thread in kernel, in system ticks (sysconf(_SC_CLK_TCK) ticks per second). I recommend reading the first line, then removing everything up to and including the final ), and then parsing the fields using sscanf(). Any other process belonging to the same user can do this.

Note that TID is a value returned by a gettid() call, and are not related to pthread_t values. You either need to have the threads report their gettid() and pthread_self() somehow, or you cannot really say which thread is which from outside the process.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python thread safety: printing from thread to redirected stdout - safe? BrianK Programming 2 10-11-2010 11:28 AM
command thread help drmrrc Linux - Newbie 1 12-04-2009 07:02 PM
Command to list stack size of each thread in an application mannw72 Linux - Newbie 1 03-14-2007 06:31 PM
unix command thread status similar to ps powah Linux - Newbie 1 09-29-2006 10:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration