- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试创建一个程序来获取 CPU(%) 和内存(kB) 使用情况以在 Ubuntu 终端上显示它。我一直在寻找可以显示它的命令,我找到了;
ps -p <pid> -o %cpu,%mem
当我直接在终端上测试它时,它工作得很好。但是在我的程序中它给了我这个错误: error: garbage option
这是我的代码:
#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) *
}
因此,我尝试将程序分为 10 个步骤。每个人都会执行命令。步骤之间间隔一秒
我怎样才能使这段代码起作用?我可以使用任何其他命令来替换这个命令吗?
最佳答案
已经解决了。我就是这样做的:
#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) */
}
我用过 ps -p <pid> -o %cpu | grep -v %CPU
获取 CPU(%) 使用率。
计算内存(kB)使用量:pmap -x <pid> " | grep total | awk '{print $3}'"
在这种情况下,我使用了 awk '{print $3}'
从 Comand pmap -x <pid>
打印第三列和 grep total
只打印我需要的行。
有两种方式运行这段代码:
这是了解 CPU 和内存如何工作的很好的练习。
关于c - 使用 pid linux 制作一个显示 CPU(%) 和内存使用率(kB) 的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52634766/
我将文件读入字符串,更改第一行,然后将此字符串写入新文件。我通过以下代码(稍微缩短了一点)来做到这一点: while(jspIterator.hasNext()){
adb shell dumsys meminfo返回的内存是kB还是KB? 哪里: kB = 1000 bytes KB = 1024 bytes 最佳答案 它是 KB(1024 字节)或 ki
我能够解析 xml 文件,并且我想下载 xml 给出的 url 的文件。我有以下代码: try{ /* Create a URL we want to load some xm
这个问题在这里已经有了答案: Android, Compressing an image (2 个答案) 关闭 10 个月前。 我正在 android 中开发一个应用程序,它将捕获照片并存储在 sq
我将文件保存在我的 MySQL 数据库中的 LONGBLOB 列上,当我在我的 IDE 中执行选择时,我注意到一些 base64 文件内容有消息 206.2 kB (204.8 kB loaded)放
使用 Indy 的 TIdTCPServer 组件,一个包被分两部分接收,但客户端发送了一个 64 KB 的包。 如何在服务器 OnExecute 事件中接收完整的包? 现在我放了一个原型(proto
我正在编写一个正则表达式,它可以捕获一个值及其后面的任何 mb、kb、gb、字节正则表达式是: (?\p{N}+)(?:\s*)(?[mb|kb|gb|b|bytes]) 但是当给定输入“40
我刚刚创建了 range(1,100000) 的 python 列表. 使用 SparkContext 完成以下步骤: a = sc.parallelize([i for i in range(1,
我的要求是将相机捕获的图像上传到服务器,但它应该小于 500 KB。如果大于 500 KB,则需要减小到小于 500 KB (但稍微接近) 为此,我使用以下代码 - @Override pub
我有以两种不同方式加载和保存图像的代码-第一种使用openCV,第二种使用PIL。 import cv2 from PIL import Image img = cv2.imread("/home/m
我有一个 android 视频播放器,它显示 SD 卡上的所有视频名称和文件大小,但大小以字节显示,我无法将它转换为 KB、MB、GB 等。我尝试除以 int值增加 1024 但它不起作用。它打印出错
任何人都可以向我解释一下摘要报告中如何测量吞吐量、Kb/秒和平均字节数吗? 我得到了以下登录操作的总结报告。 Label : Login Action(sampler) Sample# : 1 ave
我需要将上传图片的大小调整为最大 100 kB。可能吗? 例如:尺寸为 1200x600 的 image1.jpg 有 280kB,我需要将其调整为 <100kB。 最佳答案 ImageMagick
我需要将上传图片的大小调整为最大 100 kB。可能吗? 例如:尺寸为 1200x600 的 image1.jpg 有 280kB,我需要将其调整为 <100kB。 最佳答案 ImageMagick
我有例如: Document doc = Jsoup.connect("http://example.com/").get(); String title = doc.title(); Documen
我正忙于Android通话录音机,当我调用电话时录音机显示它正在录音,在我挂断电话后,它保存文件,但保存的文件是0 KB 有没有人遇到同样的问题,请帮我解决一下。 这是我的录制代码 recorder
我正在以 KB 为单位将文件存储在数据库中。我尝试将文件信息返回的文件长度转换为 KB,如下所示。 FileInfo FileVol = new FileInfo(DownloadPath); int
在我的应用程序中,显示照片中的所有视频。选择视频后,将使用 avplayer 播放该视频。但是当我尝试获取所选视频文件的大小(kb)时,它显示错误。当我尝试复制视频文件时出现同样的错误。 我已获取这些
我正在尝试在 firebase 存储中上传图像,我希望图像不应大于 50 kb 我正在尝试获取位图的大小,以便我可以知道位图的大小是否超过 50 kb这图片不会显示在画廊中 我已经尝试了许多人建议的代
在我的 android 应用程序中,它收集了一些数据和照片扔手机。 之后它将所有这些东西插入到一个对象中,然后将这个对象发送到服务器。 在将此对象发送到服务器之前,我想向用户显示数据的大小以及将此数据
我是一名优秀的程序员,十分优秀!