Results 1 to 10 of 12
I am using an x86 machine with 4 cores to run a threaded C code. I want to determine which core is running which thread that is how are they ...
- 09-12-2009 #1Just Joined!
- Join Date
- Sep 2009
- Posts
- 8
Finding Which Core Your C Thread is Running On
I am using an x86 machine with 4 cores to run a threaded C code. I want to determine which core is running which thread that is how are they scheduled. Can I get some hint on this?
- 09-12-2009 #2
Hi,
if you type
in the console, you get the processor number (which may also be the number of a core on a single physical processor) a given thread is scheduled for and being executed on.Code:ps -mo pid,fname,user,psr,sgi_p
So you can either extract the info from the text output of this command or you look into "ps" how they did it.Debian GNU/Linux -- You know you want it.
- 09-12-2009 #3Just Joined!
- Join Date
- Sep 2009
- Posts
- 8
Thanks a lot I will try that and let you know
- 09-12-2009 #4Just Joined!
- Join Date
- Sep 2009
- Posts
- 8
PID COMMAND USER PSR P
16575 bash ubrahmak - -
- - ubrahmak 3 *
17978 gedit ubrahmak - -
- - ubrahmak 0 *
18108 path_p4 ubrahmak - -
- - ubrahmak 1 *
- - ubrahmak 0 0
18246 ps ubrahmak - -
- - ubrahmak 1 1
I included the code which you gave in my C code as system("ps -mo pid,fname,user,psr,sgi_p"); . I am getting the above thing. path_p4 is my code which is being executed. I get no information about the threads. Can you please help? Thanks
- 09-12-2009 #5
If I understand the manpage of ps correctly, this means your program consists of two threads. One thread is assigned to the second core but not being executed at the time being. The second thread is scheduled for the first core and this is also the core that processed the thread currently. (The second core is busy executing the ps command itself).
But don't take my word for it. "man ps" explains all possible options.Debian GNU/Linux -- You know you want it.
- 09-12-2009 #6Just Joined!
- Join Date
- Sep 2009
- Posts
- 8
Actually My code has 8 threads and a main thread
- 09-12-2009 #7Just Joined!
- Join Date
- Sep 2009
- Posts
- 8
Hey my mistake. Actually the point where I was executing this instruction, some threads exited. So its showing only 2 threads. I did that again just after threads were created and got the information. Thanks a lot
- 09-12-2009 #8Just Joined!
- Join Date
- Sep 2009
- Posts
- 8
Is it possible to know particularly which thread is running where. Like this is just telling me the processor where the process is scheduled and where it is being executed. Now if I want to know which processor is running my main thread and which one is running the other threads..example processor 0 runs the main thread, processor 1 is running thread 4 and so on...Is it possible to know that??
- 09-13-2009 #9Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Unless to do something to explicitly lock a process and/or thread to a particular cpu/core this is pretty much a meaningless exercise since the operating system will move threads and processes between cores as necessary. In any case, what is your purpose for this?
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 09-13-2009 #10Just Joined!
- Join Date
- Sep 2009
- Posts
- 8
I am designing some algorithms. To optimize the performance, I wanted to see how the threads are scheduled. Because if i am using a processor with 8 cores for example and I have 7 hreads in my program.If out of 7 threads 4 are scheduled on the same processor core due to some reason, then it is a loss of resources..


Reply With Quote
