msg_create.c 3.9 KB

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