2 * Copyright (c) 2009, Stathis Kamperis
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 #include <unistd.h> /* fork() */
38 #define MQNAME "/t_mq_prio"
42 /* Function prototypes. */
43 static void myhandler(int sig);
47 const char lowmsg[] = "Parent says hello";
48 const char highmsg[] = "Parent asks for HELP";
53 signal(SIGABRT, myhandler);
55 /* Create a message queue for write only with default parameters. */
56 md = mq_open(MQNAME, O_CREAT | O_EXCL | O_WRONLY, 0700, NULL);
59 /* Send messages, low first, then high. */
60 rv = mq_send(md, lowmsg, sizeof(lowmsg), /* priority */ 0);
63 rv = mq_send(md, highmsg, sizeof(highmsg), /* priority */ 1);
66 /* Disassociate with message queue. */
70 /* Fork and have child read the message from parent. */
75 /* We are inside the child. */
76 md = mq_open(MQNAME, O_RDONLY);
79 char msg_recvd[8192]; /* Implementation defined. */
80 rv = mq_receive(md, msg_recvd, sizeof(msg_recvd), &prio);
83 * Make sure that the message with higher priority,
84 * is delivered first of all.
86 assert(strcmp(msg_recvd, highmsg) == 0 && prio == 1);
88 rv = mq_receive(md, msg_recvd, sizeof(msg_recvd), &prio);
90 assert(strcmp(msg_recvd, lowmsg) == 0 && prio == 0);
92 /* Disassociate with message queue. */
96 /* Remove the message queue from the system. */
97 rv = mq_unlink(MQNAME);
102 /* We are inside the parent. */
104 assert(wait(&status) == pid);
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.
123 /* After this, the program will abort. */