gpt4 book ai didi

linux - 如何从用户空间应用程序(Linux、C)中获取 CPU 核心数?

转载 作者:行者123 更新时间:2023-11-30 17:51:43 34 4
gpt4 key购买 nike

大概有一个库或简单的 asm blob 可以获取我正在执行的当前 CPU 的编号。

最佳答案

使用sched_getcpu来确定调用线程正在运行的CPU。请参阅 man getcpu (系统调用)和 man sched_getcpu (库包装器)。但是,请注意它的内容:

The information placed in cpu is only guaranteed to be current at the time of the call: unless the CPU affinity has been fixed using sched_setaffinity(2), the kernel might change the CPU at any time. (Normally this does not happen because the scheduler tries to minimize movements between CPUs to keep caches hot, but it is possible.) The caller must be prepared to handle the situation when cpu and node are no longer the current CPU and node.

关于linux - 如何从用户空间应用程序(Linux、C)中获取 CPU 核心数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16548744/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com