[英]Making a program that shows CPU(%) and Memory usage(kB) using pid linux
I'm trying to create a program that gets the CPU(%) and Memory(kB) usage to show it on Ubuntu Terminal.我正在尝试创建一个程序来获取 CPU(%) 和内存(kB) 使用情况,以在 Ubuntu 终端上显示它。 I've been searching for some command that shows me it, and i got that;
我一直在寻找一些向我展示它的命令,我得到了;
ps -p <pid> -o %cpu,%mem
When I test it directly on terminal, it works just fine.当我直接在终端上测试它时,它工作得很好。 But on my program it gives me that error: error: garbage option
但是在我的程序上它给了我这个错误:错误:垃圾选项
Here's my code:这是我的代码:
#include <unistd.h>
#include<time.h>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main (int argc, char *argv[], char *envp[]) {
int pid ; /* Process ID */
char usage[150];
char kill[50];
pid = fork () ; /* Process reaplication */
sprintf(usage,"%s%d%s","ps -p ", pid, " %cpu,%mem");
sprintf(kill, "%s%d", "kill -9 ", pid);
if ( pid < 0 ) { /*if fork do not work*/
perror ("Erro: ") ;
exit (-1) ; /* Ends process with error: -1 */
} else if( pid > 0 ) { /* If i'm parent process*/
for(int i=0;i<10;i++) {
system("clear");
printf("Processing (%ds)\n", i);
int aux;
for(aux=i;aux>0;aux--) {
printf("=");
}
printf("=\n");
system(usage);
sleep(1);
}
system(kill);
} else /* senão, sou o processo filho (pid == 0) { */
if(strcmp(argv[1],"cpu")==0) { /* if argv[1] = cpu -> Execute code using intese cpu*/
for(;;){}
}
if(strcmp(argv[1],"cpu-mem")) { /* if argv[1] = cpu-mem -> Execute code using intese cpu and memory */
int moment = clock();
for (;;) {
while(clock() - moment < 5){} /* makes mem use less intense */
malloc(sizeof(1000));
}
}
}
perror ("Erro: ") ; /* if do not work */
printf ("Tchau !\n") ;
exit(0) ; /* Ends process with success (código 0) *
}
So, I'm trying to divide the program in 10 steps.所以,我试图将程序分成 10 个步骤。 Each one is gonna execute the command.
每个人都会执行命令。 There is one sec between the steps
步骤之间有一秒
How can I get this code to work?我怎样才能让这个代码工作? Is there any other commands that I can use to replace this one?
有没有其他命令可以用来替换这个命令?
Already solved it.已经解决了。 That's how I did:
我就是这样做的:
#include <unistd.h>
#include<time.h>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main (int argc, char *argv[], char *envp[]) {
int pid ; /* Process ID */
char mem_usage[50];
char cpu_usage[50];
char kill[50];
pid = fork () ; /* Process reaplication */
sprintf(cpu_usage,"%s%d%s","ps -p ", pid, " -o %cpu | grep -v %CPU");
sprintf(mem_usage, "%s%d%s", "pmap -x ", pid," | grep total | awk '{print $3}'");
sprintf(kill, "%s%d", "kill -9 ", pid);
if ( pid < 0 ) { /*if fork do not work*/
perror ("Erro: ") ;
exit (-1) ; /* Ends process with error: -1 */
}
else if( pid > 0 ) /* If i'm parent process*/
{
for(int i=0;i<10;i++)
{
system("clear");
if(i == 0 || i == 3 || i == 6 || i == 9)
{
printf("Processing.\n");
}
else if(i == 1 || i == 4 || i == 7)
{
printf("Processing..\n");
}
else if(i == 2 || i == 5 || i == 8)
{
printf("Processing...\n");
}
printf("%ds\n", i+1);
printf("================\n");
printf("CPU(%%)\n");
system(cpu_usage);
printf("MEM(kB)\n");
system(mem_usage);
sleep(1);
}
system(kill);
}
else /* senão, sou o processo filho (pid == 0) */
{
if(strcmp(argv[1],"cpu")==0) /* if argv[1] = cpu -> Execute code using intese cpu*/
{
for(;;){}
}
if(strcmp(argv[1],"cpu-mem")==0) /* if argv[1] = cpu-mem -> Execute code using intese cpu and memory */
{
int moment = clock();
for (;;) {
sleep(0.001); /* makes mem use less intense */
malloc(50* sizeof(int));
}
}
}
perror ("Erro: ") ; /* if do not work */
printf ("Tchau !\n") ;
exit(0) ; /* Ends process with success (código 0) */
}
I've used ps -p <pid> -o %cpu | grep -v %CPU
我用过
ps -p <pid> -o %cpu | grep -v %CPU
ps -p <pid> -o %cpu | grep -v %CPU
to get the CPU(%) usage. ps -p <pid> -o %cpu | grep -v %CPU
获取 CPU(%) 使用率。
To calculate the Memory(kB) usage: pmap -x <pid> " | grep total | awk '{print $3}'"
计算内存(kB)使用量:
pmap -x <pid> " | grep total | awk '{print $3}'"
In this case, I've used awk '{print $3}'
to print the third column from the Comand pmap -x <pid>
and grep total
to print only the line I needed.在这种情况下,我使用
awk '{print $3}'
从命令pmap -x <pid>
打印第三列,并使用grep total
仅打印我需要的行。
There are two ways to run this code:有两种方法可以运行此代码:
This was a good exercise to learn how CPU and Memory work.这是一个很好的练习,可以了解 CPU 和内存的工作原理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.