简体   繁体   English

C-sysinfo()返回错误的值i686

[英]C - sysinfo() returning bad values i686

I'm trying to write a simple program to alert me when ram is getting fulled, but I've some problems with sysinfo(), the sample program is in C, I've grabbed it from a site with examples the code seems to be okay, any ideas of why this can be happening ? 我正在尝试编写一个简单的程序来在ram变满时向我发出警报,但是sysinfo()遇到了一些问题,示例程序在C中,我从一个带有示例代码的网站上抓到了它。可以,为什么会发生这种情况? sorry about my english it's not my native language... 抱歉我的英语不是我的母语...

code is below: 代码如下:

/* sysinfo.c by detour@metalshell.com
 *
 * Display the uptime, load averages, total ram, free ram,
 * shared ram, buffered ram, total swap, free swap, and
 * number of processes running on a linux machine.
 *
 * http://www.metalshell.com/
 *
 */

#include <sys/sysinfo.h>
#include <stdio.h>

int main() {
  int days, hours, mins;
  struct sysinfo sys_info;

  if(sysinfo(&sys_info) != 0)
    perror("sysinfo");

  // Uptime
  days = sys_info.uptime / 86400;
  hours = (sys_info.uptime / 3600) - (days * 24);
  mins = (sys_info.uptime / 60) - (days * 1440) - (hours * 60);

  printf("Uptime: %ddays, %dhours, %dminutes, %ldseconds\n",
                      days, hours, mins, sys_info.uptime % 60);

  // Load Averages for 1,5 and 15 minutes
  printf("Load Avgs: 1min(%ld) 5min(%ld) 15min(%ld)\n",
          sys_info.loads[0], sys_info.loads[1], sys_info.loads[2]);

  // Total and free ram.
  printf("Total Ram: %ldk\tFree: %ldk\n", sys_info.totalram / 1024,
                                        sys_info.freeram / 1024);

  // Shared and buffered ram.
  printf("Shared Ram: %ldk\n", sys_info.sharedram / 1024);
  printf("Buffered Ram: %ldk\n", sys_info.bufferram / 1024);

  // Swap space
  printf("Total Swap: %ldk\tFree: %ldk\n", sys_info.totalswap / 1024,
                                           sys_info.freeswap / 1024);

  // Number of processes currently running.
  printf("Number of processes: %d\n", sys_info.procs);

  return 0;
}

After rereading what you are trying to use sysinfo for and reading the manual page for sysinfo I have an idea what might be bothering you about its results. 重读你想怎么使用后sysinfo并阅读手册页sysinfo我有一个想法可能困扰你关于它的结果。 If this is not your problem then you will need to post more (like actual output of the above program and comments about what is wrong with it and why you think that is wrong). 如果这不是您的问题,那么您将需要发布更多内容(例如上述程序的实际输出以及有关该程序有问题以及为什么您认为这是错误的评论)。

Old versions of Linux had a different version of sysinfo that was very similar to the current version, but not compatible with it. Linux的旧版本具有与当前版本非常相似的sysinfo版本,但与之不兼容。 There were a few fields added to its structure as well as a slight change to the meaning of the memory fields. 它的结构中增加了一些字段,并且存储字段的含义略有变化。 These fields now need to be interpreted along with the mem_unit field. 这些字段现在需要与mem_unit字段一起解释。 This is because it is possible for some machines to have more memory than can be expressed within one long integer. 这是因为某些机器可能有比一个长整数表示的更多的内存。

This type of situation became somewhat common on 32 bit x86 where more than 2^32 (4gb) of RAM were installed in some machines. 这种类型的情况在32位x86上变得很常见,在某些机器上安装了2 ^ 32(4gb)以上的RAM。 I suspect that this may be your problem since your program does not mention mem_unit at all. 我怀疑这可能是您的问题,因为您的程序根本没有提到mem_unit

I think that if you try: 我认为,如果您尝试:

 printf("Total Ram: %lluk\tFree: %lluk\n",
                sys_info.totalram *(unsigned long long)sys_info.mem_unit / 1024,
                sys_info.freeram *(unsigned long long)sys_info.mem_unit/ 1024);

Then that line may start to produce output that makes more sense for you. 然后,该行可能开始产生对您更有意义的输出。 A similar change on the other lines that deal with RAM should also make them make more sense. 在处理RAM的其他行上进行的类似更改也应该使它们更有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM