我如何知道进程正在哪个核心上运行?

发布于 2024-09-18 21:59:00 字数 159 浏览 5 评论 0 原文

我目前正在开发一个关于在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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千笙结 2024-09-25 21:59:00

您可能需要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.

撩动你心 2024-09-25 21:59:00

您可以使用内联汇编(在 x86 架构上)来实现此目的:

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

您可以在此处阅读有关 CPUID 指令的更多信息 http://download.intel.com/design/processor/applnots/24161832.pdf

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文