Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3426306
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:41:58+00:00 2026-05-18T06:41:58+00:00

I’m trying to write a simple program to alert me when ram is getting

  • 0

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 ? 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;
}
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:41:58+00:00Added an answer on May 18, 2026 at 6:41 am

    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. 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. 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. 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. I suspect that this may be your problem since your program does not mention mem_unit at all.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.