rwakeup0.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * This file is part of Jehanne.
  3. *
  4. * Copyright (C) 2015 Giacomo Tesio <giacomo@tesio.it>
  5. *
  6. * Jehanne is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, version 2 of the License.
  9. *
  10. * Jehanne is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <u.h>
  19. #include <lib9.h>
  20. /* verify that rwakeup returns 0 on Rendez that has already been awaken */
  21. Rendez r;
  22. QLock l;
  23. int ready;
  24. int verbose = 0;
  25. int killerProc; /* pid, will kill the other processes if starved */
  26. int
  27. handletimeout(void *v, char *s)
  28. {
  29. /* just not exit, please */
  30. if(strcmp(s, "timedout") == 0){
  31. if(verbose)
  32. print("%d: noted: %s\n", getpid(), s);
  33. print("FAIL: timedout\n");
  34. exits("FAIL");
  35. }
  36. return 0;
  37. }
  38. void
  39. killKiller(void)
  40. {
  41. postnote(PNPROC, killerProc, "interrupt");
  42. }
  43. void
  44. stopAllAfter(int seconds)
  45. {
  46. int pid;
  47. switch((pid = rfork(RFMEM|RFPROC|RFNOWAIT)))
  48. {
  49. case 0:
  50. if(verbose)
  51. print("killer proc started: pid %d\n", getpid());
  52. sleep(seconds * 1000);
  53. postnote(PNGROUP, killerProc, "timedout");
  54. if(verbose)
  55. print("killer proc timedout: pid %d\n", getpid());
  56. exits("FAIL");
  57. case -1:
  58. fprint(2, "%r\n");
  59. exits("rfork fails");
  60. default:
  61. killerProc = pid;
  62. atexit(killKiller);
  63. }
  64. }
  65. void
  66. main(void)
  67. {
  68. int s, w;
  69. rfork(RFNOTEG);
  70. if (!atnotify(handletimeout, 1)){
  71. fprint(2, "%r\n");
  72. exits("atnotify fails");
  73. }
  74. r.l = &l;
  75. stopAllAfter(30);
  76. /* one process to sleep */
  77. switch((s = rfork(RFMEM|RFPROC|RFNOWAIT)))
  78. {
  79. case 0:
  80. ready = 1;
  81. qlock(&l);
  82. rsleep(&r);
  83. qunlock(&l);
  84. exits(nil);
  85. break;
  86. case -1:
  87. print("rfork: %r\n");
  88. exits("rfork fails");
  89. break;
  90. default:
  91. while(ready == 0)
  92. ;
  93. break;
  94. }
  95. /* one process to wakeup */
  96. switch((w = rfork(RFMEM|RFPROC|RFNOWAIT)))
  97. {
  98. case 0:
  99. qlock(&l);
  100. rwakeup(&r);
  101. ready = 2;
  102. qunlock(&l);
  103. exits(nil);
  104. break;
  105. case -1:
  106. print("rfork: %r\n");
  107. exits("rfork fails");
  108. break;
  109. default:
  110. while(ready == 1)
  111. ;
  112. break;
  113. }
  114. /* now, we try to wakeup a free Rendez */
  115. qlock(&l);
  116. if(rwakeup(&r) == 0){
  117. qunlock(&l);
  118. print("PASS\n");
  119. exits("PASS");
  120. }
  121. if((s = open(smprint("/proc/%d/ctl", s), OWRITE)) >= 0){
  122. write(s, "kill", 5);
  123. close(s);
  124. }
  125. if((w = open(smprint("/proc/%d/ctl", w), OWRITE)) >= 0){
  126. write(s, "kill", 5);
  127. close(s);
  128. }
  129. print("FAIL");
  130. exits("FAIL");
  131. }