Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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 ...
  1. #1
    ugb
    ugb is offline
    Just 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?

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Hi,

    if you type
    Code:
    ps -mo pid,fname,user,psr,sgi_p
    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.

    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.

  3. #3
    ugb
    ugb is offline
    Just Joined!
    Join Date
    Sep 2009
    Posts
    8
    Thanks a lot I will try that and let you know

  4. #4
    ugb
    ugb is offline
    Just 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

  5. #5
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Quote Originally Posted by ugb View Post
    18108 path_p4 ubrahmak - -
    - - ubrahmak 1 *
    - - ubrahmak 0 0
    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.

  6. #6
    ugb
    ugb is offline
    Just Joined!
    Join Date
    Sep 2009
    Posts
    8
    Actually My code has 8 threads and a main thread

  7. #7
    ugb
    ugb is offline
    Just 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

  8. #8
    ugb
    ugb is offline
    Just 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??

  9. #9
    Linux Guru Rubberman's Avatar
    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!

  10. #10
    ugb
    ugb is offline
    Just 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..

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...