forked from jtylermalone/CSCI340-Project1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project1.c
329 lines (262 loc) · 10.9 KB
/
Project1.c
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
////////////////////////// HERE /////////////////////////////////
struct Process {
char pid[10];
char ppid[10];
char name[40];
char vsize[16];
int visited;
char *parentProcess;
int numChildren;
int childProcess[];
};
void buildTree(struct Process processes[], int numProcesses) {
/*
for (int i = 0; i < numProcesses; i++){
//printf("%s\n", processes[i].pid);
int childProcs = 0;
for (int j = 0; j < numProcesses; j++) {
//printf("pid: %s ppid: %s\n", processes[i].pid, processes[j].ppid);
if (strcmp(processes[i].pid, processes[j].ppid) == 0) {
//printf("pid %s child pid: %s\n", processes[i].pid, processes[j].pid);
int childPID = atoi(processes[j].pid);
//printf("CHILDPID 1: %d\n", childPID);
processes[i].childProcess[childProcs] = childPID;
//printf("CHILDPID 2: %d\n\n", processes[i].childProcess[childProcs]);
//printf("childProcs: %d\n", childProcs);
//childProcs++;
}
}
processes[i].numChildren = childProcs;
//printf("pid: %s number of children: %d\n", processes[i].pid, processes[i].numChildren);
}
*/
for (int i = 0; i < numProcesses; i++){
//printf("(%s) %s , %s kb\n", processes[i].pid, processes[i].name, processes[i].vsize);
int childProcs = 0;
//printf("pid: %s\n", processes[i].pid);
for (int j = 0; j < numProcesses; j++) {
if (atoi(processes[i].pid) == atoi(processes[j].ppid) && (atoi(processes[i].pid) != atoi(processes[j].pid)) && processes[j].visited != 1 && processes[i].visited != 1){
//int childPID = processes[j].pid;
processes[i].childProcess[childProcs] = atoi(processes[j].pid);
//printf("pid: %d ppid: %d\n", atoi(processes[i].pid), atoi(processes[i].ppid));
childProcs++;
processes[i].visited = 1;
processes[j].visited = 1;
}
}
processes[i].numChildren = childProcs;
//printf("$$$$$$$$$$$$$$$$ childProcs: %d\n", processes[i].numChildren);
}
// This print statement is for debugging purposes only
//printf("pid: %s ppid: %s name: %s vsize: %s\n\n", processes[i].pid, processes[i].ppid, processes[i].name,
//processes[i].vsize);
}
void preorderTraversal(struct Process rootProc, struct Process processes[], int numProcesses) {
if (rootProc.childProcess[0] == 0)
return;
printf("(%s) %s , %s kb\n", rootProc.pid, rootProc.name, rootProc.vsize);
rootProc.visited = 1;
for (int i = 0; i < rootProc.numChildren; i++) {
for (int j = 0; j < numProcesses; j++) {
//printf("number of children: %d\n", rootProc.numChildren);
//printf("comparing %d to %d\n", rootProc.childProcess[i], atoi(processes[j].pid));
if (rootProc.childProcess[i] == atoi(processes[j].pid) && rootProc.visited != 1) {
//printf("child %d: %d", i, rootProc.childProcess[i]);
//printf("(%s) %s , %s kb\n", rootProc.pid, rootProc.name, rootProc.vsize);
//printf(": (%s) %s , %s kb\n", processes[j].pid, processes[j].name, processes[j].vsize);
preorderTraversal(processes[j], processes, numProcesses);
}
}
}
}
struct Process findProcess(struct Process processes[], int processesSize, int pid) {
for (int i = 0; i < processesSize; i++)
if (atoi(processes[i].pid) == pid)
return processes[i];
}
////////////////////////////////////////////////////////////// /////////
void printChildren(struct Process processes[], int processesSize) {
for(int i = 0; i < processesSize; i++) {
processes[i].visited = 0;
}
for (int i = 0; i < processesSize; i++) {
int size = processes[i].numChildren;
if(processes[i].visited != 1) {
printf("parent pid: %s\n", processes[i].pid);
}
for (int j = 0; j < processesSize; j++) {
if ((strcmp(processes[i].pid, processes[j].ppid) == 0) && (processes[i].visited != 1)) {
printf(" pid: %s parent pid: %s\n", processes[j].pid, processes[j].ppid);
processes[j].visited = 1;
}
}
}
/*
int size = parentProcess.numChildren;
if (size == 0)
return;
//printf("size: %d\n", size);
if (size != 0)
for (int i = 0; i < size; i++) {
for (int j = 0; j < processesSize; j++) {
if (parentProcess.childProcess[i] == atoi(processes[j].pid)){
printf("childprocess: %d pid: %d\n ", parentProcess.childProcess[i], atoi(processes[j].pid));
//printf("********************************\n\n");
printChildren(processes[j], processes, processesSize);
}
}
}
//for (int i = 0; i < size; i++) {
//printf(" children: %d\n", parentProcess.childProcess[i]);
//for (int j = 0; j < processesSize; j++){
//printf(" child pid: %d\n", parentProcess.childProcess[i]);
//}
*/
}
int main(void)
{
// all of this business about
struct dirent *de; // Pointer for directory entry
int numberOfProcesses = 0;
// opendir() returns a pointer of DIR type.
DIR *dr = opendir("/proc/");
FILE *fp;
if (dr == NULL)
{
printf("Could not open current directory" );
return 0;
}
// in this while loop, we are only concerned with getting the current
// number of running processes.
while ((de = readdir(dr)) != NULL)
{
// de is the variable that holds each directory in /proc/ in turn,
// and de->d_name is used to obtain the directory name.
char* dirName = de->d_name;
// This if statement is ugly and long, but it works (at least I think).
//It makes sure we only interact with folders that have numeric names.
if (dirName[0] == '0' || dirName[0] == '1' || dirName[0] == '2' || dirName[0] == '3' || dirName[0] == '4' ||
dirName[0] == '6' || dirName[0] == '7' || dirName[0] == '8' || dirName[0] == '9'){
numberOfProcesses += 1;
}
}
closedir(dr);
// this list will hold all of the process structs that we instantiate.
// I don't know if it's actually useful or not.
struct Process processes[numberOfProcesses * sizeof(struct Process)];
int processCounter = 0;
dr = opendir("/proc/");
// this while loop is where we actually loop through each running process.
while ((de = readdir(dr)) != NULL) {
// definining all the variables we need below. comm1 and comm2 are used
// when the process name has a space in it (is two words long).
char* pid[10], ppid[5], vsize[20], comm[50], dummy[15], comm1[15], comm2[15];
char buff[255]; // saw this online, no idea if it's necessary or not
// de is the variable that holds each directory in /proc/ in turn,
// and de->d_name is used to obtain the directory name.
char* dirName = de->d_name;
if (dirName[0] == '0' || dirName[0] == '1' || dirName[0] == '2' || dirName[0] == '3' || dirName[0] == '4' ||
dirName[0] == '6' || dirName[0] == '7' || dirName[0] == '8' || dirName[0] == '9'){
// assuming that no stat file will have a full path
// longer than 20 characters
char fileName[20];
// this adds "/proc/<pid>/stat" to the fileName variable
snprintf(fileName, 20, "/proc/%s/stat", dirName);
// this opens filename for reading
FILE *fp = fopen(fileName, "r");
// resetting filename
memset(fileName, 0, strlen(fileName));
// I accept that this line below is ugly, but I don't know a way around it.
// It scans the stat file, which has several values separated by spaces,
// and scans each value into a variable as needed. Since vsize is the (I
// think) 23rd variable, it's necessary to scan this many variables. To
// scan in the values we aren't concerned with, we are using a dummy
// variable.
fscanf(fp, "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s", dummy, comm, dummy,
ppid, dummy, dummy, dummy, dummy, dummy, dummy, dummy, dummy, dummy, dummy, dummy, dummy, dummy,
dummy, dummy, dummy, dummy, dummy, vsize);
// this if statement executes if the process name is two words (i.e. contains
// a space). Since each comm ends with a ), if there is a space in the comm
// name then comm (at this point) won't end with ). Then, we have to combine
// both parts of comm. I hope that makes some sort of sense.
if (comm[strlen(comm)-1] != ')'){
// I'm not 100% sure if this is necessary, but here we reset all relevant
// stat variables.
memset(comm, 0, strlen(comm));
memset(dummy, 0, strlen(dummy));
// this resets the fp pointer back to the beginning of the stat file so
// that we can scan from scratch
fseek(fp, 0, SEEK_SET);
// need to scan for relevant values again
fscanf(fp, "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s", dummy, comm1, comm2, dummy, ppid, dummy, dummy, dummy, dummy, dummy, dummy,
dummy, dummy, dummy, dummy, dummy, dummy, dummy, dummy, dummy, dummy, dummy, dummy, vsize);
// combining both comm1 and comm2 into comm
strcat(comm, comm1);
strcat(comm, " ");
strcat(comm, comm2);
}
// below, we are instantiating a Process and copying the relevant
// strings into its fields.
struct Process newProcess;
strcpy(newProcess.pid, dirName);
strcpy(newProcess.ppid, ppid);
strcpy(newProcess.name, comm);
strcpy(newProcess.vsize, vsize);
// adding the new process to the processes list
processes[processCounter] = newProcess;
// processCounter is what is being assigned to each
// process struct's procNum field.
processCounter += 1;
// here we are resetting each of the variable's back
// so that they're blank. C is weird so I think
// this is necessary here...
memset(fileName, 0, strlen(fileName));
memset(dirName, 0, strlen(dirName));
memset(comm, 0, strlen(comm));
memset(ppid, 0, strlen(ppid));
memset(vsize, 0, strlen(vsize));
}
}
closedir(dr);
// set adjacency matrix to all zeros
// I think what's going on here is that edges are being added
// to the graph (not sure if they're added correctly) and I am
// (I think!) correctly changing the relevant entries in the
// adjacency matrix to 1's.
for (int i = 0; i < numberOfProcesses; i++){
// This print statement is for debugging purposes only
//printf("pid: %s ppid: %s name: %s vsize: %s\n\n", processes[i].pid, processes[i].ppid, processes[i].name,
//processes[i].vsize);
}
printf("number of processes: %d\n", numberOfProcesses);
buildTree(processes, numberOfProcesses);
//printf("number of children: %d\n", processes[0].numChildren);
//preorderTraversal(processes[0], processes, numberOfProcesses);
//for (int i = 0; i < processes[0].numChildren; i++)
//printf("child %d: %d\n", i, processes[0].childProcess[i]);
//printChildren(processes[0]);
//struct Process test = findProcess(processes, numberOfProcesses, 1364);
//printf("**** %s\n", test.pid);
for(int i = 0; i < numberOfProcesses; i++) {
processes[i].visited = 0;
}
for(int i = 0; i < numberOfProcesses; i++) {
if(processes[i].visited != 1) {
preorderTraversal(processes[i],processes,numberOfProcesses);
}
}
//printChildren(processes, numberOfProcesses);
/*
for (int i = 0; i < numberOfProcesses; i++){
//printf("i: %d\n", i);
printChildren(processes, numberOfProcesses);
}
*/
printf("Done!\n");
return 0;
}