From 5902bc0bb5383151b1b0694d05a4751b871d021d Mon Sep 17 00:00:00 2001 From: dan Date: Tue, 14 Jan 2025 16:31:49 -0500 Subject: [PATCH] Fixed bugs in tree linking --- src/server.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/server.c b/src/server.c index 1677dc9..305a803 100644 --- a/src/server.c +++ b/src/server.c @@ -96,7 +96,7 @@ void linkFamily(Process *head) { while (orphans) { orphans = 0; 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; if (current == NULL) break; orphans = 1; @@ -124,6 +124,7 @@ int resetVisits(Process *head) { while (current != NULL) { current->visited = 0; current = current->next; + processCount++; } return processCount; @@ -142,13 +143,16 @@ void printFamilyTree(Process *head) { } current->visited = 1; - printf("%10d ", current->pid); + visited++; + + printf("%10d %10d %1d ", current->pid, current->ppid, current->iskernel); // `ps f` style tree - for (int i=1; i 0) puts(" \\_ "); + for (int i=1; i 0) printf(" \\_ "); printf("%s\n", current->label); - if (current->child != NULL) { +nextProcess: + if (current->child != NULL && !current->child->visited) { // process children first depth++; current = current->child; @@ -159,6 +163,7 @@ void printFamilyTree(Process *head) { // return to parent when tree is exhausted depth--; current = current->parent; + goto nextProcess; // parent was already visited, so find next process from parent } else { // no parent - scan for unvisited process while (current != NULL && current->visited == 1) current = current->next; @@ -252,6 +257,7 @@ char *readProcesses(char *procdir) { free(cur->next); cur->next = NULL; + printFamilyTree(head); linkFamily(head); printFamilyTree(head); @@ -355,7 +361,7 @@ int main(int argc, char *argv[]) { if (argc == 1) { char *buf = readProcesses("/proc"); if (buf == NULL) return 4; - printf(buf); + //printf(buf); free(buf); return 0;