-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessTree.c
More file actions
90 lines (84 loc) · 2.13 KB
/
ProcessTree.c
File metadata and controls
90 lines (84 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <sys/types.h>
/* Struct to store tokenized data*/
struct insertNodes
{
char *storecmd;
char *storepid;
char *storeppid;
struct insertNodes *processtree;
};
struct insertNodes * insert(struct insertNodes **root, char *cmd, char *ppid,
char *pid)
{
while(*processroot)
{
printf("storepid %s, ppid %s, pid %s ,compare %d , storecmd %s, cmd %s \n",(*processroot)->storepid,ppid,pid,strcmp((*processroot)->storepid,ppid),(*root)->storecmd,cmd);
if(strcmp((*processroot)->storepid,ppid)==0)
{
printf("equals\n");
*processroot=malloc(1000);
(*processroot)->processtree=0;
(*processroot)->storecmd=strdup(cmd);
return(*processroot);
}
if(strcmp((*processroot)->storepid,pid)!=0)
{
processroot=&((*processroot)->processtree);
}
}
printf("new\n");
*processroot=malloc(1000);
(*processroot)->processstree=0;
(*processroot)->storecmd=strdup(cmd);
return(*processroot);
}
int i=0;
void print(struct insertNodes *processroot)
{
if(processroot->processtree)
{
printf("\t %d. %s, %s, %s\n",i,root->storecmd,root->storepid,processroot->storeppid);
i=i+1;
print(processroot->processtree);
}
i=i+1;
}
void print1(struct insertNodes *processroot)
{
if(strcmp(root->storepid,processroot->processtree->storeppid)==0)
{
printf("Equals : %s, %s, %s\n",processroot->storecmd,processroot->storepid,processroot->storeppid);
print1(processroot->processtree);
}
if(strcmp(processroot->storepid,processroot->processtree->storeppid)<0)
{
printf("Not Equals : %s, %s, %s\n",processroot->storecmd,processroot->storepid,processroot->storeppid);
}
return;
print1(root->stree);
}
int main()
{
FILE *fp;
char line[256];
fp=popen("ps -axl","r");/* Print process and output to file*/
char cmd[256];
char pid[256];
char ppid[256];
struct insertNodes *processtree;
while(fgets(line,sizeof line,fp))
{
sscanf(line,"%*s %*s %*s %*s %*s %*s %s %s %*s %*s %s",pid,ppid,cmd);
if(strcmp(pid,"PID")!=0)
{
insert(&processtree,cmd,ppid,pid);
}
}
/*Print tree to screen*/
printf("processtree: %s\n", (tree)->stree->ncmd);
print(processtree);
return(0);
}