?? procitem.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 + -