-
大小: 4KB文件類型: .cpp金幣: 1下載: 0 次發(fā)布日期: 2021-01-07
- 語(yǔ)言: C/C++
- 標(biāo)簽: linux??進(jìn)程樹(shù)??
資源簡(jiǎn)介
在Linux環(huán)境下,用C++語(yǔ)言編寫(xiě)一個(gè)程序,以樹(shù)狀結(jié)構(gòu)(即體現(xiàn)父子關(guān)系)輸出系統(tǒng)當(dāng)前所有進(jìn)程。 通過(guò)/proc目錄獲得各進(jìn)程的父進(jìn)程ppid,獲得進(jìn)程樹(shù)的父親表示,再將進(jìn)程樹(shù)的父親表示轉(zhuǎn)換成左孩子與右孩子,最后中序遍歷二叉樹(shù),輸出進(jìn)程信息即可。
代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
using?namespace?std;
struct?pid_info{???//store?processes‘?information
int?pid;???//pid
int?ppid; //parent?pid
char?process_name[1024]; //process?name
pid_info*?child; //child?node
pid_info*?next_sibling; //brother?node
pid_info():child(NULL)next_sibling(NULL)pid(0)ppid(0){}??//initialize
pid_info(pid_info&?pi){???//copy?constructor
pid?=?pi.pid;
ppid=pi.ppid;
strcpy(process_namepi.process_name);
child?=?pi.child;
next_sibling?=?pi.next_sibling;
}
pid_info&operator?=?(const?pid_info&?pi){???//operator?=?overload
if(this?==?&pi)
return?*this;
pid?=?pi.pid;
ppid=pi.ppid;
strcpy(process_namepi.process_name);
child?=?pi.child;
ne
評(píng)論
共有 條評(píng)論