该部分的实验思路来自:read://https_rivers-shall.github.io/?url=https%3A%2F%2Frivers-shall.github.io%2F2019%2F03%2F05%2FNJU-OS-M1-pstree%2F
第一部分已经完成的工作
第一部分我们从/proc中读取了进程id和其父进程ppid,并将其信息存储在了数组中。
这一部分要为打印进程树创建相应的数据结构
要打印进程树我们需要知道每个进程的父进程,要知道每个进程的孩子进程。仅仅使用第一部分我们使用的简单数组的结构也可以实现这部分功能,但是我们可以使用一种更为高效的数据结构。

相应的结构体定义
typedef struct proc{
struct proc *next;
struct proc *parent;
struct child *children;
int pid;
char pName[50];
}PROC;
typedef struct child{
const PROC* proc;
struct child *next;
}CHILD;
构建相应数据结构的思路
我们使用单链表来存储进程的信息,在加入一个进程时,由于我们知道其父进程的pid,同时可以将其父进程也加入进程链表。在后面再扫描到其父进程时,我们再完善其父进程的信息(如父进程的名字)
PROC* add_proc(const char* name,int pid,int ppid)
{
PROC *this=find_proc(pid);
/*
if(this==NULL)
{
printf("dont find process %d %s in the list\n",pid,name);
}else
{
printf(" the process %d %s in the list\n",pid,name);
}
*/
if(this!=NULL)//该进程已被加入队列
{
rename_proc(this,name)