简体   繁体   English

如何使用C获得Linux顶级输出

[英]How can you get a Linux top like output using C

I am trying to get the CPU usage and details of all the running processes on embedded Linux while a C test is running. 我正在尝试在C测试运行时获取CPU使用率以及嵌入式Linux上所有正在运行的进程的详细信息。 Is there a top equivalent in C that I could use ? 我可以使用C中的top等效项吗?

我不确定top哪个方面会让您感到困惑,但请看一下top源代码

I think you need to parse the /proc file system. 我认为您需要解析/ proc文件系统。 The kernel has a special file system which is normally mounted at /proc with a unique directory for each process (eg. /proc/30 belongs to the process with PID=30). 内核有一个特殊的文件系统,该文件系统通常安装在/ proc上,每个进程都有一个唯一的目录(例如/ proc / 30属于PID = 30的进程)。 The files in these directories are all text files and contains lots of info on the running processes. 这些目录中的文件都是文本文件,并且包含有关正在运行的进程的大量信息。

Instead of top , you should probably use ps with the right arguments and you can parse the output. 可以使用ps和正确的参数来代替top ,然后可以解析输出。

You'll have to call that with a system call or popen from your C code. 您必须使用system调用来调用它,或者从C代码中popen You cannot get this information strictly inside your C code (at least I don't think you can). 您不能严格在C代码内部获得此信息(至少我认为不能)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用c程序在linux中获取磁盘使用情况? - How can get disk usage in linux using c program? 如何在C中使用print进行这样的输出 - how to make output like this, using print in C 如何在Linux中使用c或c ++而不使用system()或exec()函数的情况下调用pwd或ls -l之类的系统函数? - how we can call a system function like pwd or ls -l without using system() or exec() function using c or c++ in linux? 如何使用C获取Linux中的CPU个数? - How to get the number of CPUs in Linux using C? 如何检查您何时仅获得 integer(如 2)而不是 integer 后跟 char(如 2c) - How to check when you scanf that you get only integer (like 2) and not integer followed by char (like 2c) 如何使用C清除Linux终端中的先前输出? - how to clear my previous output in Linux terminal using C? 如何使用C(linux)中的管道将GCC的输出保存在文件中? - how to save the output of GCC in a file using pipes in C (linux)? 如何使用在Linux上运行的C语言程序获取驱动器的确切总空间? - How can I get exact total space of a drive using C language program running on Linux? 获取管道中的命令输出,C代表Linux - get command output in pipe, C for Linux 您如何在C语言中解释这个类似于函数的宏`slice`的输出? - How do you explain the output from this function-like macro `slice` in C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM