mirror of
https://github.com/SmallPond/MIT6.828_OS.git
synced 2026-04-28 12:50:48 +08:00
LAB 4 IS DONE.
This commit is contained in:
38
lab/user/forktree.c
Normal file
38
lab/user/forktree.c
Normal 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("");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user