我如何知道进程在哪个核心上运行? [英] How could I know which core a process is running on?
问题描述
我目前正在从事一个有关在Linux环境中将进程设置为一个核心的项目.我使用sched_setaffinity来完成这项工作,并且我想知道linux是否提供了一些功能来获取进程正在哪个内核上运行.我使用top命令,发现它可以使用j选项获取此信息.因此,我敢肯定有一些方法可以在用户空间中获取此信息.
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.
推荐答案
您可能需要 sched_getcpu()
.如果您运行的是旧版本的glibc,则可以阅读/proc/[pid]/stat 的第39个字段以获取相应的pid-请参见
proc(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.
这篇关于我如何知道进程在哪个核心上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!