msg_create.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* Test to create 100 message queues and query them from another process*/
  2. #define _GNU_SOURCE
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/ipc.h>
  7. #include <sys/msg.h>
  8. #include <sys/time.h>
  9. #include <sys/types.h>
  10. #include <sys/wait.h>
  11. #include <unistd.h>
  12. struct msg_buf {
  13. long mtype;
  14. char msgtext[512];
  15. };
  16. #define TEST_TIMES 1000
  17. #define DO_BENCH 1
  18. int create_q(int key) {
  19. int r = msgget(key, IPC_CREAT | 0600);
  20. #ifndef DO_BENCH
  21. printf("The identifier used is %d\n", r);
  22. #endif
  23. if (r < 0) {
  24. perror("msgget\n");
  25. exit(-1);
  26. }
  27. #ifndef DO_BENCH
  28. else
  29. printf("Created a message queue\n");
  30. #endif
  31. return r;
  32. }
  33. int connect_q(int key) {
  34. int r = msgget(key, 0);
  35. #ifndef DO_BENCH
  36. printf("The identifier used is %d\n", r);
  37. #endif
  38. if (r < 0) {
  39. perror("msgget");
  40. exit(-1);
  41. }
  42. #ifndef DO_BENCH
  43. else
  44. printf("Connected the message queue\n");
  45. #endif
  46. return r;
  47. }
  48. enum { PARALLEL, SERIAL, IN_PROCESS } mode = PARALLEL;
  49. int pipefds[4];
  50. int keys[TEST_TIMES];
  51. /* server always creates queues */
  52. void server(void) {
  53. struct timeval tv1, tv2;
  54. int i;
  55. gettimeofday(&tv1, NULL);
  56. for (i = 0; i < TEST_TIMES; i++) {
  57. create_q(keys[i]);
  58. }
  59. gettimeofday(&tv2, NULL);
  60. if (mode == PARALLEL) {
  61. close(pipefds[0]);
  62. char byte;
  63. if (write(pipefds[1], &byte, 1) != 1) {
  64. perror("write error");
  65. exit(1);
  66. }
  67. }
  68. if (mode == PARALLEL) {
  69. close(pipefds[3]);
  70. char byte;
  71. if (read(pipefds[2], &byte, 1) != 1) {
  72. perror("read error");
  73. exit(1);
  74. }
  75. }
  76. printf("time spent on %d creation: %llu microsecond\n", TEST_TIMES,
  77. (tv2.tv_sec * 1000000ULL + tv2.tv_usec) - (tv1.tv_sec * 1000000ULL + tv1.tv_usec));
  78. if (mode != IN_PROCESS)
  79. exit(0);
  80. }
  81. /* client always connects queues */
  82. void client(void) {
  83. struct timeval tv1, tv2;
  84. int i;
  85. int ids[TEST_TIMES];
  86. if (mode == PARALLEL) {
  87. close(pipefds[1]);
  88. char byte;
  89. if (read(pipefds[0], &byte, 1) != 1) {
  90. perror("read error");
  91. exit(1);
  92. }
  93. }
  94. gettimeofday(&tv1, NULL);
  95. for (i = 0; i < TEST_TIMES; i++) {
  96. ids[i] = connect_q(keys[i]);
  97. }
  98. gettimeofday(&tv2, NULL);
  99. for (i = 0; i < TEST_TIMES; i++) {
  100. msgctl(ids[i], IPC_RMID, NULL);
  101. }
  102. if (mode == PARALLEL) {
  103. close(pipefds[2]);
  104. char byte;
  105. if (write(pipefds[3], &byte, 1) != 1) {
  106. perror("write error");
  107. exit(1);
  108. }
  109. }
  110. printf("time spent on %d connection: %llu microsecond\n", TEST_TIMES,
  111. (tv2.tv_sec * 1000000ULL + tv2.tv_usec) - (tv1.tv_sec * 1000000ULL + tv1.tv_usec));
  112. if (mode != IN_PROCESS)
  113. exit(0);
  114. }
  115. int main(int argc, char** argv) {
  116. int i;
  117. for (i = 0; i < TEST_TIMES; i++) {
  118. keys[i] = rand();
  119. }
  120. if (pipe(pipefds) < 0 || pipe(pipefds + 2) < 0) {
  121. perror("pipe error");
  122. return 1;
  123. }
  124. /* server to be the parent and client to be the child */
  125. if (argc == 1) {
  126. if (fork() == 0)
  127. client();
  128. else
  129. server();
  130. }
  131. /* client to be the parent and server to be the child */
  132. if (argc == 2 && strcmp(argv[1], "reverse") == 0) {
  133. if (fork() == 0)
  134. server();
  135. else
  136. client();
  137. }
  138. /* both client and server are children */
  139. if (argc == 2 && strcmp(argv[1], "children") == 0) {
  140. if (fork() == 0)
  141. server();
  142. if (fork() == 0)
  143. client();
  144. wait(NULL);
  145. wait(NULL);
  146. }
  147. /* server run first and client run later */
  148. if (argc == 2 && strcmp(argv[1], "serial") == 0) {
  149. mode = SERIAL;
  150. if (fork() == 0)
  151. server();
  152. wait(NULL);
  153. if (fork() == 0)
  154. client();
  155. wait(NULL);
  156. }
  157. /* server run first and client run later (in the same process) */
  158. if (argc == 2 && strcmp(argv[1], "in-process") == 0) {
  159. mode = IN_PROCESS;
  160. server();
  161. client();
  162. }
  163. return 0;
  164. }