在分析luci时,看到 usr/lib/luci/sys.lua 里调用 nixio.sysinfo()。这是一个c调用lua的用例。在nixio的代码process.c里导出了给lua调用的接口。在其中看到nixio.sysinfo()的实现是调用linux的sysinfo()系统调用。

       #include <sys/sysinfo.h>

       int sysinfo(struct sysinfo *info)

成功返回0, 失败返回-1,同时errno被赋值。 struct sysinfo 的结构是:

           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 */
};

系统负载平均值,其基数是1 << SI_LOAD_SHIFT, SI_LOAD_SHIFT是个偏移值。 函数使用很简单,见下例:


#include <stdio.h>
#include <sys/sysinfo.h> int main(void)
{
struct sysinfo info; if (sysinfo(&info) < 0) {
perror("sysinfo failed");
return -1;
} printf("uptime %ld\n", info.uptime );
printf("loads %.2f, %.2f, %.2f\n",
(double)info.loads[0] / (1 << SI_LOAD_SHIFT),
(double)info.loads[1] / (1 << SI_LOAD_SHIFT),
(double)info.loads[2] / (1 << SI_LOAD_SHIFT));
printf("totalram %ld\n", info.totalram );
printf("freeram %ld\n", info.freeram );
printf("sharedram %ld\n", info.sharedram);
printf("bufferram %ld\n", info.bufferram);
printf("totalswap %ld\n", info.totalswap);
printf("freeswap %ld\n", info.freeswap );
printf("procs %d\n", info.procs );
printf("totalhigh %ld\n", info.totalhigh);
printf("freehigh %ld\n", info.freehigh );
printf("mem_unit %d\n", info.mem_unit ); return 0;
}

sysinfo 系统调用的更多相关文章

  1. Arm Linux系统调用流程详细解析

    Linux系统通过向内核发出系统调用(system call)实现了用户态进程和硬件设备之间的大部分接口. 系统调用是操作系统提供的服务,用户程序通过各种系统调用,来引用内核提供的各种服务,系统调用的 ...

  2. linux 内核修炼之道——系统调用

    1.问:什么是系统调用? 用户应用程序访问并使用内核所提供的各种服务的途径即是系统调用,也称系统调用接口层. 2.问:为什么需要系统调用? ① 系统调用作为内核和应用程序之间的中间层,扮演了一个桥梁角 ...

  3. [Linux]Linux系统调用列表

    本文列出了大部分常见的Linux系统调用,并附有简要中文说明. 以下是Linux系统调用的一个列表,包含了大部分常用系统调用和由系统调用派生出的的函数.这可能是你在互联网上所能看到的唯一一篇中文注释的 ...

  4. Linux系统调用

    在前面,我们接触到了很多函数能够实现系统相关的功能,比如解析命令行参数.控制进程以及映射内存等等.实际上,这些函数能够分为两大类: 库函数--这些函数就像普通函数一样,参数放置在寄存器或者栈里,运行时 ...

  5. 常用的Linux系统调用命令

    常用的Linux系统调用命令   下面一些函数已经过时,被新的更好的函数所代替了(gcc在链接这些函数时会发出警告),但因为兼容的原因还保留着,这些函数将在前面标上“*”号以示区别.   一.进程控制 ...

  6. Linux系统调用(转载)

    目录: 1. Linux系统调用原理 2. 系统调用的实现 3. Linux系统调用分类及列表 4.系统调用.用户编程接口(API).系统命令和内核函数的关系 5. Linux系统调用实例 6. Li ...

  7. Linux系统调用列表

    转自Linux系统调用列表 一.进程控制: fork 创建一个新进程 clone 按指定条件创建子进程 execve 运行可执行文件 exit 中止进程 _exit 立即中止当前进程 getdtabl ...

  8. 系统调用服务号 linux 2.6.32

    http://www.cnblogs.com/xcywt/p/4998963.html 系统定义符号集中声明在 /usr/src/kernels/linux-2.6.32/include/linux/ ...

  9. Linux常用系统调用

    转载 http://www.ibm.com/developerworks/cn/linux/kernel/syscall/part1/appendix.html#icomments 按照惯例,这个列表 ...

随机推荐

  1. [题解] codevs 1486 愚蠢的矿工

    http://codevs.cn/problem/1486/ 我们比较熟悉二叉树,题目中给出的是一棵多叉树,我们需要将这可二叉树改造成二叉树. 二叉树可以为这样的: 父亲结点左边储存儿子,右边储存兄弟 ...

  2. 数论基础之组合数&计数问题

    一.组合数:问题引入:现在有 n 个球,取其中的 k 个球,问一共有多少种方式?答案: 公式直观解释:我们考虑有顺序地取出 k 个球:第一次有 n 种选择,第二次有 n-1 种选择,...,第 k 次 ...

  3. 洛谷p1049 01背包

    dp水之旅背包 题目描述 有一个箱子容量为V(正整数,0<=V<=20000),同时有n个物品(0<n<=30,每个物品有一个体积(正整数). 要求n个物品中,任取若干个装入箱 ...

  4. MyBatis的动态sql小练习,小回顾

    关键字if+trim trim可以去除多余的关键字,是where和set的组合 trim标记是一个格式化的标记,可以完成set或者是where标记的功能,如下代码: <trim prefix=& ...

  5. 20. ROUTINES

    20. ROUTINES ROUTINES表提供有关存储例程(存储过程和存储函数)的信息. ROUTINES表不包含内置SQL函数或用户定义函数(UDF). 名为mysql.proc Name的列表示 ...

  6. linux常用过滤日志命令

    #过滤nginx访问日志awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr -k1 | head -n 10 ...

  7. AnjularJs-Select添加数据并设置默认值

    html中 <select name="book_classify" class="all_list_btn" ng-model="select ...

  8. LeetCode(52) N-Queens II

    题目 Follow up for N-Queens problem. Now, instead outputting board configurations, return the total nu ...

  9. POJ 1287 Networking (最小生成树模板题)

    Description You are assigned to design network connections between certain points in a wide area. Yo ...

  10. HDU 5469 Antonidas

    Antonidas Time Limit: 4000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID:  ...