mq.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <pthread.h>
  2. #include <mqueue.h>
  3. #include <sys/fcntl.h>
  4. void child(void* parameter)
  5. {
  6. char msg[16];
  7. ssize_t len;
  8. mqd_t mq;
  9. int i;
  10. char expect;
  11. printf("Child here!\n");
  12. if((mq = mq_open("Q001", O_RDONLY, 0, 0)) == -1)
  13. {
  14. printf("mq_open = %d\n", errno);
  15. pthread_exit(0);
  16. }
  17. else printf("mq = %x\n", &mq);
  18. for(expect = 'V'; expect <= 'Z'; ++expect)
  19. {
  20. memset(msg, 0, sizeof(msg));
  21. printf("Child waiting...\n");
  22. if((len = mq_receive(mq, msg, sizeof(msg), 0)) == -1)
  23. {
  24. printf("mq_receive = %d\n", errno);
  25. pthread_exit(0);
  26. }
  27. else printf("GOT MESSAGE %c!\n", expect);
  28. if(len != 8)
  29. printf("expected len 8 got %d\n", len);
  30. for(i = 0; i < 8; ++i)
  31. if(msg[i] != expect)
  32. printf("msg[%d] got %c expected %c\n", i, msg[i], expect);
  33. }
  34. if(mq_close(mq) == -1)
  35. printf("mq_close failed\n");
  36. printf("child done\n");
  37. pthread_exit(0);
  38. }
  39. void send(void* parameter)
  40. {
  41. mqd_t mq;
  42. char msg[8];
  43. printf("Send here!\n");
  44. if((mq = mq_open("Q001", O_WRONLY, 0, 0)) == -1)
  45. {
  46. printf("mq_open = %d\n", errno);
  47. pthread_exit(0);
  48. }
  49. else printf("mq = %x\n", &mq);
  50. /* task will be blocked waiting for message */
  51. memset(msg, 'Z', 8);
  52. if(mq_send(mq, msg, 8, 0) == -1)
  53. printf("mq_send = %d\n", errno);
  54. if(mq_close(mq) == -1)
  55. printf("mq_close failed\n");
  56. printf("Send done\n");
  57. pthread_exit(0);
  58. }
  59. void test_thread(void* parameter)
  60. {
  61. struct mq_attr attr;
  62. mqd_t mq;
  63. char msg[8];
  64. pthread_t pthrId;
  65. printf("Main here!\n");
  66. attr.mq_flags = 0;
  67. attr.mq_maxmsg = 3;
  68. attr.mq_msgsize = 16;
  69. if((mq = mq_open("Q001", O_CREAT | O_RDWR, 0, &attr)) == -1)
  70. {
  71. printf("mq_open = %d\n", errno);
  72. pthread_exit(0);
  73. }
  74. else printf("mq = %x\n", &mq);
  75. memset(msg, 'V', 8);
  76. if(mq_send(mq, msg, 8, 0) == -1)
  77. printf("mq_send = %d\n", errno);
  78. memset(msg, 'W', 8);
  79. if(mq_send(mq, msg, 8, 0) == -1)
  80. printf("mq_send = %d\n", errno);
  81. memset(msg, 'X', 8);
  82. if(mq_send(mq, msg, 8, 0) == -1)
  83. printf("mq_send = %d\n", errno);
  84. if(pthread_create(&pthrId, 0, &child, 0)!=0)
  85. {
  86. printf("pthread_create Child = %d\n", errno);
  87. pthread_exit(0);
  88. }
  89. /* Should block */
  90. printf("send Y\n");
  91. memset(msg, 'Y', 8);
  92. if(mq_send(mq, msg, 8, 0) == -1)
  93. printf("mq_send = %d\n", errno);
  94. else printf("sent Y\n");
  95. if(pthread_create(&pthrId, 0, send, 0)!=0)
  96. {
  97. printf("pthread_create Send = %d\n", errno);
  98. pthread_exit(0);
  99. }
  100. if(mq_close(mq) == -1)
  101. printf("mq_close failed\n");
  102. printf("main done\n");
  103. pthread_exit(0);
  104. }
  105. #include <finsh.h>
  106. void libc_mq()
  107. {
  108. rt_thread_t tid;
  109. tid = rt_thread_create("mqtest", test_thread, RT_NULL,
  110. 2048, 20, 5);
  111. if (tid != RT_NULL)
  112. {
  113. rt_thread_startup(tid);
  114. }
  115. }
  116. FINSH_FUNCTION_EXPORT(libc_mq, mqueue test);