Find the answer to your Linux question:
Results 1 to 8 of 8
Is there any way I can get the current process/thread ID on each cpu(quad core system) in my own program? I guess there may be some function calls I can ...
  1. #1
    Just Joined!
    Join Date
    Aug 2009
    Posts
    10

    Get Process ID on each core

    Is there any way I can get the current process/thread ID on each cpu(quad core system) in my own program?
    I guess there may be some function calls I can use in Linux, but I am not sure.

    Thanks

  2. #2
    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
    I'm not sure I understand what you are trying to find out. Are you trying to determine what processes are active on a specific CPU/core? Why? FWIW, the OS can/does move processes and threads between cores at will. What you see at one point in time, may not be the same a few milliseconds later.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Aug 2009
    Posts
    10
    That's exactly what I want to find out. Is there any way I can do this?

    I know OS scheduler will swap process/thread in and out frequently, but my intention is to control on which cpu a thread can be scheduled to run, through CPU affinity function call.

    Thanks for your reply.

  4. #4
    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
    Honestly, unless you have a REALLY good reason for this, I cannot recommend that you do this (setting CPU affinity for a process). This might be useful for hard realtime applications, but then you should be running the realtime scheduler for that. A LOT of work has gone into optimizing the Linux kernel for SMP processing on multi-core and multi-cpu systems. It is doubtful that in other than extraordinary cases you would be able to improve the efficiency of the system, and it all likelyhood will end up with a system with inferior performance characteristics.

    Just a quick question. How much experience do you have in the design and implementation of smp schedulers and kernels? Unless you have at least 5-10 years of intense experience in this domain, then don't waste your time. If it is a learning experience you are looking for, then experiment with the standard linux schedulers in the kernel. You'll learn more that way, and can, if you get good at it, influence the development of the kernel in a way that will benefit all of us.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Aug 2009
    Posts
    10
    Thank Linux guru for your good suggestions.

    I don't intend to write a real time scheduler.
    Currently I'm working on new SMP architecture, so mainly I am concentrating on hardware level, if I have a good reason(like power consumption) to migrate a thread from cpu0 to cpu1, I can't just swap cpu resgiters value between these two cpus, which will crash the OS.

    That's why I need OS's help to do this for me.
    It seems the only function I can use is to set CPU affinity, and I need the PID on each core first, but I don't know how to get it.

    I really appreciate your help.

  6. #6
    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
    Quote Originally Posted by fisherg View Post
    Thank Linux guru for your good suggestions.

    I don't intend to write a real time scheduler.
    Currently I'm working on new SMP architecture, so mainly I am concentrating on hardware level, if I have a good reason(like power consumption) to migrate a thread from cpu0 to cpu1, I can't just swap cpu resgiters value between these two cpus, which will crash the OS.

    That's why I need OS's help to do this for me.
    It seems the only function I can use is to set CPU affinity, and I need the PID on each core first, but I don't know how to get it.

    I really appreciate your help.
    Sorry, but I'm not sure how to either, off hand (still getting up to speed on Linux kernel programming). I would suggest that you read all the documentation you can find. Two sites you must visit are:

    1. The Linux Kernel Archives
    2. The Linux Documentation Project
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  7. #7
    Just Joined!
    Join Date
    Aug 2009
    Posts
    10
    Thank you for your quick reply.You are really a kind person.

    In linux shell, if I know the program name, I can get PID with command like "pgrep -x name".
    I am wondering whether I can use something like this in C code, to get the PID if I know the name of the process.

  8. #8
    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
    Quote Originally Posted by fisherg View Post
    Thank you for your quick reply.You are really a kind person.

    In linux shell, if I know the program name, I can get PID with command like "pgrep -x name".
    I am wondering whether I can use something like this in C code, to get the PID if I know the name of the process.
    Well, if you have the linux sources, you could easily enough look at the source for pgrep and see how that tool does it.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

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