LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 06-12-2003, 10:38 PM   #1
cs87668
LQ Newbie
 
Registered: Jun 2003
Posts: 6

Rep: Reputation: 0
How do I get system information by C/C++


Dear all,

I want to get some informtion of system by C/C++ Language, such as:


1. CPU (These are percentages of total CPU time)

us: user time
sy: system time
id: idle time

2. Procs
r: The number of processes waiting for run time.
b: The number of processes in uninterruptable sleep.
w: The number of processes swapped out but otherwise runnable.

I have traced the source code of "vmstat", and it is too complex to understand.

Can anyone help me and give me some hits,

thank you~

Last edited by cs87668; 06-12-2003 at 10:40 PM.
 
Old 06-12-2003, 11:06 PM   #2
yixudong
LQ Newbie
 
Registered: Aug 2002
Location: Somewhere in East World
Distribution: RH7.x
Posts: 11

Rep: Reputation: 0
first of all ,C language does not provide any funtion like this.

Maybe you can find or write a library to do the tasks upon.

I think you should find them in the kernel struct although it is not the best way.And the entries you want is served more to the kernel ,not to the user,so it is hard to find a function.
 
Old 06-13-2003, 02:44 AM   #3
aizkorri
Member
 
Registered: Feb 2002
Location: Basque Country
Distribution: Fedora 14, Ubuntu 14.04
Posts: 434
Blog Entries: 1

Rep: Reputation: 55
I think its easyer to call system functions like 'ps -x' from C.
 
Old 06-13-2003, 04:42 AM   #4
turnip
Member
 
Registered: Jul 2002
Posts: 143

Rep: Reputation: 15
something like

Code:
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>


int main(){

char buffer[5000];

        system("top > output");

        ifstream output ("output");
        output.read (buffer,5000);
        cout << buffer << endl;

        ofstream outputfile ("output");
        outputfile << "";
        outputfile.close();

        system("rm output");
}
should give you what you are looking for.

you can also add another system("vmstat >> output");

for a little more information.

hope this helped.

Last edited by turnip; 06-13-2003 at 04:52 AM.
 
Old 06-14-2003, 12:35 AM   #5
AMDPwred
Member
 
Registered: Dec 2001
Location: Richmond, VA
Posts: 391

Rep: Reputation: 30
I believe some of this info is scattered around in the /proc/ directory as well.
 
Old 06-14-2003, 02:04 PM   #6
benguin
LQ Newbie
 
Registered: Oct 2002
Location: Montreal
Distribution: Ubuntu Dapper and Edgy
Posts: 23

Rep: Reputation: 15
/proc directory indeed.
use a c++ istream to open and read /proc/loadavg. That'd give you the load values.
you can use turnips way to get the process info.

:-)
 
Old 06-14-2003, 10:28 PM   #7
cs87668
LQ Newbie
 
Registered: Jun 2003
Posts: 6

Original Poster
Rep: Reputation: 0
thanks for everyboady's reply,

I trace the source code of procps package (including vmstat.c, sysinfo.c...) finally
But I still don't understand that the algorithm of code and the equation which calculate the sysytem information value.

Now, I copy some part of codes from the package, and I can get the information which I need now.

Last edited by cs87668; 06-14-2003 at 10:36 PM.
 
Old 06-15-2003, 05:51 AM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
You may want to have a look at RedHat reference on the proc filesystem.
 
  


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
System Information RedHatCore Linux - Newbie 3 08-16-2005 02:19 PM
System Information cranky Linux - Newbie 8 10-28-2004 05:26 AM
Collecting System Information srivatsan Linux - Networking 2 06-30-2004 08:28 AM
System Information Command? Pete Dogg Linux - General 16 05-13-2003 01:58 PM
system information killi Linux - Hardware 2 05-10-2003 10:35 AM

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

All times are GMT -5. The time now is 03:19 PM.

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