lat_proc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * lat_proc.c - process creation tests
  3. *
  4. * Usage: lat_proc
  5. *
  6. * TODO - linux clone, plan9 rfork, IRIX sproc().
  7. *
  8. * Copyright (c) 1994 Larry McVoy. Distributed under the FSF GPL with
  9. * additional restriction that results may published only if
  10. * (1) the benchmark is unmodified, and
  11. * (2) the version in the sccsid below is included in the report.
  12. * Support for this development by Sun Microsystems is gratefully acknowledged.
  13. */
  14. char *id = "$Id$\n";
  15. #include "bench.h"
  16. #ifdef STATIC
  17. #define PROG "/tmp/hello-s"
  18. #else
  19. #define PROG "/tmp/hello"
  20. #endif
  21. void do_shell(void)
  22. {
  23. int pid;
  24. int status;
  25. switch (pid = fork()) {
  26. case -1:
  27. perror("fork");
  28. exit(1);
  29. case 0: /* child */
  30. close(1);
  31. execlp("/bin/sh", "sh", "-c", PROG, 0);
  32. exit(1);
  33. default:
  34. while (wait(&status) != pid)
  35. ;
  36. if (WEXITSTATUS(status))
  37. exit(WEXITSTATUS(status));
  38. }
  39. }
  40. void do_vforkexec(void)
  41. {
  42. int pid;
  43. char *nav[2];
  44. int status;
  45. nav[0] = PROG;
  46. nav[1] = 0;
  47. switch (pid = vfork()) {
  48. case -1:
  49. perror("vfork");
  50. exit(1);
  51. case 0: /* child */
  52. close(1);
  53. execve(PROG, nav, 0);
  54. exit(1);
  55. default:
  56. while (wait(&status) != pid)
  57. ;
  58. if (WEXITSTATUS(status))
  59. exit(WEXITSTATUS(status));
  60. }
  61. }
  62. void do_forkexec(void)
  63. {
  64. int pid;
  65. char *nav[2];
  66. int status;
  67. nav[0] = PROG;
  68. nav[1] = 0;
  69. switch (pid = fork()) {
  70. case -1:
  71. perror("fork");
  72. exit(1);
  73. case 0: /* child */
  74. close(1);
  75. execve(PROG, nav, 0);
  76. exit(1);
  77. default:
  78. while (wait(&status) != pid)
  79. ;
  80. if (WEXITSTATUS(status))
  81. exit(WEXITSTATUS(status));
  82. }
  83. }
  84. void do_fork(void)
  85. {
  86. int pid;
  87. int status;
  88. switch (pid = fork()) {
  89. case -1:
  90. perror("fork");
  91. exit(1);
  92. case 0: /* child */
  93. exit(0);
  94. default:
  95. while (wait(&status) != pid)
  96. ;
  97. if (WEXITSTATUS(status))
  98. exit(WEXITSTATUS(status));
  99. }
  100. }
  101. void do_dfork(void)
  102. {
  103. int pid;
  104. int status;
  105. switch (pid = fork()) {
  106. case -1:
  107. perror("fork");
  108. exit(1);
  109. case 0: /* child */
  110. do_fork();
  111. exit(0);
  112. default:
  113. while (wait(&status) != pid)
  114. ;
  115. if (WEXITSTATUS(status))
  116. exit(WEXITSTATUS(status));
  117. }
  118. }
  119. void do_dforkexec(void)
  120. {
  121. int pid;
  122. int status;
  123. switch (pid = fork()) {
  124. case -1:
  125. perror("fork");
  126. exit(1);
  127. case 0: /* child */
  128. do_forkexec();
  129. exit(0);
  130. default:
  131. while (wait(&status) != pid)
  132. ;
  133. if (WEXITSTATUS(status))
  134. exit(WEXITSTATUS(status));
  135. }
  136. }
  137. void do_procedure(int r)
  138. {
  139. use_int(r);
  140. }
  141. int
  142. main(int ac, char **av)
  143. {
  144. if (ac < 2) goto usage;
  145. if (!strcmp("procedure", av[1])) {
  146. BENCH(do_procedure(ac), 0);
  147. micro("Procedure call", get_n());
  148. } else if (!strcmp("fork", av[1])) {
  149. BENCH(do_fork(), 0);
  150. #ifdef STATIC
  151. micro("Static Process fork+exit", get_n());
  152. #else
  153. micro("Process fork+exit", get_n());
  154. #endif
  155. } else if (!strcmp("fork-size", av[1])) {
  156. size_t size = bytes(av[2]);
  157. memset(malloc(size), 0, size);
  158. BENCH(do_fork(), 0);
  159. #ifdef STATIC
  160. micro("Static Process fork+exit", get_n());
  161. #else
  162. micro("Process fork+exit", get_n());
  163. #endif
  164. } else if (!strcmp("dfork", av[1])) {
  165. BENCH(do_dfork(), 0);
  166. #ifdef STATIC
  167. micro("Static Process double fork+exit", get_n());
  168. #else
  169. micro("Process double fork+exit", get_n());
  170. #endif
  171. } else if (!strcmp("vfork", av[1])) {
  172. BENCH(do_vforkexec(), 0);
  173. #ifdef STATIC
  174. micro("Static Process vfork+execve", get_n());
  175. #else
  176. micro("Process vfork+execve", get_n());
  177. #endif
  178. } else if (!strcmp("exec", av[1])) {
  179. BENCH(do_forkexec(), 0);
  180. #ifdef STATIC
  181. micro("Static Process fork+execve", get_n());
  182. #else
  183. micro("Process fork+execve", get_n());
  184. #endif
  185. } else if (!strcmp("dforkexec", av[1])) {
  186. BENCH(do_dforkexec(), 0);
  187. #ifdef STATIC
  188. micro("Static Process double fork+execve", get_n());
  189. #else
  190. micro("Process double fork+execve", get_n());
  191. #endif
  192. } else if (!strcmp("shell", av[1])) {
  193. BENCH(do_shell(), 0);
  194. #ifdef STATIC
  195. micro("Static Process fork+/bin/sh -c", get_n());
  196. #else
  197. micro("Process fork+/bin/sh -c", get_n());
  198. #endif
  199. } else {
  200. usage: printf("Usage: %s fork|vfork|exec|shell|dfork|dforkexec\n", av[0]);
  201. }
  202. return(0);
  203. }