本文介绍了SYSINFO系统调用不返回正确freeram值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近写了使用SYSINFO系统调用来显示系统统计信息下面的C code,有什么好笑我是SYSINFO结构freeram变量不返回的可用RAM的数量,而不是它返回当前的内存使用。我不得不用一种变通方法,以显示由减去TOTALRAM的freeram正确的值。我曾尝试谷歌上搜索这个特定的变量,但无济于事。任何了解这个怪异的行为将是非常有益的。

  / *
 * C程序打印象系统正常运行的系统统计,
 *总的RAM空间,免费的RAM空间,进程计数,页面大小
 * /#包括LT&; SYS / sysinfo.h> // SYSINFO
#包括LT&;&stdio.h中GT;
#包括LT&;&unistd.h中GT; //的sysconf
#包括syscalls.h//只包含一个包装函数 - 错误诠释的main()
{
    结构SYSINFO信息;    如果(SYSINFO(安培;!信息)= 0)
        错误(SYSINFO:错误读取系统统计);    的printf(正常运行时间:%ld的:%ld个:%ld个\\ N,info.uptime / 3600,info.uptime%3600/60,info.uptime 60%);
    的printf(总RAM:%ld的MB \\ n,info.totalram / 1024/1024);
    的printf(自由RAM:%ld的MB \\ n,(info.totalram-info.freeram)/ 1024/1024);
    的printf(处理数数:%d \\ n,info.procs);
    的printf(页面大小:%ld个字节\\ n的sysconf(_SC_PAGESIZE));    返回0;
}


解决方案

删除

 的#includesyscalls.h

可能是,你从什么地方和编辑借了code。双引号用于导入非官方的头文件。该自定义头文件是不是真的需要。

这是没有必要的。您code将会运行得很好。

在我的电脑, $免费-m 的freeram值与程序info.freeram匹配。显然,freeram是不是你认为它的显示。

了解更多关于

MemFree是自由存储兼; MemFree +缓冲区+缓存是可用内存(你想要的)。所以,你只是了解长期freeram错误。

I recently wrote the following C code using sysinfo systemcall to display system statistics, what amused me was that the freeram variable of sysinfo structure doesn't return the amount of free RAM instead it is returning the current RAM usage. I had to use a workaround to show the correct value by subtracting freeram from totalram. I have tried googling about this specific variable but to no avail. Any insight into this weird behavior would be really helpful.

/*
 * C program to print the system statistics like system uptime, 
 * total RAM space, free RAM space, process count, page size
 */

#include <sys/sysinfo.h>    // sysinfo
#include <stdio.h>
#include <unistd.h>     // sysconf
#include "syscalls.h"       // just contains a wrapper function - error

int main()
{
    struct sysinfo info;

    if (sysinfo(&info) != 0)
        error("sysinfo: error reading system statistics");

    printf("Uptime: %ld:%ld:%ld\n", info.uptime/3600, info.uptime%3600/60, info.uptime%60);
    printf("Total RAM: %ld MB\n", info.totalram/1024/1024);
    printf("Free RAM: %ld MB\n", (info.totalram-info.freeram)/1024/1024);
    printf("Process count: %d\n", info.procs);
    printf("Page size: %ld bytes\n", sysconf(_SC_PAGESIZE));

    return 0;
}
解决方案

Remove

#include "syscalls.h"

May be, you borrowed the code from somewhere and edited. Double quotes are used to import unofficial header files. That custom header file is not really required.

It's not needed. You code will run fine.

On my PC, freeram value of $free -m matches with info.freeram of the program. Apparently, freeram is not what you think it's showing.

Read more about the http://www.redhat.com/advice/tips/meminfo.html

MemFree is the free memory & MemFree + Buffers + Cached is available memory (which you want). So, you are just understanding the term freeram wrongly.

这篇关于SYSINFO系统调用不返回正确freeram值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 23:19