简体   繁体   English

linux cpu用法

[英]linux cpu usage

I am working on unix. 我正在研究unix。 I want to knwo the current cpu usage of a process. 我想知道一个进程当前的cpu使用情况。 I understood that ps give the average of cpu used till the process is up - it is not the current usage. 我明白ps给出了cpu的平均值,直到进程结束 - 这不是当前的用法。

Is there a way to print only the cpu from the top command without 10 more parameters and headers? 有没有办法只从top命令打印cpu而没有10多个参数和标题? I know how to do it with awk - this is not the way i want to do it. 我知道如何使用awk - 这不是我想要的方式。

top  -p 20705 -bc -n 1  | tail -n 2 | awk '{ print $9}' | head -n 1

If there is another simple way to do it, not reading /proc/stat... 如果还有另一种简单的方法,那就不要读/ proc / stat ...

If there is a simple way doing it from c++, it is also ok. 如果从c ++有一个简单的方法,它也可以。

Most likely, you will need to read /proc/stat , However, here is an interesting article with C code that may help you out. 最有可能的是,你需要阅读/proc/stat ,但是, 这是一篇有趣的C代码文章可能会帮助你。 To understand and use the output from the program you should do man 5 proc . 要理解和使用程序的输出,你应该做man 5 proc And here is the source code . 这是源代码

The bottom line is that you will need to read from /proc/stat to do what you want. 最重要的是,您需要从/proc/stat读取以执行您想要的操作。

to see cpu usage of a proccess whose pid is 24556 查看pid为24556的进程的cpu使用情况

ps -p 24556 -o \%cpu=

to see mem usage of a process named syslogd 查看名为syslogd的进程的mem用法

ps -C syslogd -o \%mem=

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM