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

59 lines
878 B

  1. #include <ulib.h>
  2. #include <stdio.h>
  3. void
  4. do_yield(void) {
  5. yield();
  6. yield();
  7. yield();
  8. yield();
  9. yield();
  10. yield();
  11. }
  12. int parent, pid1, pid2;
  13. void
  14. loop(void) {
  15. cprintf("child 1.\n");
  16. while (1);
  17. }
  18. void
  19. work(void) {
  20. cprintf("child 2.\n");
  21. do_yield();
  22. if (kill(parent) == 0) {
  23. cprintf("kill parent ok.\n");
  24. do_yield();
  25. if (kill(pid1) == 0) {
  26. cprintf("kill child1 ok.\n");
  27. exit(0);
  28. }
  29. }
  30. exit(-1);
  31. }
  32. int
  33. main(void) {
  34. parent = getpid();
  35. if ((pid1 = fork()) == 0) {
  36. loop();
  37. }
  38. assert(pid1 > 0);
  39. if ((pid2 = fork()) == 0) {
  40. work();
  41. }
  42. if (pid2 > 0) {
  43. cprintf("wait child 1.\n");
  44. waitpid(pid1, NULL);
  45. panic("waitpid %d returns\n", pid1);
  46. }
  47. else {
  48. kill(pid1);
  49. }
  50. panic("FAIL: T.T\n");
  51. }