Initial import of t_shmat.
[pcca-tests.git] / mqueue.h / t_mq_parent_child_multmsg.c
CommitLineData
322a3d9c
SK
1/*
2 * Copyright (c) 2009, Stathis Kamperis
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
18 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
24 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
8b527d10 28#include <assert.h>
782efee9 29#include <errno.h>
8b527d10
SK
30#include <fcntl.h>
31#include <mqueue.h>
32#include <stdio.h>
782efee9 33#include <string.h>
8b527d10
SK
34#include <stdlib.h>
35#include <unistd.h> /* fork() */
36
782efee9
SK
37#include <sys/types.h>
38#include <sys/wait.h>
39
322a3d9c 40#define MQNAME "/t_mq_parent_child_multmsg"
8b527d10 41
f39ab718 42int
43main(void)
8b527d10 44{
782efee9
SK
45 /* Parent recites a poem. */
46 const char *msg[] = { "But I, being poor, have only my dreams;",
47 "I have spread my dreams under your feet;",
48 "Tread softly because you tread on my",
49 "dreams.",
50 "W.B. Yeats" };
8b527d10 51
f39ab718 52 /* Create the message queue. */
53 mqd_t md;
54 md = mq_open(MQNAME, O_CREAT | O_EXCL | O_WRONLY, 0700, NULL);
55 assert(md != (mqd_t)-1);
8b527d10 56
782efee9 57 /* Send messages. */
f39ab718 58 size_t i, N;
59
60 N = sizeof(msg) / sizeof(msg[0]);
61 for (i = 0; i < N; i++) {
62 assert(mq_send(md, msg[i], strlen(msg[i]) + 1,
63 /* priority */ 0) != -1);
782efee9 64 }
8b527d10
SK
65
66 /* Disassociate with message queue. */
f39ab718 67 assert(mq_close(md) != -1);
8b527d10
SK
68
69 /* Fork and have child read the message from parent. */
f39ab718 70 pid_t pid;
8b527d10 71 pid = fork();
f39ab718 72 assert(pid != -1);
73
74 if (pid == 0) {
a7064607 75 /* We are inside the child. */
f39ab718 76 md = mq_open(MQNAME, O_RDONLY | O_NONBLOCK);
77 assert(md != (mqd_t)-1);
8b527d10
SK
78
79 char msg_recvd[8192]; /* Implementation defined. */
f39ab718 80 for (i = 0; i < N; i++) {
81 assert(mq_receive(md, msg_recvd, sizeof(msg_recvd),
82 /* priority */ NULL) != -1);
8b527d10 83
8a60e009
SK
84 /*printf("%s\n", msg_recvd);*/
85 assert(strcmp(msg_recvd, msg[i]) == 0);
782efee9 86 }
8b527d10
SK
87
88 /* Disassociate with message queue. */
f39ab718 89 assert(mq_close(md) != -1);
8b527d10
SK
90
91 /* Remove the message queue from the system. */
f39ab718 92 assert(mq_unlink(MQNAME) != -1);
8b527d10
SK
93 } else {
94 /* We are inside the parent. */
f39ab718 95
96 /* Wait for child to complete. */
782efee9 97 int status;
f39ab718 98 assert(wait(&status) == pid);
99
100 printf("passed\n");
8b527d10
SK
101 }
102
f39ab718 103 /* Only reached by child. */
8b527d10
SK
104 return EXIT_SUCCESS;
105}