Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



Linux Programmer's Reference    (ISBN: 0072123559)


 

 List Price: $19.99
 Our Price: $19.99
 Used Price: $3.87

 Release Date: 10 December, 1999
 Manufacturer: McGraw-Hill Osborne Media (Paperback)
 Sales Rank: 165,538

 Author: Richard Petersen









More Info

 Sysinfo 2003-06-03 13:47:01
 
Category: source:c:linux
Description: 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.
Platform: linux
Author: detour
Viewed: 6921
Rating: 4.3/5 (55 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
sysinfo.c
/* 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;
}
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?