Fixed bugs in tree linking

This commit is contained in:
dan 2025-01-14 16:31:49 -05:00
parent 810586053d
commit 5902bc0bb5

View File

@ -96,7 +96,7 @@ void linkFamily(Process *head) {
while (orphans) { while (orphans) {
orphans = 0; orphans = 0;
Process *current = head; Process *current = head;
// while on an actual process node, and it doesn't have a parent link, and should have a parent link // while on an actual process node, and it has a parent link or doesn't need a parent link
while (current != NULL && (current->parent != NULL || current->ppid <= 0)) current = current->next; while (current != NULL && (current->parent != NULL || current->ppid <= 0)) current = current->next;
if (current == NULL) break; if (current == NULL) break;
orphans = 1; orphans = 1;
@ -124,6 +124,7 @@ int resetVisits(Process *head) {
while (current != NULL) { while (current != NULL) {
current->visited = 0; current->visited = 0;
current = current->next; current = current->next;
processCount++;
} }
return processCount; return processCount;
@ -142,13 +143,16 @@ void printFamilyTree(Process *head) {
} }
current->visited = 1; current->visited = 1;
printf("%10d ", current->pid); visited++;
printf("%10d %10d %1d ", current->pid, current->ppid, current->iskernel);
// `ps f` style tree // `ps f` style tree
for (int i=1; i<depth; i++) puts(" "); for (int i=1; i<depth; i++) printf(" ");
if (depth > 0) puts(" \\_ "); if (depth > 0) printf(" \\_ ");
printf("%s\n", current->label); printf("%s\n", current->label);
if (current->child != NULL) { nextProcess:
if (current->child != NULL && !current->child->visited) {
// process children first // process children first
depth++; depth++;
current = current->child; current = current->child;
@ -159,6 +163,7 @@ void printFamilyTree(Process *head) {
// return to parent when tree is exhausted // return to parent when tree is exhausted
depth--; depth--;
current = current->parent; current = current->parent;
goto nextProcess; // parent was already visited, so find next process from parent
} else { } else {
// no parent - scan for unvisited process // no parent - scan for unvisited process
while (current != NULL && current->visited == 1) current = current->next; while (current != NULL && current->visited == 1) current = current->next;
@ -252,6 +257,7 @@ char *readProcesses(char *procdir) {
free(cur->next); free(cur->next);
cur->next = NULL; cur->next = NULL;
printFamilyTree(head);
linkFamily(head); linkFamily(head);
printFamilyTree(head); printFamilyTree(head);
@ -355,7 +361,7 @@ int main(int argc, char *argv[]) {
if (argc == 1) { if (argc == 1) {
char *buf = readProcesses("/proc"); char *buf = readProcesses("/proc");
if (buf == NULL) return 4; if (buf == NULL) return 4;
printf(buf); //printf(buf);
free(buf); free(buf);
return 0; return 0;