1
2 /*
3 * Copyright (c) 2002, Intel Corporation. All rights reserved.
4 * Created by: crystal.xiong REMOVE-THIS AT intel DOT com
5 * This file is licensed under the GPL license. For the full content
6 * of this license, see the COPYING file at the top level of this
7 * source tree.
8 *
9 * 1. Two threads sending/receiving on different message queue.
10 * 2. Set different Priority to the messages in the message queue, to
11 * see whether the highest priority is received first.
12 */
13
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <stdlib.h>
18 #include <sys/wait.h>
19 #include <sys/mman.h>
20 #include <string.h>
21 #include <getopt.h>
22 #include <pthread.h>
23 #include <limits.h>
24 #include <mqueue.h>
25
26 #include "posixtest.h"
27
28 #define MQ_NAME_1 "/testmsg1"
29 #define MQ_NAME_2 "/testmsg2"
30 #define MSG_SIZE 128
31 #define MAX_MSG 3
32
33 const char *s_msg_ptr[] = { "msg test 1", "msg test 2", "msg test 3" };
34
35 char r_msg_ptr_1[MAX_MSG][MSG_SIZE];
36 char r_msg_ptr_2[MAX_MSG][MSG_SIZE];
37 pthread_t send1, send2, rev1, rev2;
38
send_1(void * mq)39 int *send_1(void *mq)
40 {
41 int i;
42 mqd_t mq1 = *(mqd_t *) mq;
43
44 printf("Enter into send_1 \n");
45 for (i = 0; i < MAX_MSG; i++) {
46 if (-1 == mq_send(mq1, s_msg_ptr[i], MSG_SIZE, i)) {
47 perror("mq_send doesn't return success \n");
48 pthread_exit((void *)1);
49 }
50 printf("[%d] send '%s' in thread send_1. \n", i + 1,
51 s_msg_ptr[i]);
52 }
53 pthread_exit(NULL);
54
55 }
56
send_2(void * mq)57 int *send_2(void *mq)
58 {
59 int i;
60 mqd_t mq2 = *(mqd_t *) mq;
61
62 printf("Enter into send_2 \n");
63 for (i = 0; i < MAX_MSG; i++) {
64 if (-1 == mq_send(mq2, s_msg_ptr[i], MSG_SIZE, i)) {
65 perror("mq_send doesn't return success \n");
66 pthread_exit((void *)1);
67 }
68 printf("[%d] send '%s' in thread send_2. \n", i + 1,
69 s_msg_ptr[i]);
70 }
71 pthread_exit(NULL);
72 }
73
receive_1(void * mq)74 int *receive_1(void *mq)
75 {
76 int i;
77 mqd_t mq1 = *(mqd_t *) mq;
78
79 printf("Enter into receive_1 \n");
80 for (i = 0; i < MAX_MSG; i++) {
81 if (-1 == mq_receive(mq1, r_msg_ptr_1[i], MSG_SIZE, NULL)) {
82 perror("mq_receive doesn't return success \n");
83 pthread_exit((void *)1);
84 }
85 printf("[%d] receive '%s' in thread receive_1. \n", i + 1,
86 r_msg_ptr_1[i]);
87 }
88 pthread_exit(NULL);
89 }
90
receive_2(void * mq)91 int *receive_2(void *mq)
92 {
93 int i;
94 mqd_t mq2 = *(mqd_t *) mq;
95
96 printf("Enter into receive_2 \n");
97 for (i = 0; i < MAX_MSG; i++) {
98 if (-1 == mq_receive(mq2, r_msg_ptr_2[i], MSG_SIZE, NULL)) {
99 perror("mq_receive doesn't return success \n");
100 pthread_exit((void *)1);
101 }
102 printf("[%d] receive '%s' in thread receive_2. \n", i + 1,
103 r_msg_ptr_2[i]);
104 }
105 pthread_exit(NULL);
106 }
107
main(void)108 int main(void)
109 {
110
111 mqd_t mq1 = 0, mq2 = 0;
112 struct mq_attr mqstat;
113 int oflag = O_CREAT | O_NONBLOCK | O_RDWR;
114
115 memset(&mqstat, 0, sizeof(mqstat));
116 mqstat.mq_maxmsg = MAX_MSG;
117 mqstat.mq_msgsize = MSG_SIZE;
118 mqstat.mq_flags = 0;
119
120 if ((mq1 = mq_open(MQ_NAME_1, oflag, 0777, &mqstat)) == (mqd_t)-1) {
121 printf("mq_open doesn't return success \n");
122 return PTS_UNRESOLVED;
123 }
124 if ((mq2 = mq_open(MQ_NAME_2, oflag, 0777, &mqstat)) == (mqd_t)-1) {
125 printf("mq_open doesn't return success \n");
126 return PTS_UNRESOLVED;
127 }
128 pthread_create(&send1, NULL, (void *)send_1, (void *)&mq1);
129 pthread_create(&send2, NULL, (void *)send_2, (void *)&mq2);
130 pthread_create(&rev1, NULL, (void *)receive_1, (void *)&mq1);
131 pthread_create(&rev2, NULL, (void *)receive_2, (void *)&mq2);
132 pthread_join(send1, NULL);
133 pthread_join(send2, NULL);
134 pthread_join(rev1, NULL);
135 pthread_join(rev2, NULL);
136
137 mq_close(mq1);
138 mq_close(mq2);
139 mq_unlink(MQ_NAME_1);
140 mq_unlink(MQ_NAME_2);
141 return PTS_PASS;
142 }
143