Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quantify RAM, CPU use of a process in C under Linux

How to find out, how much RAM and CPU "eats" certain process in Linux? And how to find out all runned processes (including daemons and system ones)? =)

UPD: using C language

like image 367
shybovycha Avatar asked Feb 26 '23 17:02

shybovycha


1 Answers

Use top or ps.

For example, ps aux will list all processes along with their owner, state, memory used, etc.

EDIT: To do that with C under Linux, you need to read the process files in the proc filesystem. For instance, /proc/1/status contains information about your init process (which always has PID 1):

char buf[512];
unsigned long vmsize;
const char *token = "VmSize:";
FILE *status = fopen("/proc/1/status", "r");
if (status != NULL) {
    while (fgets(buf, sizeof(buf), status)) {
        if (strncmp(buf, token, strlen(token)) == 0) {
            sscanf(buf, "%*s %lu", &vmsize);
            printf("The INIT process' VM size is %lu kilobytes.\n", vmsize);
            break;
        }
    }
    fclose(status);
}
like image 52
Frédéric Hamidi Avatar answered Mar 08 '23 17:03

Frédéric Hamidi