How could I know which core a process is running on?

孤街醉人 提交于 2019-12-23 12:55:39

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/3691331/how-could-i-know-which-core-a-process-is-running-on

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!