1068 Chapter 52
Figure 52-1 helps clarify a number of details of the use of message queue descrip-
tors (all of which are analogous to the use to file descriptors):
z An open message queue description has an associated set of flags. SUSv3 speci-
fies only one such flag, O_NONBLOCK, which determines whether I/O is nonblocking.
z Two processes can hold message queue descriptors (descriptor x in the diagram)
that refer to the same open message queue description. This can occur because
a process opens a message queue and then calls fork(). These descriptors share
the state of the O_NONBLOCK flag.
z Two processes can hold open message queue descriptors that refer to different
message queue descriptions that refer to the same message queue (e.g.,
descriptor z in process A and descriptor y in process B both refer to /mq-r). This
occurs because the two processes each used mq_open() to open the same queue.
52.4 Message Queue Attributes
The mq_open(), mq_getattr(), and mq_setattr() functions all permit an argument that
is a pointer to an mq_attr structure. This structure is defined in <mqueue.h> as follows:
struct mq_attr {
long mq_flags; /* Message queue description flags: 0 or
O_NONBLOCK [mq_getattr(), mq_setattr()] */
long mq_maxmsg; /* Maximum number of messages on queue
[mq_open(), mq_getattr()] */
long mq_msgsize; /* Maximum message size (in bytes)
[mq_open(), mq_getattr()] */
long mq_curmsgs; /* Number of messages currently in queue
[mq_getattr()] */
};
Before we look at the mq_attr structure in detail, it is worth noting the following
points:
z Only some of the fields are used by each of the three functions. The fields used
by each function are indicated in the comments accompanying the structure
definition above.
z The structure contains information about the open message queue description
(mq_flags) associated with a message descriptor and information about the
queue referred to by that descriptor (mq_maxmsg, mq_msgsize, mq_curmsgs).
z Some of the fields contain information that is fixed at the time the queue is created
with mq_open() (mq_maxmsg and mq_msgsize); the others return information
about the current state of the message queue description (mq_flags) or message
queue (mq_curmsgs).
Setting message queue attributes during queue creation
When we create a message queue with mq_open(), the following mq_attr fields deter-
mine the attributes of the queue:
z The mq_maxmsg field defines the limit on the number of messages that can be
placed on the queue using mq_send(). This value must be greater than 0.