msg_create_libos.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Test to create 100 message queues and query them from another process*/
  2. #define _XOPEN_SOURCE 700
  3. #include <shim_unistd.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/ipc.h>
  8. #include <sys/msg.h>
  9. #include <sys/time.h>
  10. #include <sys/types.h>
  11. #include <sys/wait.h>
  12. #include <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. 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. int r = msgget(key, 0);
  36. #ifndef DO_BENCH
  37. printf("The identifier used is %d\n", r);
  38. #endif
  39. if (r < 0) {
  40. perror("msgget");
  41. exit(-1);
  42. }
  43. #ifndef DO_BENCH
  44. else
  45. printf("Connected the message queue\n");
  46. #endif
  47. return r;
  48. }
  49. int keys[TEST_TIMES];
  50. int ids[TEST_TIMES];
  51. /* server always creates queues */
  52. void server(void) {
  53. struct timeval tv1, tv2;
  54. int i;
  55. gettimeofday(&tv1, NULL);
  56. for (i = 0; i < TEST_TIMES; i++) {
  57. ids[i] = create_q(keys[i]);
  58. }
  59. for (i = 0; i < TEST_TIMES; i++) {
  60. msgpersist(ids[i], MSGPERSIST_STORE);
  61. }
  62. gettimeofday(&tv2, NULL);
  63. printf("time spent on %d creation: %llu microsecond\n", TEST_TIMES,
  64. (tv2.tv_sec * 1000000ULL + tv2.tv_usec) - (tv1.tv_sec * 1000000ULL + tv1.tv_usec));
  65. }
  66. /* client always connects queues */
  67. void client(void) {
  68. struct timeval tv1, tv2;
  69. int i;
  70. gettimeofday(&tv1, NULL);
  71. for (i = 0; i < TEST_TIMES; i++) {
  72. msgpersist(ids[i], MSGPERSIST_LOAD);
  73. }
  74. gettimeofday(&tv2, NULL);
  75. printf("time spent on %d connection: %llu microsecond\n", TEST_TIMES,
  76. (tv2.tv_sec * 1000000ULL + tv2.tv_usec) - (tv1.tv_sec * 1000000ULL + tv1.tv_usec));
  77. for (i = 0; i < TEST_TIMES; i++) {
  78. msgctl(ids[i], IPC_RMID, NULL);
  79. }
  80. }
  81. int main(int argc, char** argv) {
  82. for (int i = 0; i < TEST_TIMES; i++) {
  83. keys[i] = rand();
  84. }
  85. server();
  86. client();
  87. return 0;
  88. }