Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a little program in C that I want to use to output some system stats to my HD44780 16x2 character display. The system I'll be working with is a Debian ARM system and, although irrelevant, the display is on the GPIO header.(The system is a Raspberry Pi).

As an initial (somewhat unambitious) attempt, I'd like to start with something simple like RAM and CPU usage (I'm new to C).

I understand that if I make external command calls I need to fork() and execve() (or some equiv that will let me return the results), what I would like to know is how I go about getting the information I want in a nice clean format that I can use.

Surely I will not have to call (for e.g);

free -h

And then use awk or similar to chop out the piece I want? There must be a cleaner way?

The question should be seen as more of a generic, what is best practice for getting info about the system in C (the RAM/CPU usage are just an initial example).

share|improve this question
up vote 7 down vote accepted

Information about the state of the system is beyond the scope of the C language specification, so anything you do will be specific to the system where your program will run.

Since you've identified a target environment (Linux) and a program that provides some of the information you're after (free(1)), your best bet would be to acquire and examine the source for that program, learn how it works and use what you've learned in your own program. (This would certainly be much better than spawning copies of the utility and picking the output apart.)

share|improve this answer
Straight to the point, thanks. I'll do exactly this. – Hamid Sep 5 '12 at 19:13

Give that you're on a modern Linux system, the information you're looking for is found in the pseudo-filesystem rooted at /proc/. Every process has a /proc/[pid] subdirectory, and other subdirectories provide a global status. E.g. /proc/meminfo is the source for free(1).

share|improve this answer
/proc/meminfo prints just as much junk as calling free would, so I'd still have to string manipulate the result, would I not? – Hamid Sep 6 '12 at 9:43
@Hamid: It would be unlikely that an existing API would include precisely the information you need, nothing less and nothing more. And since cutting is easy, a good API errs towards providing more. – MSalters Sep 6 '12 at 9:46

There are various library routines that provide system information, try taking a look at the sysinfo() call.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.