lat_proc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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("dfork", av[1])) {
  156. BENCH(do_dfork(), 0);
  157. #ifdef STATIC
  158. micro("Static Process double fork+exit", get_n());
  159. #else
  160. micro("Process double fork+exit", get_n());
  161. #endif
  162. } else if (!strcmp("vfork", av[1])) {
  163. BENCH(do_vforkexec(), 0);
  164. #ifdef STATIC
  165. micro("Static Process vfork+execve", get_n());
  166. #else
  167. micro("Process vfork+execve", get_n());
  168. #endif
  169. } else if (!strcmp("exec", av[1])) {
  170. BENCH(do_forkexec(), 0);
  171. #ifdef STATIC
  172. micro("Static Process fork+execve", get_n());
  173. #else
  174. micro("Process fork+execve", get_n());
  175. #endif
  176. } else if (!strcmp("dforkexec", av[1])) {
  177. BENCH(do_dforkexec(), 0);
  178. #ifdef STATIC
  179. micro("Static Process double fork+execve", get_n());
  180. #else
  181. micro("Process double fork+execve", get_n());
  182. #endif
  183. } else if (!strcmp("shell", av[1])) {
  184. BENCH(do_shell(), 0);
  185. #ifdef STATIC
  186. micro("Static Process fork+/bin/sh -c", get_n());
  187. #else
  188. micro("Process fork+/bin/sh -c", get_n());
  189. #endif
  190. } else {
  191. usage: printf("Usage: %s fork|vfork|exec|shell|dfork|dforkexec\n", av[0]);
  192. }
  193. return(0);
  194. }