sem.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8. #include <sys/ipc.h>
  9. #include <sys/sem.h>
  10. #include <sys/time.h>
  11. #include <sys/wait.h>
  12. #define TEST_TIMES 1000
  13. #define DO_BENCH 1
  14. enum { PARALLEL, SERIAL, IN_PROCESS } mode = PARALLEL;
  15. int pipefds[2], key;
  16. /* server always sends messages */
  17. void server (void)
  18. {
  19. struct timeval tv1, tv2;
  20. int semid;
  21. struct sembuf buf;
  22. if ((semid = semget(key, 2, mode == SERIAL ? 0600|IPC_CREAT : 0)) < 0) {
  23. perror("semget");
  24. exit(1);
  25. }
  26. gettimeofday(&tv1, NULL);
  27. for (int i = 0 ; i < TEST_TIMES ; i++) {
  28. buf.sem_num = 0;
  29. buf.sem_op = 1;
  30. buf.sem_flg = 0;
  31. if (semop(semid, &buf, 1) < 0) {
  32. perror("semop");
  33. exit(1);
  34. }
  35. #ifndef DO_BENCH
  36. printf("Semaphore %d signaled\n", i);
  37. #endif
  38. }
  39. gettimeofday(&tv2, NULL);
  40. printf("time spent on %d semop (signal): %llu microsecond\n",
  41. TEST_TIMES,
  42. (tv2.tv_sec * 1000000ull + tv2.tv_usec) -
  43. (tv1.tv_sec * 1000000ull + tv1.tv_usec));
  44. if (mode == PARALLEL) {
  45. close(pipefds[0]);
  46. char byte = 0;
  47. write(pipefds[1], &byte, 1);
  48. buf.sem_num = 1;
  49. buf.sem_op = -1;
  50. buf.sem_flg = 0;
  51. if (semop(semid, &buf, 1) < 0) {
  52. perror("semop");
  53. exit(1);
  54. }
  55. semctl(semid, 0, IPC_RMID);
  56. }
  57. if (mode != IN_PROCESS)
  58. exit(0);
  59. }
  60. /* client always sends messages */
  61. void client (void)
  62. {
  63. struct timeval tv1, tv2;
  64. int semid;
  65. struct sembuf buf;
  66. if (mode == PARALLEL) {
  67. close(pipefds[1]);
  68. char byte = 0;
  69. read(pipefds[0], &byte, 1);
  70. }
  71. if ((semid = semget(key, 0, 0)) < 0) {
  72. perror("semget");
  73. exit(1);
  74. }
  75. gettimeofday(&tv1, NULL);
  76. for (int i = 0 ; i < TEST_TIMES ; i++) {
  77. buf.sem_num = 0;
  78. buf.sem_op = -1;
  79. buf.sem_flg = 0;
  80. if (semop(semid, &buf, 1) < 0) {
  81. perror("semop");
  82. exit(1);
  83. }
  84. #ifndef DO_BENCH
  85. printf("Semaphore %d wakened\n", i);
  86. #endif
  87. }
  88. gettimeofday(&tv2, NULL);
  89. if (mode == PARALLEL) {
  90. buf.sem_num = 1;
  91. buf.sem_op = 1;
  92. buf.sem_flg = 0;
  93. if (semop(semid, &buf, 1) < 0) {
  94. perror("semop");
  95. exit(1);
  96. }
  97. }
  98. else
  99. semctl(semid, 0, IPC_RMID);
  100. printf("time spent on %d semop (wait): %llu microsecond\n",
  101. TEST_TIMES,
  102. (tv2.tv_sec * 1000000ull + tv2.tv_usec) -
  103. (tv1.tv_sec * 1000000ull + tv1.tv_usec));
  104. if (mode != IN_PROCESS)
  105. exit(0);
  106. }
  107. int main (int argc, char ** argv)
  108. {
  109. int semid;
  110. key = rand();
  111. #ifndef DO_BENCH
  112. printf("Semaphore key: 0x%8x\n", key);
  113. #endif
  114. /* server run first and client run later */
  115. if (argc == 2 && strcmp(argv[1], "serial") == 0) {
  116. mode = SERIAL;
  117. if (fork() == 0)
  118. server();
  119. wait(NULL);
  120. if (fork() == 0)
  121. client();
  122. wait(NULL);
  123. }
  124. if ((semid = semget(key, 2, 0600|IPC_CREAT)) < 0) {
  125. perror("semget");
  126. exit(1);
  127. }
  128. /* server run first and client run later (in the same process) */
  129. if (argc == 2 && strcmp(argv[1], "in-process") == 0) {
  130. mode = IN_PROCESS;
  131. server();
  132. client();
  133. semctl(semid, 0, IPC_RMID);
  134. return 0;
  135. }
  136. pipe(pipefds);
  137. /* server to be the parent and client to be the child */
  138. if (argc == 1) {
  139. if (fork() == 0)
  140. client();
  141. else
  142. server();
  143. }
  144. /* client to be the parent and server to be the child */
  145. if (argc == 2 && strcmp(argv[1], "reverse") == 0) {
  146. if (fork() == 0)
  147. server();
  148. else
  149. client();
  150. }
  151. /* both client and server are children */
  152. if (argc == 2 && strcmp(argv[1], "children") == 0) {
  153. if (fork() == 0)
  154. server();
  155. if (fork() == 0)
  156. client();
  157. wait(NULL);
  158. wait(NULL);
  159. }
  160. semctl(semid, 0, IPC_RMID);
  161. return 0;
  162. }