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() */
37 #define MQNAME "/t_mq_select"
41 const char msg[] = "Child says hello to parent";
42 struct timeval abs_timeout;
47 /* Create a message queue for write only with default parameters. */
48 md = mq_open(MQNAME, O_CREAT | O_EXCL | O_RDWR, 0777, NULL);
51 /* Initialize descriptor set to null set. */
54 /* Add message queue descriptor to set. */
56 assert(FD_ISSET(md, &ms)); /* Just a sanity check. */
58 /* Wait for 2 seconds. */
59 abs_timeout.tv_sec = 2;
60 abs_timeout.tv_usec = 0;
62 /* This should time out. */
63 assert(select(md + 1, &ms, NULL, NULL, &abs_timeout) == 0);
66 assert(FD_ISSET(md, &ms)); /* Just a sanity check. */
73 /* We are inside the child. */
74 /* Wait for parent to block on select(). */
78 assert(mq_send(md, msg, sizeof(msg), /* priority */ 0) != -1);
80 /* We are inside the parent. */
81 assert(select(md + 1, &ms, NULL, NULL, &abs_timeout) > 0);
82 assert(FD_ISSET(md, &ms));
84 char msg_recvd[8192]; /* Implementation defined. */
85 assert(mq_receive(md, msg_recvd, sizeof(msg_recvd), NULL) != -1);
86 assert(strcmp(msg_recvd, msg) == 0);
88 /* Remove message queue descriptor from the set. */
92 * At this point we know for sure that the child has completed,
93 * otherwise we would still be blocked by select().
96 /* Disassociate with message queue. */
97 assert(mq_close(md) != -1);
99 /* Remove the message queue from the system. */
100 assert(mq_unlink(MQNAME) != -1);
105 return (EXIT_SUCCESS);