【Linux C】C 语言和 linux cmd 混用
    
#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);
		
		
	}
	
}
 
