forktest.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * forktest - test fork().
  31. *
  32. * This should work correctly when fork is implemented.
  33. *
  34. * It should also continue to work after subsequent assignments, most
  35. * notably after implementing the virtual memory system.
  36. */
  37. #include <unistd.h>
  38. #include <string.h>
  39. #include <stdlib.h>
  40. #include <stdio.h>
  41. #include <err.h>
  42. /*
  43. * This is used by all processes, to try to help make sure all
  44. * processes have a distinct address space.
  45. */
  46. static volatile int mypid;
  47. /*
  48. * Helper function for fork that prints a warning on error.
  49. */
  50. static
  51. int
  52. dofork(void)
  53. {
  54. int pid;
  55. pid = fork();
  56. if (pid < 0) {
  57. warn("fork");
  58. }
  59. return pid;
  60. }
  61. /*
  62. * Check to make sure each process has its own address space. Write
  63. * the pid into the data segment and read it back repeatedly, making
  64. * sure it's correct every time.
  65. */
  66. static
  67. void
  68. check(void)
  69. {
  70. int i;
  71. mypid = getpid();
  72. /* Make sure each fork has its own address space. */
  73. for (i=0; i<800; i++) {
  74. volatile int seenpid;
  75. seenpid = mypid;
  76. if (seenpid != getpid()) {
  77. errx(1, "pid mismatch (%d, should be %d) "
  78. "- your vm is broken!",
  79. seenpid, getpid());
  80. }
  81. }
  82. }
  83. /*
  84. * Wait for a child process.
  85. *
  86. * This assumes dowait is called the same number of times as dofork
  87. * and passed its results in reverse order. Any forks that fail send
  88. * us -1 and are ignored. The first 0 we see indicates the fork that
  89. * generated the current process; that means it's time to exit. Only
  90. * the parent of all the processes returns from the chain of dowaits.
  91. */
  92. static
  93. void
  94. dowait(int nowait, int pid)
  95. {
  96. int x;
  97. if (pid<0) {
  98. /* fork in question failed; just return */
  99. return;
  100. }
  101. if (pid==0) {
  102. /* in the fork in question we were the child; exit */
  103. exit(0);
  104. }
  105. if (!nowait) {
  106. if (waitpid(pid, &x, 0)<0) {
  107. warn("waitpid");
  108. }
  109. else if (WIFSIGNALED(x)) {
  110. warnx("pid %d: signal %d", pid, WTERMSIG(x));
  111. }
  112. else if (WEXITSTATUS(x) != 0) {
  113. warnx("pid %d: exit %d", pid, WEXITSTATUS(x));
  114. }
  115. }
  116. }
  117. /*
  118. * Actually run the test.
  119. */
  120. static
  121. void
  122. test(int nowait)
  123. {
  124. int pid0, pid1, pid2, pid3;
  125. /*
  126. * Caution: This generates processes geometrically.
  127. *
  128. * It is unrolled to encourage gcc to registerize the pids,
  129. * to prevent wait/exit problems if fork corrupts memory.
  130. */
  131. pid0 = dofork();
  132. putchar('0');
  133. check();
  134. pid1 = dofork();
  135. putchar('1');
  136. check();
  137. pid2 = dofork();
  138. putchar('2');
  139. check();
  140. pid3 = dofork();
  141. putchar('3');
  142. check();
  143. /*
  144. * These must be called in reverse order to avoid waiting
  145. * improperly.
  146. */
  147. dowait(nowait, pid3);
  148. dowait(nowait, pid2);
  149. dowait(nowait, pid1);
  150. dowait(nowait, pid0);
  151. putchar('\n');
  152. }
  153. int
  154. main(int argc, char *argv[])
  155. {
  156. int nowait=0;
  157. if (argc==2 && !strcmp(argv[1], "-w")) {
  158. nowait=1;
  159. }
  160. else if (argc!=1 && argc!=0) {
  161. warnx("usage: forktest [-w]");
  162. return 1;
  163. }
  164. warnx("Starting.");
  165. test(nowait);
  166. warnx("Complete.");
  167. return 0;
  168. }