简体   繁体   English

如何在 zsh 或 bash 中获取逻辑 CPU 的数量?

[英]How do I get the # of logical CPUs in zsh or bash?

How do I get the # of logical CPUs in zsh or bash?如何在 zsh 或 bash 中获取逻辑 CPU 的数量? I want to create a makep alias that passes the # of cpus to 'make -j'.我想创建一个 makep 别名,将 cpus 号传递给“make -j”。

For Linux: 对于Linux:

grep -c proc /proc/cpuinfo

The shell doesn't matter. 外壳无关紧要。 This will work in any. 这将适用于任何情况。

The simpelst solution is to call "nproc" which just returns the number of logical cores simpelst解决方案是调用“ nproc”,它仅返回逻辑核的数量

nproc --all give you all installed processors (on openvz this returns the cpus of the system, not of the maybe limited number in your container!) nproc --all为您提供所有已安装的处理器(在openvz上,它返回系统的cpus,而不是容器中可能有限的数量!)

nproc without any option gives you the number of cores available to the current process (so bash or zsh in your case) nproc is part of coreutils. 不带任何选项的nproc为您提供了当前进程可用的内核数量(因此,您的情况是bash或zsh)nproc是coreutils的一部分。

No need to grep :) 无需grep :)

For more info see "man nproc" 有关更多信息,请参见“ man nproc”。

HTH 高温超导

On macOS you can do:在 macOS 上,您可以执行以下操作:

sysctl -n hw.ncpu

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM