当前位置:网站首页>使用popen来执行一个命令并获得返回结果
使用popen来执行一个命令并获得返回结果
2022-07-27 06:10:00 【hawanglc】
在linux系统中,我们要想执行一个命令,可以使用system()或者exec()或者popen()函数来完成。有时候,我们要将取得命令的返回结果,并处理这些结果,这个时候,就需要用popen()了。
本文就是演示了popen()的用法。需要注意的是popen()需要和pclose()同时使用,就像打开文件和关闭文件同时使用一样。
程序的思路:在用malloc()申请一块内存之后,就显示一下这个进程的内存使用量,free()一块内存的时候,也显示一下内存的使用量。
/*
cc -g malloc2.c -o malloc2
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX 20
char *mallocMemory(int i)
{
char *p = malloc(1024 * 1024 * sizeof(char));
printf("%d\tchar *p = malloc(1024*1024*sizeof(char));\t addr is %p\n", i, p);
return p;
}
void getMemoryInfo(char *msg, int size, char *cmd)
{
FILE *fp = NULL;
fp = popen(cmd, "r");
if (fp == NULL)
{
return;
}
fread(msg, sizeof(char), size, fp);
pclose(fp);
}
int main(int argc, char **argv)
{
int i = 0;
char *cmd = "ps -aux | grep malloc2 | grep -v grep | grep -v tail && date";
int system_return = 0;
char msg[1000] = {0};
char *p_malloc[MAX] = {0};
getMemoryInfo(msg, sizeof(msg), cmd);
printf("%s", msg);
for (; i < MAX; i++)
{
p_malloc[i] = mallocMemory(i);
memset(msg, 0x00, sizeof(msg));
getMemoryInfo(msg, sizeof(msg), cmd);
printf("%s", msg);
sleep(10);
}
for (i = 0; i < MAX; i++)
{
printf("free(p_malloc[%d]);\n", i);
free(p_malloc[i]);
memset(msg, 0x00, sizeof(msg));
getMemoryInfo(msg, sizeof(msg), cmd);
printf("%s", msg);
sleep(10);
}
sleep(20);
memset(msg, 0x00, sizeof(msg));
getMemoryInfo(msg, sizeof(msg), cmd);
printf("%s", msg);
return 0;
}程序的输出的结果略。通过输出的结果,可以看出,每一次malloc一块内存,程序的虚拟内存就会变大,每free一块内存,程序的虚拟内存就会变小。而物理内存的变化却滞后于虚拟内存的变化,它是代表了载入内存的可执行的代码的量。
边栏推荐
- Digital image processing -- Chapter 3 gray scale transformation and spatial filtering
- Watermelon book learning Chapter 5 --- neural network
- How to learn C language? This article gives you the complete answer
- Watermelon book learning notes - Chapter 4 decision tree
- C# Winfrom 常用功能整合-2
- No.0 training platform course-2. SSRF Foundation
- Py2exe QT interface style becomes Win98 solution
- Codeforces Round #787 (Div. 3)(7/7)
- Usage of string class
- 二叉树--天然的查找语义(1)基础篇
猜你喜欢

多线程【初阶-上篇】

Drools (5): drools basic syntax (3)

Automatically generate UML sequence diagram according to text (draw.io format)

Which C4d cloud rendering platform to cooperate with?

Convert Excel to csv/csv UTF-8

No.0 training platform course-2. SSRF Foundation

C# Winfrom 常用功能整合-2

Digital image processing - Chapter 6 color image processing

35. Search insert position

A Competitive Swarm Optimizer for Large Scale Optimization
随机推荐
如何取得对象的DDL信息
35. Search insert position
ShowDoc漏洞学习——CNVD-2020-26585(任意文件上传)
Golang encapsulates the packages involved in MySQL and the differences between sqlx and Gorm
Pytorch notes: td3
py2exe qt界面风格变成了win98解决方案
【golang学习笔记2.1】 golang中的数组中的排序和查找
(posted) comparison of Eureka, consumer and Nacos 1
Student status management system based on SSM
Calledprocesserror during pre commit install
用typescript实现排序-递增
Overall dichotomy?
C# 常用功能整合-3
C# Winfrom 常用功能整合-2
Please ask the big guys a question. The pgsqlcdc task can't monitor changes after running for a period of time. Just restart it. What should I do
Usage of string class
Quartus: an error is reported when adding a.V file to someone else's project
Gbase 8C core technology
The possibility of metauniverse from the perspective of technical principles: how Omniverse "built" Mars
Drools(5):Drools基础语法(3)