msg_create.c 3.7 KB

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