当前位置:网站首页>使用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一块内存,程序的虚拟内存就会变小。而物理内存的变化却滞后于虚拟内存的变化,它是代表了载入内存的可执行的代码的量。
边栏推荐
- pre-commit install 时 CalledProcessError
- Drools(5):Drools基础语法(3)
- Interpretation of deepsort source code (VII)
- 请问有人使用oracle xstream 时出现个别capture延迟很大的吗,该如何解决延迟问题呢
- String类的用法
- Visual horizontal topic bug1:filenotfounderror: could not find module 'mvcameracontrol dll‘ (or one of it
- How to implement Devops with automation tools | including low code and Devops application practice
- Netease Yunxin appeared at the giac global Internet architecture conference to decrypt the practice of the new generation of audio and video architecture in the meta universe scene
- Summary of APP launch in vivo application market
- 想sink 到 redis-hash 里面 把 对象的属性和值都写进去 ,大佬们有Demo 吗?
猜你喜欢

Netease Yunxin appeared at the giac global Internet architecture conference to decrypt the practice of the new generation of audio and video architecture in the meta universe scene

ESP8266(ESP-12F) 第三方库使用 -- SparkFun_APDS9960 (手势识别)

No.0 training platform course-2. SSRF Foundation

DDD Domain Driven Design Notes

Firefox browser, when accessing Tencent cloud server, failed to establish a secure connection.

C4D动画如何提交云渲染农场快速渲染?

(转帖)eureka、consul、nacos的对比2

Dajiang livox customized format custommsg format conversion pointcloud2

C# 常用功能整合-2

Jmeter: interface automation test - BeanShell compares database data and return data
随机推荐
Codeforces Round #787 (Div. 3)(7/7)
Digital image processing Chapter 1 Introduction
A Competitive Swarm Optimizer for Large Scale Optimization
Jmeter: interface automation test - BeanShell compares database data and return data
sql-labs SQL注入平台-第1关Less-1 GET - Error based - Single quotes - String(基于错误的GET单引号字符型注入)
C# Winfrom 常用功能整合-2
把Excel转换成CSV/CSV UTF-8
Gbase 8C - SQL reference 6 SQL syntax (11)
A Competitive Swarm Optimizer for Large Scale Optimization
2022-07-25 顾宇佳 学习笔记
指令集董事长潘爱民出席2022 ECUG Con,为中国技术力量发声
Drools (5): drools advanced syntax
Codeforces Round #804 (Div. 2)(5/5)
Relevant principles of MySQL index optimization
MySQL quickly compares database table data
LogCat工具
MySQL2
ShowDoc漏洞学习——CNVD-2020-26585(任意文件上传)
?实验 7 基于 Mysql 的 PHP 管理系统实现
2021 interview questions for php+go of Zhongda factory (2)