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

📄 procitem.cpp

📁 一个linux进程管理器,具有以下功能: 管理系统的进程
💻 CPP
字号:
#include "ProcItem.h"#include <linux/param.h>#include <iostream>#include <bitset>using namespace std;		string ProcItem::getComm(){	return comm;}void ProcItem::setComm(string comm){	this->comm = comm;}int ProcItem::getPid(){	return pid;}void ProcItem::setPid(int pid){	this->pid = pid;}int ProcItem::getUid(){	return uid;}void ProcItem::setUid(int uid){	this->uid = uid;}int ProcItem::getEuid(){	return euid;}void ProcItem::setEuid(int euid){	this->euid = euid;}int ProcItem::getPpid(){	return ppid;}void ProcItem::setPpid(int ppid){	this->ppid = ppid;}string ProcItem::getProcessState(){	return this->processState;}void ProcItem::setProcessState(string processState){	this->processState = processState;}int ProcItem::getNice(){	return this->nice;}void ProcItem::setNice(int nice){	this->nice = nice;}unsigned long ProcItem::getVmSize(){	return vmSize;}void ProcItem::setVmSize(unsigned long size){	this->vmSize = size;}unsigned long ProcItem::getVmRSS(){	return vmRSS;}void ProcItem::setVmRSS(unsigned long size){	this->vmRSS = size;}unsigned long ProcItem::getUserTime(){	return this->userTime;}void ProcItem::setGetUserTime(unsigned long time){	this->userTime = time;}unsigned long ProcItem::getSystemTime(){	return this->systemTime;}void ProcItem::setSystemTime(unsigned long time){	this->systemTime = systemTime;}unsigned long ProcItem::getStartTime(){	return startTime;}void ProcItem::setStartTime(unsigned long time){	this->startTime = time;}		vector<int> ProcItem::getChildID(){	return this->childrenID;}void ProcItem::setChildID(vector<int> ids){	this->childrenID = ids;}void ProcItem::addChild(int id){	this->childrenID.push_back(id);}FileInfo ProcItem::getOpenedFile(){	return this->openedFile;}void ProcItem::addOpenedFile(FileItem file){	this->openedFile.insert(file);}void ProcItem::setOpenedFile(FileInfo files){	this->openedFile = files;}string ProcItem::getOwner(){	return this->owner;}void ProcItem::setOwner(string owner){	this->owner = owner;}string ProcItem::getEffective(){	return this->effective;}void ProcItem::setEffective(string effective){	this->effective = effective;}vector<SigItem> ProcItem::getSigItems(){	sigItems.clear();	ofstream outfile(pathName.c_str());	outfile<<pid;	outfile.close();	setSigInfo(pathName);	return sigItems;}void ProcItem::addSigItem(SigItem s){	sigItems.push_back(s);}void ProcItem::setSigItems(vector<SigItem> s){	sigItems = s;}ProcItem::ProcItem(){	this->ppid = -1;	pathName = "/proc/psinfo";}void ProcItem::setBasicInfo(char * pathName){	ifstream infile(pathName);	if(infile.fail())		return;	char ch;	stringstream sstr;	while(!infile.eof())	{		infile.get(ch);		if(!infile.eof())			sstr<<ch;	}			string str(sstr.str());	string tem;		int i = str.find("Name:");	sstr.seekg(i);	sstr>>tem;	sstr>>this->comm;		i = str.find("State:");	sstr.seekg(i);	string tmp;	sstr>>tem;	sstr>>tem;	sstr>>tmp;//	sstr>>this->processState;	this->processState = tem+tmp;		i = str.find("Pid:");	sstr.seekg(i);	sstr>>tem;	sstr>>this->pid;		i = str.find("PPid");	sstr.seekg(i);	sstr>>tem;	sstr>>this->ppid;		i = str.find("Uid:");	sstr.seekg(i);	sstr>>tem;	sstr>>this->uid>>this->euid;		i = str.find("VmSize:");	sstr.seekg(i);	sstr>>tem;	sstr>>this->vmSize;		i = str.find("VmRSS:");	sstr.seekg(i);	sstr>>tem;	sstr>>this->vmRSS;				infile.close();		struct passwd * a = getpwuid(uid);	if(a != NULL)		owner = a->pw_name;	a = getpwuid(euid);	if(a != NULL)		effective = a->pw_name;}void ProcItem::setTimeInfo(char * pathName){	ifstream infile(pathName);	if(infile.fail())		return;	char ch;	stringstream sstr;	while(!infile.eof())	{		infile.get(ch);		if(!infile.eof())			sstr<<ch;	}	infile.close();//	cout<<sstr.str();//	exit(0);	int pid;// 进程(包括轻量级进程,即线程)号	string comm;// 应用程序或命令的名字	string task_state;//R:runnign, S:sleeping (TASK_INTERRUPTIBLE), D:disk sleep (TASK_UNINTERRUPTIBLE), T: stopped, T:tracing stop,Z:zombie, X:dead	int ppid;//父进程ID	int pgid;// 线程组号	int sid;// 该任务所在的会话组ID	int tty_nr;// 该任务的tty终端的设备号,INT(34817/256)=主设备号,(34817-主设备号)=次设备号	int tty_pgrp;// 终端的进程组号,当前运行在该任务所在终端的前台任务(包括shell 应用程序)的PID。	unsigned long taskflags;// 进程标志位,查看该任务的特性	unsigned long min_flt;//该任务不需要从硬盘拷数据而发生的缺页(次缺页)的次数	unsigned long cmin_flt;// 累计的该任务的所有的waited-for进程曾经发生的次缺页的次数目	unsigned long  maj_flt;// 该任务需要从硬盘拷数据而发生的缺页(主缺页)的次数	unsigned long cmaj_flt;// 累计的该任务的所有的waited-for进程曾经发生的主缺页的次数目	unsigned long utime;// 该任务在用户态运行的时间,单位为jiffies	unsigned long stime;//该任务在核心态运行的时间,单位为jiffies	unsigned long cutime;// 累计的该任务的所有的waited-for进程曾经在用户态运行的时间,单位为jiffies	unsigned long cstime;// 累计的该任务的所有的waited-for进程曾经在核心态运行的时间,单位为jiffies	int priority;// 任务的动态优先级	int nice;// 任务的静态优先级	int num_threads;// 该任务所在的线程组里线程的个数	unsigned long it_real_value;// 由于计时间隔导致的下一个 SIGALRM 发送进程的时延,以 jiffy 为单位.	unsigned long start_time;// 该任务启动的时间,单位为jiffies	sstr>>pid>>comm>>task_state>>ppid>>pgid>>sid>>tty_nr>>tty_pgrp>>taskflags>>min_flt>>cmin_flt>>maj_flt>>cmaj_flt>>utime>>stime>>cutime>>cstime>>priority>>nice>>num_threads>>it_real_value>>start_time;/*>>vsize>>rss>>rlim>>start_code>>end_code>>start_stack>>kstkesp>>kstkeip>>pendingsig>>block_sig>>sigign>>sigcatch>>wchan>>nswap>>cnswap>>exit_signal>>task_cpu>>task_rt_priority>>task_policy>>anom;*/		this->nice = nice;		this->userTime = utime*(1000/CLOCKS_PER_SEC);	this->systemTime = stime*(1000/CLOCKS_PER_SEC);//	cout<<pid<<" "<<userTime<<" "<<systemTime<<endl;	ifstream uptimefile("/proc/uptime");	stringstream sstrtime;	while(!uptimefile.eof())	{		uptimefile.get(ch);		if(!uptimefile.eof())			sstrtime<<ch;	}	uptimefile.close();	double uptime;	sstrtime>>uptime;		this->startTime = uptime-start_time/CLOCKS_PER_SEC;	}void ProcItem::setOpenedFileInfo(char * pathName){	DIR * dir;	struct dirent * dirent;		char fileName[1000];		char buf[256];	int fd;	if((dir = opendir(pathName)) == NULL)	{		printf("Open %s error",pathName);	}	else	{				while((dirent = readdir(dir)) != NULL)		{			if(strcmp(dirent->d_name,".") != 0 && strcmp(dirent->d_name,"..") != 0)			{						memset(fileName,0,sizeof(fileName));						strcat(fileName,dirent->d_name);				stringstream sstr(fileName);				sstr>>fd;				memset(fileName,0,sizeof(fileName));				memset(buf,0,sizeof(buf));				sprintf(fileName,"/proc/%i/fd/%i",this->pid,fd);				readlink(fileName,buf, sizeof(buf));				this->addOpenedFile(FileItem(fd,buf));			}						}						closedir(dir);	}	}void ProcItem::setSigInfo(string pathName){	char ch;	stringstream sstr;	int index;	int num;	string handler;		ifstream infile(pathName.c_str());	if(infile.fail())		return;		while(!infile.eof())	{		infile.get(ch);		if(!infile.eof())			sstr<<ch;	}	infile.close();		unsigned long l;	sstr>>l;	bitset<64> b(l);	string strBlock = b.to_string();	for(index = 1;index < SigItem::nameSize;index++)	{		sstr>>num;		sstr>>handler;		SigItem s;		s.setSigName(SigItem::name[index]);		if(strBlock.at(strBlock.length() -index -1) == '1')			s.setIsBlocked(true);		else 			s.setIsBlocked(false);		if(handler == "0")		{			s.setHand(0);					}		else if(handler == "1")		{			s.setHand(1);		}		else		{			s.setHand(2);			s.setAddress(handler);		}		addSigItem(s);	}	}

⌨️ 快捷键说明

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