⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 proccollection.cpp

📁 一个linux进程管理器,具有以下功能: 管理系统的进程
💻 CPP
字号:
#include "ProcCollection.h"#include <ctype.h>#include <iostream>using namespace std;void ProcCollection::updateInfo(){	this->procInfo.clear();	initialize();}void ProcCollection::initialize(){			if(isInitProc)		initProc();	if(isInitSys)		initSys();}void ProcCollection::initProc(){	char path_stat[100];	bool isDigit = true;	DIR * dir;	struct dirent * dirent;		int index ;	char path_name[20];		if((dir = opendir("/proc")) == NULL)	{		printf("Open %s error");	}	else	{				while((dirent = readdir(dir)) != NULL)		{			if(strcmp(dirent->d_name,".") != 0 && strcmp(dirent->d_name,"..") != 0)			{	isDigit = true;				index = 0 ;			while(dirent->d_name[index] != 0)			{				if(isdigit(dirent->d_name[index]) == 0)				{					isDigit = false;					break;				}				index++;			}					if(!isDigit)				continue;			memset(path_stat,0,sizeof(path_stat));			strcat(path_stat,"/proc/");			strcat(path_stat,dirent->d_name);						ProcItem item;										memset(path_name,0,sizeof(path_name));			strcat(path_name,path_stat);			strcat(path_name,"/status");							item.setBasicInfo( path_name);							memset(path_name,0,sizeof(path_name));			strcat(path_name,path_stat);			strcat(path_name,"/stat");			item.setTimeInfo(path_name);										memset(path_name,0,sizeof(path_name));			strcat(path_name,path_stat);			strcat(path_name,"/fd");							item.setOpenedFileInfo( path_name);							this->procInfo.push_back(item);			}						}						closedir(dir);	}			setProcessRelation();}void ProcCollection::initSys(){	float total_old,idle_old,total_new,idle_new;	FILE *fp;	if((fp=fopen("/proc/uptime","r"))==NULL){		printf("can't open file");		exit(-1);	}	fscanf(fp,"%f",&total_old);	fscanf(fp,"%f",&idle_old);	fclose(fp);	if(begin)	{		cpuusage=1-(idle_old-f1)/(total_old-f2);	}	f1 = idle_old;	f2 = total_old;	begin = true;			char line[200],buf[50];	float totalm,freem;	int j,flag;	int counter=0;	int fd_info;	fd_info=open("/proc/meminfo",O_RDONLY);	read(fd_info,line,200);	for(j=0;j<=50;j++){		if(line[j]=='\n') counter++;			if(counter<3) ;		else break;	}	sscanf(line,"MemTotal:       %f KB",&totalm);//get "Total Memory" to totalm	for(j=0;j<=50;j++){		if(line[j]=='\n'){	flag=j+1;}	}	for(j=0;j<50;j++,flag++){		buf[j]=line[flag];	}	sscanf(buf,"MemFree:        %f kB",&freem);	close(fd_info);	totalMem = totalm/1024;	sizedMem = (totalm - freem)/1024;}		void ProcCollection::setProcessRelation(){	for(int i = 0;i <this->procInfo.size();i++)	{		int ppid = this->procInfo[i].getPpid();		if(ppid != -1)			this->setProcessRelation(ppid,this->procInfo[i].getPid());	}}int ProcCollection::setProcessRelation(int parentPid,int childPid){	ProcItem *item = this->getSpecProcess(parentPid);	if(item == NULL)		return -1;	item->addChild(childPid);	return 1;}ProcItem* ProcCollection::getSpecProcess(int pid){	for(int i = 0;i<this->procInfo.size();i++)	{		if(this->procInfo[i].getPid() == pid)			return &this->procInfo[i];	}	return NULL;}vector<ProcItem> ProcCollection::getProcInfo(){	return this->procInfo;}void ProcCollection::setProcInfo(vector<ProcItem> procInfo){	this->procInfo = procInfo;}float ProcCollection::getTotalMem(){	return totalMem;}float ProcCollection::getSizedMem(){	return sizedMem;}float ProcCollection::getCpuusage(){	return cpuusage;}void ProcCollection::setIsInitProc(bool b){	isInitProc = b;}void ProcCollection::setIsInitSys(bool b){	isInitSys = b;}ProcCollection::ProcCollection(){	isInitProc = true;	isInitSys = true;	begin = false;	cpuusage = -1;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -