msg_create.libos.c 2.2 KB

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