jos/user/forktree.c
Anish Athalye da1f8392b1 Lab 4
2018-10-08 21:32:14 -07:00

39 lines
529 B
C

// Fork a binary tree of processes and display their structure.
#include <inc/lib.h>
#define DEPTH 3
void forktree(const char *cur);
void
forkchild(const char *cur, char branch)
{
char nxt[DEPTH+1];
if (strlen(cur) >= DEPTH)
return;
snprintf(nxt, DEPTH+1, "%s%c", cur, branch);
if (fork() == 0) {
forktree(nxt);
exit();
}
}
void
forktree(const char *cur)
{
cprintf("%04x: I am '%s'\n", sys_getenvid(), cur);
forkchild(cur, '0');
forkchild(cur, '1');
}
void
umain(int argc, char **argv)
{
forktree("");
}