获取linux系统的函数,Linux获取系统信息函数

Linux中,可以用sysinfo来获取系统相关信息。

sysinfo结构体

Linux中,可以用sysinfo来获取系统相关信息。

Linux中,sysinfo是用来获取系统相关信息的结构体。

函数声明和原型:

#include

int sysinfo(struct sysinfo *info);

结构体信息:

在Linux

2.3.16中,结构体的信息是(单位是字节):

struct

sysinfo

{

long

uptime;

unsigned

long

loads[3];

unsigned

long totalram;

unsigned

long freeram;

unsigned

long sharedram;

unsigned

long bufferram;

unsigned

long totalswap;

unsigned

long freeswap;

unsigned

short procs;

char

_f[22];

在Linux 2.3.23(i386), 2.3.48 (all

architectures)中,结构体的信息是(单位是mem_unit字节):

struct

sysinfo {

long

uptime;

unsigned long

loads[3];

unsigned long totalram;

unsigned long freeram;

unsigned long sharedram;

unsigned long bufferram;

unsigned long totalswap;

unsigned long freeswap;

unsigned short

procs;

unsigned long totalhigh;

unsigned long freehigh;

unsigned int mem_unit;

char

_f[20-2*sizeof(long)-sizeof(int)];

#include

_syscall1(int, sysinfo, struct sysinfo*,

info);

//using

namespace std;

int main(int

argc, char *agrv[])

{

struct

sysinfo s_info;

int

error;

error =

sysinfo(&s_info);

printf("\n\ncode error=%d\n",error);

printf("Uptime = %ds\nLoad: 1 min%d / 5 min %d / 15 min

%d\n"

"RAM: total %d / free %d /shared%d\n"

"Memory in buffers =

%d\nSwap:total%d/free%d\n"

"Number of processes = %d\n",

s_info.uptime, s_info.loads[0],

s_info.loads[1], s_info.loads[2],

s_info.totalram, s_info.freeram,

s_info.totalswap, s_info.freeswap,

s_info.procs);

return

0;

}

输出结果:

#/home/chenhd/future>

./Sysinfo_1

code

error=0

Uptime =

154s

Load: 1

min22976 / 5 min 13888 / 15 min 5408

RAM: total

128307200 / free 55656448 /shared263168000

Memory in

buffers = 263168000

Swap:total50/free1073821804

Number of

processes = 1073823352

#/home/chenhd/future>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值