linux c语言获取进程总数,如何使用C获取Linux中的CPU数量?

如何使用C获取Linux中的CPU数量?

是否有API可以获取Linux中可用的CPU数量?我的意思是,不使用/ proc / cpuinfo或任何其他sys-node文件...

我发现使用sched.h实现:

int GetCPUCount()

{

cpu_set_t cs;

CPU_ZERO(&cs);

sched_getaffinity(0, sizeof(cs), &cs);

int count = 0;

for (int i = 0; i < 8; i++)

{

if (CPU_ISSET(i, &cs))

count++;

}

return count;

}

但是,使用通用库是否还没有更高的层次?

7个解决方案

75 votes

#include

long number_of_processors = sysconf(_SC_NPROCESSORS_ONLN);

chrisaycock answered 2020-01-01T07:08:41Z

20 votes

此代码(从此处绘制)应在Windows和* NIX平台上均适用。

#ifdef _WIN32

#define WIN32_LEAN_AND_MEAN

#include

#else

#include

#endif

#include

#include

#include

#include

int main() {

long nprocs = -1;

long nprocs_max = -1;

#ifdef _WIN32

#ifndef _SC_NPROCESSORS_ONLN

SYSTEM_INFO info;

GetSystemInfo(&info);

#define sysconf(a) info.dwNumberOfProcessors

#define _SC_NPROCESSORS_ONLN

#endif

#endif

#ifdef _SC_NPROCESSORS_ONLN

nprocs = sysconf(_SC_NPROCESSORS_ONLN);

if (nprocs < 1)

{

fprintf(stderr, "Could not determine number of CPUs online:\n%s\n",

strerror (errno));

exit (EXIT_FAILURE);

}

nprocs_max = sysconf(_SC_NPROCESSORS_CONF);

if (nprocs_max < 1)

{

fprintf(stderr, "Could not determine number of CPUs configured:\n%s\n",

strerror (errno));

exit (EXIT_FAILURE);

}

printf ("%ld of %ld processors online\n",nprocs, nprocs_max);

exit (EXIT_SUCCESS);

#else

fprintf(stderr, "Could not determine number of CPUs");

exit (EXIT_FAILURE);

#endif

}

Vikram.exe answered 2020-01-01T07:09:01Z

18 votes

#include

#include

int main(int argc, char *argv[])

{

printf("This system has %d processors configured and "

"%d processors available.\n",

get_nprocs_conf(), get_nprocs());

return 0;

}

[https://linux.die.net/man/3/get_nprocs]

Владимир Николайчук answered 2020-01-01T07:08:25Z

13 votes

使用_SC_NPROCESSORS_ONLN是最干净,最便携式的解决方案。 万一打开失败,您可以简单地假设1 cpu或2 cpus。 除了微优化以外,依赖于了解cpus数量的代码(例如,选择要运行的理想线程数量)几乎肯定会使某些事情变得愚蠢。

_SC_NPROCESSORS_ONLN解决方案依赖于非标准(特定于glibc的)sysconf扩展,该扩展比/proc(所有Linux系统都具有/proc,但是有些系统具有非glibc libc或缺少_SC_NPROCESSORS_ONLN的glibc的旧版本)更大。

R.. answered 2020-01-01T07:09:27Z

11 votes

您一开始提到的sched_affinity()版本仍然比/proc/cpuinfo和/或_SC_NPROCESSORS_ONLN更好,因为它仅计算给定进程可用的CPU(某些进程可能会被外部进程调用的sched_setaffinity()禁用)。 唯一的更改是使用CPU_COUNT(),而不是循环执行CPU_ISSET。

RCL answered 2020-01-01T07:09:48Z

1 votes

就最近的Intel CPU而言,我个人使用以下命令:

int main()

{

unsigned int eax=11,ebx=0,ecx=1,edx=0;

asm volatile("cpuid"

: "=a" (eax),

"=b" (ebx),

"=c" (ecx),

"=d" (edx)

: "0" (eax), "2" (ecx)

: );

printf("Cores: %d\nThreads: %d\nActual thread: %d\n",eax,ebx,edx);

}

输出:

Cores: 4

Threads: 8

Actual thread: 1

或者,更简洁地说:

#include

int main()

{

unsigned int ncores=0,nthreads=0,ht=0;

asm volatile("cpuid": "=a" (ncores), "=b" (nthreads) : "a" (0xb), "c" (0x1) : );

ht=(ncores!=nthreads);

printf("Cores: %d\nThreads: %d\nHyperThreading: %s\n",ncores,nthreads,ht?"Yes":"No");

return 0;

}

输出:

Cores: 4

Threads: 8

HyperThreading: Yes

Zibri answered 2020-01-01T07:10:21Z

0 votes

扫描sys文件系统下的cpu *目录的另一种方法:

#include

#include

#include

#define LINUX_SYS_CPU_DIRECTORY "/sys/devices/system/cpu"

int main() {

int cpu_count = 0;

DIR *sys_cpu_dir = opendir(LINUX_SYS_CPU_DIRECTORY);

if (sys_cpu_dir == NULL) {

int err = errno;

printf("Cannot open %s directory, error (%d).\n", LINUX_SYS_CPU_DIRECTORY, strerror(err));

return -1;

}

const struct dirent *cpu_dir;

while((cpu_dir = readdir(sys_cpu_dir)) != NULL) {

if (fnmatch("cpu[0-9]*", cpu_dir->d_name, 0) != 0)

{

/* Skip the file which does not represent a CPU */

continue;

}

cpu_count++;

}

printf("CPU count: %d\n", cpu_count);

return 0;

}

Sunil Bojanapally answered 2020-01-01T07:10:41Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值