LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-17-2010, 06:30 AM   #1
chamila1986
Member
 
Registered: Oct 2009
Posts: 31

Rep: Reputation: 15
Get system current status to C program from "htop"


Actually I suppose to use this "htop" command to get the system utilities like current processor speed ,running programs ,memory usage(ram and swap) to my program. I planned to get it using popen(). I was success popen("top | grep Mem", "r") with top command. But are there a way to take such information using "htop". Or are there any idea of taking current CPU,MEM,Swp usage as well as Tasks and running tasks using htop or other way to C program...
 
Old 01-17-2010, 09:00 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Programs like top, htop, ps get their information from files and subdirectories in /proc. You can read that information in your own program. IMHO in general it doesn't make to much sense writing program's in C just to run other program's like "ps", or "top" combined with 'grep' etc..

See man 5 proc for information about how to read the things in /proc.

Example code of a program that lists all PID's and the amount of memory the processes use:
Code:
/* Show memory usage for each PID */

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <fnmatch.h>
#include <sys/types.h>
#include <string.h>

#define BUFSZ 1024
#define MEM_FIELD_NAME "VmSize:"

void processdir(const struct dirent *piddir)
{
    char path[BUFSZ];
    char line[BUFSZ];
    char *memstr;
    FILE *pidmemfile;
    int  offset = strlen(MEM_FIELD_NAME);
    
    /* Construct full path of the file containt memory-info if this PID */
    snprintf(path, BUFSZ, "/proc/%s/status", piddir->d_name);
    
    /* Open the file */
    pidmemfile = fopen(path, "rt");

    /* Read line-by-line until we found the line we want */
    while (fgets(line, BUFSZ, pidmemfile) != NULL) {
        memstr = strstr(line, MEM_FIELD_NAME);
        if (memstr != NULL) {  /* Found our line */
            memstr += offset;
            while (*memstr == ' ' || *memstr == '\t') {
                if (*memstr == '\0') {
                    fprintf(stderr, "unexpected error in %s.\n", path);
                    exit(1);
                }
                ++memstr;
            }
            printf("PID %s: %s", piddir->d_name, memstr);
            break;
        }
    }
    fclose(pidmemfile);
}


int main() 
{
     DIR *procdir;
     struct dirent *procentry;

     procdir = opendir("/proc");
     if (procdir == NULL) {
         perror("Could not open directory /proc");
         return 1;
     }

     for(;;) {
         procentry = readdir(procdir);
         if (procentry == NULL) {
             break;
         }
         /* if the name of an entry in /proc has only digits, then
          * it is a subdirectory containg info about a process,
          * while the name itself is the PID of the process.
          */        
         if (!fnmatch("[1-9]*", procentry->d_name, 0)) {
             processdir(procentry);
         }
     }
     return 0;
}

Last edited by Hko; 01-17-2010 at 09:05 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to compile and run a C# program that contains "using System.Windows.Forms" landonmkelsey Programming 3 06-08-2007 05:45 PM
"status=0x50 DriveReady Seek Complete" system crash cyungle Linux - Hardware 1 09-20-2006 05:27 PM
Proposed solution for "status" (aka "problem solved") indicator demerson3 LQ Suggestions & Feedback 12 04-08-2006 02:15 PM
C -communicating with system("ftp"); using "program|ftp>>myfifo" probably hansschmucker Programming 1 03-23-2005 01:39 PM
Redhat Linux W/Gnome X System - Doesn't "Save Current Setup" drdroid Linux - General 2 03-26-2004 02:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration