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) {
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<depth; i++) puts(" ");
if (depth > 0) puts(" \\_ ");
for (int i=1; i<depth; i++) printf(" ");
if (depth > 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;