系统运行状态信息是我们关注的重点,通过当前系统的输出信息,如内存大小、进程数量、运行时间等,以便分析CPU负载、软硬件资源占用情况,确保系统高效和稳定。Linux系统中,提供sysinfo
以获取相关运行状态信息。
#include <sys/sysinfo.h>
int sysinfo(struct sysinfo *info);
形参,info
,返回状态信息
返回,成功反0,失败返回-1,错误码存于error
中
struct sysinfo
数据结构struct sysinfo {
long uptime; /* 系统运行时间 */
unsigned long loads[3];/* 平均负载,与top命令查看的类似 */
/* 1, 5, and 15 minute load averages */
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)]; /* 保留空间 */
};
可以通过万能“男人”man
查看其用法。
acuity@ubuntu:~$ man sysinfo
SYSINFO(2) Linux Programmer's Manual SYSINFO(2)
NAME
sysinfo - returns information on overall system statistics
SYNOPSIS
#include <sys/sysinfo.h>
int sysinfo(struct sysinfo *info);
DESCRIPTION
Until Linux 2.3.16, sysinfo() used to return information in the following structure:
struct sysinfo {
long uptime; /* Seconds since boot */
unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
unsigned long totalram; /* Total usable main memory size */
unsigned long freeram; /* Available memory size */
unsigned long sharedram; /* Amount of shared memory */
unsigned long bufferram; /* Memory used by buffers */
unsigned long totalswap; /* Total swap space size */
unsigned long freeswap; /* swap space still available */
unsigned short procs; /* Number of current processes */
char _f[22]; /* Pads structure to 64 bytes */
};
and the sizes were given in bytes.
Since Linux 2.3.23 (i386), 2.3.48 (all architectures) the structure is:
struct sysinfo {
long uptime; /* Seconds since boot */
unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
unsigned long totalram; /* Total usable main memory size */
unsigned long freeram; /* Available memory size */
unsigned long sharedram; /* Amount of shared memory */
unsigned long bufferram; /* Memory used by buffers */
unsigned long totalswap; /* Total swap space size */
unsigned long freeswap; /* swap space still available */
unsigned short procs; /* Number of current processes */
unsigned long totalhigh; /* Total high memory size */
unsigned long freehigh; /* Available high memory size */
unsigned int mem_unit; /* Memory unit size in bytes */
char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding to 64 bytes */
};
and the sizes are given as multiples of mem_unit bytes.
#include <stdio.h>
#include <sys/sysinfo.h>
int main(int argc,char **argv)
{
struct sysinfo sys_info;
if (sysinfo(&sys_info))
{
perror("call sysinfo failed\n");
return -1;
}
printf("running time: %lu S\n", sys_info.uptime);
printf("1 minute load average : %lu\n", sys_info.loads[0]);
printf("5 minute load average : %lu\n", sys_info.loads[1]);
printf("15 minute load average: %lu\n", sys_info.loads[2]);
printf("total memory: %lu MB\n", sys_info.totalram/1024/1024);
printf("free memory: %lu MB\n", sys_info.freeram/1024/1024);
printf("swap total memory: %lu MB\n", sys_info.totalswap/1024/1024);
printf("swap free memory: %lu MB\n", sys_info.freeswap/1024/1024);
printf("running progress: %u\n", sys_info.procs);
return 0;
}
acuity@ubuntu:/home$ gcc sysinfo.c -o sysinfo
acuity@ubuntu:/home$ ./sysinfo
running time: 13856 S
1 minute load average : 4032
5 minute load average : 992
15 minute load average: 256
total memory: 7965 MB
free memory: 6163 MB
swap total memory: 0 MB
swap free memory: 0 MB
running progress: 452