LAB 4 IS DONE.

This commit is contained in:
winPond
2019-07-06 10:39:40 +08:00
parent 21dd0e1a51
commit e78b336ba8
113 changed files with 11622 additions and 0 deletions

38
lab/user/forktree.c Normal file
View File

@@ -0,0 +1,38 @@
// 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("");
}