msg_create.c 3.7 KB

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