《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
556 B

12 years ago
12 years ago
  1. #include <ulib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define DEPTH 4
  5. void forktree(const char *cur);
  6. void
  7. forkchild(const char *cur, char branch) {
  8. char nxt[DEPTH + 1];
  9. if (strlen(cur) >= DEPTH)
  10. return;
  11. snprintf(nxt, DEPTH + 1, "%s%c", cur, branch);
  12. if (fork() == 0) {
  13. forktree(nxt);
  14. yield();
  15. exit(0);
  16. }
  17. }
  18. void
  19. forktree(const char *cur) {
  20. cprintf("%04x: I am '%s'\n", getpid(), cur);
  21. forkchild(cur, '0');
  22. forkchild(cur, '1');
  23. }
  24. int
  25. main(void) {
  26. forktree("");
  27. return 0;
  28. }