sysconf

本文介绍如何通过sysconf系统调用来获取计算机的一些配置信息,如页面大小、CPU个数、物理内存容量等,并展示了示例代码及运行结果。同时,解释了如何从/usr/include/i386-linux-gnu/bits/confname.h文件中查找支持的输入参数,以及如何将获取到的物理内存大小转换为GB,并与/proc/meminfo文件中的信息进行对比。

要点:通过sysconf系统调用,获取计算机的一些配置信息,如页面大小、CPU个数、物理内存的容量,等等。


详细信息:man sysconf


示例代码:

#include <unistd.h>
#include <stdio.h>

int main() 
{
    long pages_of_physical_memory = sysconf(_SC_PHYS_PAGES);

    printf("pages of physical memory: %ld\n", sysconf(_SC_PHYS_PAGES));
    printf("page size: %ld\n", sysconf(_SC_PAGE_SIZE));
    printf("avaiable pages: %ld\n", sysconf(_SC_AVPHYS_PAGES));
    printf("processors: %ld\n", sysconf(_SC_NPROCESSORS_CONF));
    printf("processors available: %ld\n", sysconf(_SC_NPROCESSORS_ONLN));

    return 0;
}

运行结果:

flying-bird@flyingbird:~/examples/cpp/sysconf$ gcc main.c
flying-bird@flyingbird:~/examples/cpp/sysconf$ ./a.out 
pages of physical memory: 514979
page size: 4096
avaiable pages: 46753
processors: 2
processors available: 2
flying-bird@flyingbird:~/examples/cpp/sysconf$ 

sysconf支持的输入参数的取值,可以从/usr/include/i386-linux-gnu/bits/confname.h中获取。

查找该文件的过程:

flying-bird@flyingbird:~/examples/cpp/sysconf$ cd /usr/include
flying-bird@flyingbird:/usr/include$ find . -name "*.h" | xargs grep "_SC_PAGE_SIZE"
./i386-linux-gnu/bits/confname.h:#define	_SC_PAGE_SIZE			_SC_PAGESIZE
flying-bird@flyingbird:/usr/include$ 

根据以上内容,要打印出机器的物理内存的大小,只需要简单计算:

printf("total memory: %.2fG\n", sysconf(_SC_PAGE_SIZE) * sysconf(_SC_PHYS_PAGES) / 1024 / 1024 / 1024.0);

如:

total memory: 1.96G

可以将以上信息和/proc虚拟文件系统的内容进行对比:

flying-bird@flyingbird:~/examples/cpp/sysconf$ cat /proc/meminfo
MemTotal:        2059916 kB
MemFree:          155416 kB
Buffers:          107992 kB
Cached:           845224 kB
SwapCached:         2712 kB
Active:          1032944 kB
Inactive:         637224 kB
Active(anon):     508080 kB
Inactive(anon):   211476 kB
Active(file):     524864 kB
Inactive(file):   425748 kB
Unevictable:           0 kB
Mlocked:               0 kB
HighTotal:       1182280 kB
HighFree:          18788 kB
LowTotal:         877636 kB
LowFree:          136628 kB
SwapTotal:       3998716 kB
SwapFree:        3993552 kB
Dirty:               772 kB
Writeback:             0 kB
AnonPages:        714980 kB
Mapped:           135424 kB
Shmem:              2596 kB
Slab:              93972 kB
SReclaimable:      78032 kB
SUnreclaim:        15940 kB
KernelStack:        3216 kB
PageTables:         8552 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     5028672 kB
Committed_AS:    2877256 kB
VmallocTotal:     122880 kB
VmallocUsed:       58748 kB
VmallocChunk:      62016 kB
HardwareCorrupted:     0 kB
AnonHugePages:         0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:       63480 kB
DirectMap2M:      849920 kB
flying-bird@flyingbird:~/examples/cpp/sysconf$ cat /proc/cpuinfo
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 15
model name	: Intel(R) Pentium(R) Dual  CPU  E2200  @ 2.20GHz
stepping	: 13
microcode	: 0xa3
cpu MHz		: 1200.000
cache size	: 1024 KB
physical id	: 0
siblings	: 2
core id		: 0
cpu cores	: 2
apicid		: 0
initial apicid	: 0
fdiv_bug	: no
hlt_bug		: no
f00f_bug	: no
coma_bug	: no
fpu		: yes
fpu_exception	: yes
cpuid level	: 10
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc arch_perfmon pebs bts aperfmperf pni dtes64 monitor ds_cpl est tm2 ssse3 cx16 xtpr pdcm lahf_lm dtherm
bogomips	: 4389.29
clflush size	: 64
cache_alignment	: 64
address sizes	: 36 bits physical, 48 bits virtual
power management:

processor	: 1
vendor_id	: GenuineIntel
cpu family	: 6
model		: 15
model name	: Intel(R) Pentium(R) Dual  CPU  E2200  @ 2.20GHz
stepping	: 13
microcode	: 0xa3
cpu MHz		: 1200.000
cache size	: 1024 KB
physical id	: 0
siblings	: 2
core id		: 1
cpu cores	: 2
apicid		: 1
initial apicid	: 1
fdiv_bug	: no
hlt_bug		: no
f00f_bug	: no
coma_bug	: no
fpu		: yes
fpu_exception	: yes
cpuid level	: 10
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc arch_perfmon pebs bts aperfmperf pni dtes64 monitor ds_cpl est tm2 ssse3 cx16 xtpr pdcm lahf_lm dtherm
bogomips	: 4389.58
clflush size	: 64
cache_alignment	: 64
address sizes	: 36 bits physical, 48 bits virtual
power management:

flying-bird@flyingbird:~/examples/cpp/sysconf$ 




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值