1 /*
2 * Copyright (c) 2002, Intel Corporation. All rights reserved.
3 * Created by: crystal.xiong REMOVE-THIS AT intel DOT com
4 * This file is licensed under the GPL license. For the full content
5 * of this license, see the COPYING file at the top level of this
6 * source tree.
7 *
8 * Test two processes can read/write from the same message queue
9 * at the same time.
10 *
11 */
12
13 #include <sys/mman.h>
14 #include <sys/wait.h>
15 #include <fcntl.h>
16 #include <limits.h>
17 #include <mqueue.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include "posixtest.h"
23
24 #define MQ_NAME "/testmsg"
25 #define MSG_SIZE 128
26 #define MAX_MSG 3
27
main(void)28 int main(void)
29 {
30 struct mq_attr mqstat, attr;
31 char r_msg_ptr[MAX_MSG][MSG_SIZE];
32 const char *s_msg_ptr[] = { "msg test 1", "msg test 2", "msg test 3" };
33 int i;
34 int oflag = O_CREAT | O_RDWR;
35 int ret_code = PTS_PASS;
36 mqd_t mq = 0;
37 pid_t pid;
38
39 memset(&mqstat, 0, sizeof(mqstat));
40 mqstat.mq_maxmsg = MAX_MSG;
41 mqstat.mq_msgsize = MSG_SIZE;
42 mqstat.mq_flags = 0;
43
44 /* #ifndef _POSIX_MESSAGE_PASSING
45 printf("_POSIX_MESSAGE_PASSING is not defined \n");
46 return PTS_UNRESOLVED;
47 #endif */
48
49 if (((mqd_t) - 1) == (mq = mq_open(MQ_NAME, oflag, 0777, &mqstat))) {
50 perror("mq_open doesn't return success \n");
51 return PTS_UNRESOLVED;
52 }
53
54 switch ((pid = fork())) {
55 case -1:
56 perror("fork");
57 ret_code = PTS_UNRESOLVED;
58 break;
59 case 0:
60 mq_getattr(mq, &attr);
61 for (i = 0; i < MAX_MSG && ret_code == PTS_PASS; i++) {
62 printf("[%d] s_msg_ptr is '%s' \n", i + 1,
63 s_msg_ptr[i]);
64 printf("Prepare to send message...\n");
65 if (-1 == mq_send(mq, s_msg_ptr[i], attr.mq_msgsize, 1)) {
66 perror("mq_send doesn't return success \n");
67 ret_code = PTS_UNRESOLVED;
68 } else {
69 printf("Process %ld send message '%s' to "
70 "process %ld \n",
71 (long)getpid(), s_msg_ptr[i], (long)pid);
72 }
73 }
74 (void)wait(NULL);
75 break;
76 default:
77 printf("Enter into child process...\n");
78 mq_getattr(mq, &attr);
79 for (i = 0; i < MAX_MSG && ret_code == PTS_PASS; i++) {
80 printf("Prepare to receive [%d] messages...\n", i + 1);
81 if (-1 ==
82 mq_receive(mq, r_msg_ptr[i], attr.mq_msgsize,
83 NULL)) {
84 perror("mq_receive doesn't return success \n");
85 ret_code = PTS_UNRESOLVED;
86 } else {
87 printf("process %ld receive message '%s' from "
88 "process %ld \n",
89 (long)getpid(), r_msg_ptr[i],
90 (long)getppid());
91 }
92 }
93 exit(ret_code);
94
95 break;
96 }
97
98 (void)mq_close(mq);
99 (void)mq_unlink(MQ_NAME);
100
101 return ret_code;
102
103 }
104