msg_create.libos.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include <shim_unistd.h>
  15. struct msg_buf {
  16. long mtype;
  17. char msgtext[512];
  18. };
  19. #define TEST_TIMES 1000
  20. #define DO_BENCH 1
  21. int create_q (int key)
  22. {
  23. int r = msgget(key, IPC_CREAT|0600);
  24. #ifndef DO_BENCH
  25. printf("The identifier used is %d\n",r);
  26. #endif
  27. if (r < 0) {
  28. perror("msgget\n");
  29. exit(-1);
  30. }
  31. #ifndef DO_BENCH
  32. else
  33. printf("Created a message queue\n");
  34. #endif
  35. return r;
  36. }
  37. int connect_q (int key)
  38. {
  39. int r = msgget(key, 0);
  40. #ifndef DO_BENCH
  41. printf("The identifier used is %d\n",r);
  42. #endif
  43. if (r < 0) {
  44. perror("msgget");
  45. exit(-1);
  46. }
  47. #ifndef DO_BENCH
  48. else
  49. printf("Connected the message queue\n");
  50. #endif
  51. return r;
  52. }
  53. int keys[TEST_TIMES];
  54. int ids[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. ids[i] = create_q(keys[i]);
  63. for (i = 0; i < TEST_TIMES; i++)
  64. msgpersist(ids[i], MSGPERSIST_STORE);
  65. gettimeofday(&tv2, NULL);
  66. printf("time spent on %d creation: %llu microsecond\n",
  67. TEST_TIMES,
  68. (tv2.tv_sec * 1000000ULL + tv2.tv_usec) -
  69. (tv1.tv_sec * 1000000ULL + tv1.tv_usec));
  70. }
  71. /* client always connects queues */
  72. int client (void)
  73. {
  74. struct timeval tv1, tv2;
  75. int i;
  76. gettimeofday(&tv1, NULL);
  77. for (i = 0; i < TEST_TIMES; i++)
  78. msgpersist(ids[i], MSGPERSIST_LOAD);
  79. gettimeofday(&tv2, NULL);
  80. printf("time spent on %d connection: %llu microsecond\n",
  81. TEST_TIMES,
  82. (tv2.tv_sec * 1000000ULL + tv2.tv_usec) -
  83. (tv1.tv_sec * 1000000ULL + tv1.tv_usec));
  84. for (i= 0; i < TEST_TIMES ; i++)
  85. msgctl(ids[i], IPC_RMID, NULL);
  86. }
  87. int main (int argc, char ** argv)
  88. {
  89. for (int i = 0; i < TEST_TIMES; i++)
  90. keys[i] = rand();
  91. server();
  92. client();
  93. return 0;
  94. }