0
点赞
收藏
分享

微信扫一扫

【Linux C】C 语言和 linux cmd 混用

墨香子儿 2022-02-23 阅读 39
linuxc语言
// test script.c 
// generate workload

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define LEN 1000


void my_system(const char *cmd)
{
	char result[10240] = {0};
	char buf[1024] = {0};
    	FILE *fp = NULL;

	if( (fp = popen(cmd, "r")) == NULL ) 
	{
	    	printf("popen error!\n");
		return;
	}

	while(fgets(buf, sizeof(buf), fp)) 
	{
		strcat(result, buf);
	}
	pclose(fp);
	printf("result: %s\n", result);
}


void main(int argc, char* argv[])
{
	unsigned long int PID = getpid();
	char cmd[50] = {0};
	sprintf(cmd, "cat /proc/%ld/status | grep VmSize", PID);
	
	for(int i = 0; i < LEN; i++)
	{
		printf("i=%d, malloc addr=%p\n", i, (char*)malloc(1024));
		my_system(cmd);
		// sleep(1);
		// printf("\n\n");
	}
	
}

在这里插入图片描述

举报

相关推荐

0 条评论