I am currently working on a project about setting process to one core in linux environment. I use sched_setaffinity to do this job and I wonder whether there are some functions provided by linux to get which core the process is running on. I use top command and find it could get this info using j option. So i am sure there are some ways to get this info in user space.
Asked
Active
Viewed 2,568 times
2 Answers
5
You probably want sched_getcpu()
. If you are running an older version of glibc, you can read the 39th field of /proc/[pid]/stat
for the appropriate pid -- see the proc(5)
man page for more details.
![](../../users/profiles/333504.webp)
llasram
- 4,045
- 22
- 26
-
Unfortunately, my Glibc version is under 2.6 so it could not work – terry Sep 12 '10 at 03:56
-
Ok. Modified my answer to include a solution for pre-2.6 glibcs. – llasram Sep 12 '10 at 09:36
1
You can use inline assembly (on a x86 arch) to achieve this:
mov eax, 1 ; cpuid functionality depends on the value of eax
cpuid ; get cpu info
shr ebx, 24 ; ebx[31:24] is the cpu ID.
mov eax, ebx ; eax contains the cpu ID
you can read more about CPUID instruction here http://download.intel.com/design/processor/applnots/24161832.pdf
![](../../users/profiles/398206.webp)
AmirW
- 776
- 8
- 20
-
Thanks for your suggestions, however my cpu is xeon 5560 and the os is RHEL 5.4 64bit. Could CPUID still work as registers all changed names? – terry Sep 12 '10 at 03:59