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;
}