量化RAM,CPU使用过程中在Linux下C [英] Quantify RAM, CPU use of a process in C under Linux

查看:159
本文介绍了量化RAM,CPU使用过程中在Linux下C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何找出多少内存和CPU吃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:用C语言

推荐答案

使用顶部 PS

例如,的ps aux 将列出所有的进程有其所有者,状态,内存使用等沿。

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

编辑::要做到这一点与Linux下C,你需要在的 PROC 文件系统。例如,的/ proc / 1 /状态包含您的初始化进程(总是 PID 1

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

这篇关于量化RAM,CPU使用过程中在Linux下C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆