Initial import of t_shmat.
[pcca-tests.git] / mqueue.h / t_mq_prio.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
9cf1119a
SK
28#include <assert.h>
29#include <fcntl.h>
30#include <mqueue.h>
e3c4e8b4 31#include <signal.h>
9cf1119a
SK
32#include <stdio.h>
33#include <string.h>
34#include <stdlib.h>
35#include <unistd.h> /* fork() */
36#include <sys/wait.h>
37
322a3d9c 38#define MQNAME "/t_mq_prio"
9cf1119a
SK
39
40mqd_t md;
41
42/* Function prototypes. */
e3c4e8b4 43static void myhandler(int sig);
9cf1119a
SK
44
45int main(void)
46{
47 const char lowmsg[] = "Parent says hello";
48 const char highmsg[] = "Parent asks for HELP";
49 unsigned int prio;
50 int rv;
51 pid_t pid;
52
e3c4e8b4
SK
53 signal(SIGABRT, myhandler);
54
9cf1119a
SK
55 /* Create a message queue for write only with default parameters. */
56 md = mq_open(MQNAME, O_CREAT | O_EXCL | O_WRONLY, 0700, NULL);
e3c4e8b4 57 assert (md != -1);
9cf1119a
SK
58
59 /* Send messages, low first, then high. */
60 rv = mq_send(md, lowmsg, sizeof(lowmsg), /* priority */ 0);
e3c4e8b4 61 assert(rv != -1);
9cf1119a
SK
62
63 rv = mq_send(md, highmsg, sizeof(highmsg), /* priority */ 1);
e3c4e8b4 64 assert(rv != -1);
9cf1119a
SK
65
66 /* Disassociate with message queue. */
67 rv = mq_close(md);
e3c4e8b4 68 assert(rv != -1);
9cf1119a
SK
69
70 /* Fork and have child read the message from parent. */
71 pid = fork();
e3c4e8b4
SK
72 assert(pid != -1);
73
74 if (pid == 0) {
9cf1119a
SK
75 /* We are inside the child. */
76 md = mq_open(MQNAME, O_RDONLY);
e3c4e8b4 77 assert(md != -1);
9cf1119a
SK
78
79 char msg_recvd[8192]; /* Implementation defined. */
80 rv = mq_receive(md, msg_recvd, sizeof(msg_recvd), &prio);
e3c4e8b4 81 assert(rv != -1);
9cf1119a
SK
82 /*
83 * Make sure that the message with higher priority,
84 * is delivered first of all.
85 */
86 assert(strcmp(msg_recvd, highmsg) == 0 && prio == 1);
87
88 rv = mq_receive(md, msg_recvd, sizeof(msg_recvd), &prio);
e3c4e8b4 89 assert(rv != -1);
9cf1119a
SK
90 assert(strcmp(msg_recvd, lowmsg) == 0 && prio == 0);
91
92 /* Disassociate with message queue. */
93 rv = mq_close(md);
e3c4e8b4 94 assert(rv != -1);
9cf1119a
SK
95
96 /* Remove the message queue from the system. */
97 rv = mq_unlink(MQNAME);
e3c4e8b4 98 assert(rv != -1);
9cf1119a
SK
99
100 printf("passed\n");
101 } else {
102 /* We are inside the parent. */
103 int status;
33b16a4f 104 assert(wait(&status) == pid);
9cf1119a
SK
105 }
106
107 return EXIT_SUCCESS;
108}
109
e3c4e8b4
SK
110static void
111myhandler(int sig)
9cf1119a 112{
9cf1119a
SK
113 /*
114 * Message queues' name & resources are persistent, i.e., they live
115 * even after the process dies. That's why, disassociate and destroy
116 * the queue on failure, or else we might end up with zombie queues and
117 * hit the limit of max open queues.
118 * Also, we don't care about the return value of the following calls.
119 */
120 mq_close(md);
121 mq_unlink(MQNAME);
122
e3c4e8b4 123 /* After this, the program will abort. */
9cf1119a 124}