mq.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2010-11-17 Bernard first version
  9. */
  10. #include <stdio.h>
  11. #include <fcntl.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <pthread.h>
  15. #include <mqueue.h>
  16. #define MQ_NAME_1 "testmsg1"
  17. #define MQ_NAME_2 "testmsg2"
  18. #define MSG_SIZE 128
  19. #define MAX_MSG 3
  20. const char *s_msg_ptr[] = {"msg test 1", "msg test 2", "msg test 3"};
  21. char r_msg_ptr_1[MAX_MSG][MSG_SIZE];
  22. char r_msg_ptr_2[MAX_MSG][MSG_SIZE];
  23. pthread_t send1, send2, rev1, rev2;
  24. int * send_1(void * mq)
  25. {
  26. int i;
  27. mqd_t mq1 = *(mqd_t *)mq;
  28. printf("Enter into send_1 \n");
  29. for (i = 0; i < MAX_MSG; i++ ) {
  30. if ( -1 == mq_send(mq1, s_msg_ptr[i], MSG_SIZE, i)) {
  31. perror("mq_send doesn't return success \n");
  32. pthread_exit((void *)1);
  33. }
  34. printf("[%d] send '%s' in thread send_1. \n", i+1, s_msg_ptr[i]);
  35. }
  36. pthread_exit((void *)0);
  37. }
  38. int * send_2(void * mq)
  39. {
  40. int i;
  41. mqd_t mq2 = *(mqd_t *)mq;
  42. printf("Enter into send_2 \n");
  43. for (i = 0; i < MAX_MSG; i++ ) {
  44. if ( -1 == mq_send(mq2, s_msg_ptr[i], MSG_SIZE, i)) {
  45. perror("mq_send doesn't return success \n");
  46. pthread_exit((void *)1);
  47. }
  48. printf("[%d] send '%s' in thread send_2. \n", i+1, s_msg_ptr[i]);
  49. }
  50. pthread_exit((void *)0);
  51. }
  52. int * receive_1(void * mq)
  53. {
  54. int i;
  55. mqd_t mq1 = *(mqd_t *)mq;
  56. printf("Enter into receive_1 \n");
  57. for (i = 0; i< MAX_MSG; i++) {
  58. if ( -1 == mq_receive(mq1, r_msg_ptr_1[i], MSG_SIZE, NULL) ) {
  59. perror("mq_receive doesn't return success \n");
  60. pthread_exit((void *)1);
  61. }
  62. printf("[%d] receive '%s' in thread receive_1. \n", i+1, r_msg_ptr_1[i]);
  63. }
  64. pthread_exit((void *)0);
  65. }
  66. int * receive_2(void * mq)
  67. {
  68. int i;
  69. mqd_t mq2 = *(mqd_t *)mq;
  70. printf("Enter into receive_2 \n");
  71. for (i = 0; i< MAX_MSG; i++) {
  72. if ( -1 == mq_receive(mq2, r_msg_ptr_2[i], MSG_SIZE, NULL) ) {
  73. perror("mq_receive doesn't return success \n");
  74. pthread_exit((void *)1);
  75. }
  76. printf("[%d] receive '%s' in thread receive_2. \n", i+1, r_msg_ptr_2[i]);
  77. }
  78. pthread_exit((void *)0);
  79. }
  80. int libc_mq()
  81. {
  82. mqd_t mq1 = 0, mq2 = 0;
  83. struct mq_attr mqstat;
  84. int oflag = O_CREAT|O_RDWR;
  85. memset(&mqstat, 0, sizeof(mqstat));
  86. mqstat.mq_maxmsg = MAX_MSG;
  87. mqstat.mq_msgsize = MSG_SIZE;
  88. mqstat.mq_flags = 0;
  89. if( ((mqd_t) -1) == (mq1 = mq_open(MQ_NAME_1,oflag,0777, &mqstat)) ) {
  90. printf("mq_open doesn't return success \n");
  91. return -1;
  92. }
  93. if( ((mqd_t) -1) == (mq2 = mq_open(MQ_NAME_2,oflag,0777, &mqstat)) ) {
  94. printf("mq_open doesn't return success \n");
  95. return -1;
  96. }
  97. pthread_create(&send1, NULL, (void *)send_1, (void *)&mq1);
  98. pthread_create(&send2, NULL, (void *)send_2, (void *)&mq2);
  99. pthread_create(&rev1, NULL, (void *)receive_1, (void *)&mq1);
  100. pthread_create(&rev2, NULL, (void *)receive_2, (void *)&mq2);
  101. pthread_join(send1, NULL);
  102. pthread_join(send2, NULL);
  103. pthread_join(rev1, NULL);
  104. pthread_join(rev2, NULL);
  105. mq_close(mq1);
  106. mq_close(mq2);
  107. mq_unlink(MQ_NAME_1);
  108. mq_unlink(MQ_NAME_2);
  109. printf("PASSED\n");
  110. return 0;
  111. }
  112. #include <finsh.h>
  113. FINSH_FUNCTION_EXPORT(libc_mq, posix mqueue test);