《操作系统》的实验代码。

32 lines
672 B

  1. #include <stdio.h>
  2. #include <ulib.h>
  3. int
  4. main(void) {
  5. int pid, ret;
  6. cprintf("I am the parent. Forking the child...\n");
  7. pid = fork();
  8. if (pid== 0) {
  9. cprintf("I am the child. spinning ...\n");
  10. while (1);
  11. }else if (pid<0) {
  12. panic("fork child error\n");
  13. }
  14. cprintf("I am the parent. Running the child...\n");
  15. yield();
  16. yield();
  17. yield();
  18. cprintf("I am the parent. Killing the child...\n");
  19. assert((ret = kill(pid)) == 0);
  20. cprintf("kill returns %d\n", ret);
  21. assert((ret = waitpid(pid, NULL)) == 0);
  22. cprintf("wait returns %d\n", ret);
  23. cprintf("spin may pass.\n");
  24. return 0;
  25. }