《操作系统》的实验代码。
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.

39 line
667 B

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