获取指定pid使用内存情况

获取指定pid使用内存情况

int get_task_mem(int pid)
{
    
    
	long mem = 0;
	FILE * p_file = NULL;
	char cmd[512]={
    
    0},fpath[128]={
    
    0},buf[128];

	if (pid==0)
		return 0;

  	sprintf(fpath,"/proc/%d/statm",pid);
	if (access(fpath,R_OK))
		return 0;

	sprintf(cmd,"cat %s |awk '{print $1}'",fpath);
    p_file = popen(cmd, "r");
    if (!p_file) {
    
    
        return 0;
    }

    if (fgets(buf,128 , p_file)!=NULL){
    
    
		mem = atol(buf);
	}
    pclose(p_file);

	return mem;

}

猜你喜欢

转载自blog.csdn.net/G_Super_Mouse/article/details/109732497