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