• sysinfo系统调用


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

    #include <sys/sysinfo.h>
    
    int sysinfo(struct sysinfo *info);

    描述:

    在Linux 2.3.16之前,sysinfo()用于返回以下结构中的信息:

    struct sysinfo {
            long uptime;             /* 启动到现在经过的时间 */
            unsigned long loads[3];  /* I/O 在1, 5, 和 15 分钟内加载的平均时间 */
            unsigned long totalram;  /* 总的可用的内存大小 */
            unsigned long freeram;   /* 还未被使用的内存大小  */
            unsigned long sharedram; /* 共享的存储器的大小 */
            unsigned long bufferram; /* 缓冲区大小 */
            unsigned long totalswap; /* 交换区大小 */
            unsigned long freeswap;  /* 还可用的交换区大小 */
            unsigned short procs;    /* 当前进程数目 */
            char _f[22];             /* 64字节的补丁结构*/
    };

    大小以字节为单位。

    自从Linux 2.3.23(i386),2.3.48(所有架构)后的结构是:

    struct sysinfo {
            long uptime;             /* 启动到现在经过的时间 */
            unsigned long loads[3];  /* I/O 在1, 5, 和 15 分钟内执行的平均时间 */
            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)]; /* 填充到64个字节 */
    };
     

    并且尺寸以mem_unit字节的倍数给出。

    sysinfo()提供了获取整个系统统计信息的简单方法。 这比读取/ dev / kmem更方便。

    返回值:

    成功时返回零。 出错时,返回-1,并适当地设置errno。

    出错值:

    EFAULT 指向struct sysinfo的指针是无效的

    版本:

    Linux内核自0.98.pl6开始有sysinfo()系统调用。

    遵守:

    这个函数是Linux专用的,不应该用在可移植的程序中。

    查看:

    proc(5)

     

    代码事例:

    #include <stdio.h>
    #include <sys/sysinfo.h>
    
    int main(void)
    {
            struct sysinfo info;
    
            sysinfo(&info);
    
            printf("uptime = %ld
    ",info.uptime);
            printf("loads = %ld %ld %ld
    ",info.loads[0],info.loads[1],info.loads[2]);
            printf("totalram = %lx
    ",info.totalram*info.mem_unit);
            printf("freeram = %lx
    ",info.freeram*info.mem_unit);
            printf("sharedram = %lx
    ",info.sharedram*info.mem_unit);
            printf("bufferram = %lx
    ",info.bufferram*info.mem_unit);
            printf("totalswap = %lx
    ",info.totalswap*info.mem_unit);
            printf("freeswap = %lx
    ",info.freeswap*info.mem_unit);
            printf("procs = %d
    ",info.procs);
            printf("totalhigh = %lx
    ",info.totalhigh*info.mem_unit);
            printf("freehigh = %lx
    ",info.freehigh*info.mem_unit);
            printf("mem_unit = %d
    ",info.mem_unit);
    
            return 0;
    }

    输出结果:

    uptime = 231884
    loads = 47136 39040 21696
    totalram = 7dd70000
    freeram = 888a000
    sharedram = 34dd000
    bufferram = 4075000
    totalswap = cfdff000
    freeswap = a8f20000
    procs = 664
    totalhigh = 483e2000
    freehigh = 1ea0000
    mem_unit = 4096
     
     
  • 相关阅读:
    虚拟机中对centOS7.4配置静态ip
    mybatis使用中出现的错误!
    http中get和post方法区别
    java中堆和栈的区别
    struts2工作流程
    springmvc工作流程
    JDBC访问数据库流程
    并行程序设计模式-Master-Worker模式-Guarded Suspension模式-不变模式-生产者-消费者模式的理解
    Future模式个人理解
    分布式系统一致性问题和Raft一致性算法
  • 原文地址:https://www.cnblogs.com/libra13179/p/14309738.html
Copyright © 2020-2023  润新知