How can I know in a C program, on which physical processor and core my code is running? I'm using Linux and gcc 4.4.3.
相关问题
- Multiple sockets for clients to connect to
- Is shmid returned by shmget() unique across proces
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- how to get running process information in java?
You can get the affinity of the process to a processor using GCC. The CPU affinity APIs might help you out. Are you trying to use this information to ensure your process is not interrupted or such high priority task?
By and large it's hard to find this out in a meaningful way. Your thread will often run on many different processors during its life. You could call a function to ask which processor you are on and get a context switch whilst the function is executing. What should the function return?
call returns virtual CPU number. Mapping of virtual CPU to real CPU info is in /proc/cpuinfo.
If your system supports VDSO, then
sched_getcpu()
is relatively fast.CPU number can be also obtained using
CPUID
instruction, but it is slower thansched_getcpu()
.You may check /proc//stat file system, according to http://www.kernel.org/doc/Documentation/filesystems/proc.txt, you should just check task_cpu flag.
As example without correct types and error checking:
See http://en.wikipedia.org/wiki/CPUID#Accessing_the_id_from_other_languages
What you want is the APIC ID... basically: