source: rtems/doc/posix_users/message.t @ 652991d

4.104.114.84.95
Last change on this file since 652991d was 652991d, checked in by Joel Sherrill <joel.sherrill@…>, on 10/11/99 at 20:58:54

Updated to incldue descriptions and reformatted to wrap lines better.

  • Property mode set to 100644
File size: 26.0 KB
Line 
1@c
2@c  COPYRIGHT(c) 1988-1998.
3@c  On-Line Applications Research Corporation(OAR).
4@c  All rights reserved.
5@c
6@c  $Id$
7@c
8
9@chapter Message Passing Manager
10
11@section Introduction
12
13The message passing manager is the means to provide communication and
14synchronization capabilities using POSIX message queues.
15
16The directives provided by the message passing manager are:
17
18@itemize @bullet
19@item @code{mq_open} - Open a Message Queue
20@item @code{mq_close} - Close a Message Queue
21@item @code{mq_unlink} - Remove a Message Queue
22@item @code{mq_send} - Send a Message to a Message Queue
23@item @code{mq_receive} - Receive a Message from a Message Queue
24@item @code{mq_notify} - Notify Process that a Message is Available
25@item @code{mq_setattr} - Set Message Queue Attributes
26@item @code{mq_getattr} - Get Message Queue Attributes
27@end itemize
28
29@section Background
30
31@subsection Theory
32
33Message queues are named objects that operate with readers and writers. 
34In addition, a message queue is a priority queue of discrete messages. 
35POSIX message queues offer a certain, basic amount of application access
36to, and control over, the message queue geometry that can be changed.
37
38@subsection Messages
39
40A message is a variable length buffer where information can be stored to
41support communication.  The length of the message and the information
42stored in that message are user-defined and can be actual data,
43pointer(s), or empty.  There is a maximum acceptable length for a message
44that is associated with each message queue.
45
46@subsection Message Queues
47
48Message queues are named objects similar to the pipes of POSIX.  They are
49a means of communicating data between multiple processes and for passing
50messages among tasks and ISRs.  Message queues can contain a variable
51number of messages from 0 to an upper limit that is user defined.  The
52maximum length of the message can be set on a per message queue basis. 
53Normally messages are sent and received from the message queue in FIFO
54order.  However, messages can also be prioritized and a priority queue
55established for the passing of messages.  Synchronization is needed when a
56task waits for a message to arrive at a queue.  Also, a task may poll a
57queue for the arrival of a message.
58
59The message queue descriptor mqd_t mq represents the message queue.  It is
60passed as an argument to all of the message queue functions.
61
62@subsection Building a Message Queue Attribute Set
63
64The mq_attr structure is used to define the characteristics of the message
65queue.
66
67@example
68@group
69/*
70 *  Create Message Queue (mq) Structure
71 */
72
73typedef struct mq_attr@{
74  long mq_flags;    /* Message queue flags */
75  long mq_maxmsg;   /* Maximum number of messages for the queue */
76  long mq_msgsize;  /* Maximum message size */
77  long mq_curmsgs;  /* Number of messages currently queued */
78@};
79@end group
80@end example
81
82All of these attributes are set when the message queue is created using
83mq_open.  The mq_flags field is not used in the creation of a message
84queue, it is only used by mq_setattr and mq_getattr.  The structure
85mq_attr is passed as an argument to mq_setattr and mq_getattr.
86
87The mq_flags contain information affecting the behavior of the message
88queue.  The O_NONBLOCK mq_flag is the only flag that is defined.  In
89mq_setattr, the mq_flag can be set to dynamically change the blocking and
90non-blocking behavior of the message queue.  If the non-block flag is set
91then the message queue is non-blocking, and requests to send and receive
92messages do not block waiting for resources.  For a blocking message
93queue, a request to send might have to wait for an empty message queue,
94and a request to receive might have to wait for a message to arrive on the
95queue. Both mq_maxmsg and mq_msgsize affect the sizing of the message
96queue.  mq_maxmsg specifies how many messages the queue can hold at any
97one time.  mq_msgsize specifies the size of any one message on the queue. 
98If either of these limits is exceeded, an error message results.
99
100Upon return from mq_getattr, the mq_curmsgs is set according to the
101current state of the message queue.  This specifies the number of messages
102currently on the queue.
103
104@subsection Notification of a Message on the Queue
105
106Every message queue has the ability to notify one (and only one) process
107whenever the queue's state changes from empty (0 messages) to nonempty. 
108This means that the process does not have to block or constantly poll
109while it waits for a message.  By calling mq_notify, you can attach a
110notification request to a message queue.  When a message is received by an
111empty queue, if there are no processes blocked and waiting for the
112message, then the queue notifies the requesting process of a message
113arrival.  There is only one signal sent by the message queue, after that
114the notification request is de-registered and another process can attach
115its notification request.  After receipt of a notification, a process must
116re-register if it wishes to be notified again.
117
118If there is a process blocked and waiting for the message, that process
119gets the message, and notification is not sent.  It is also possible for
120another process to receive the message after the notification is sent but
121before the notified process has sent its receive request.
122
123Only one process can have a notification request attached to a message
124queue at any one time.  If another process attempts to register a
125notification request, it fails.  You can de-register for a message queue
126by passing a NULL to mq_notify, this removes any notification request
127attached to the queue.  Whenever the message queue is closed, all
128notification attachments are removed.
129
130@section Operations
131
132@subsection Opening or Creating a Message Queue
133
134If the message queue already exists, mq_open() opens it, if the message
135queue does not exist, mq_open() creates it.  When a message queue is
136created, the geometry of the message queue is contained in the attribute
137structure that is passed in as an argument.  This includes mq_msgsize that
138dictates the maximum size of a single message, and the mq_maxmsg that
139dictates the maximum number of messages the queue can hold at one time. 
140The blocking or non-blocking behavior of the queue can also specified.
141
142@subsection Closing a Message Queue
143
144The mq_close() function is used to close the connection made to a message
145queue that was made during mq_open.  The message queue itself and the
146messages on the queue are persistent and remain after the queue is closed.
147
148@subsection Removing a Message Queue
149
150The mq_unlink() function removes the named message queue.  If the message
151queue is not open when mq_unlink is called, then the queue is immediately
152eliminated.  Any messages that were on the queue are lost, and the queue
153can not be opened again.  If processes have the queue open when mq_unlink
154is called, the removal of the queue is delayed until the last process
155using the queue has finished.  However, the name of the message queue is
156removed so that no other process can open it.
157
158@subsection Sending a Message to a Message Queue
159
160The mq_send() function adds the message in priority order to the message
161queue.  Each message has an assigned a priority.  The highest priority
162message is be at the front of the queue.
163
164The maximum number of messages that a message queue may accept is
165specified at creation by the mq_maxmsg field of the attribute structure. 
166If this amount is exceeded, the behavior of the process is determined
167according to what oflag was used when the message queue was opened.  If
168the queue was opened with O_NONBLOCK flag set, the process does not block,
169and an error is returned.  If the O_NONBLOCK flag was not set, the process
170does block and wait for space on the queue.
171
172@subsection Receiving a Message from a Message Queue
173
174The mq_receive() function is used to receive the oldest of the highest
175priority message(s) from the message queue specified by mqdes.  The
176messages are received in FIFO order within the priorities.  The received
177message's priority is stored in the location referenced by the msg_prio. 
178If the msg_prio is a NULL, the priority is discarded.  The message is
179removed and stored in an area pointed to by msg_ptr whose length is of
180msg_len.  The msg_len must be at least equal to the mq_msgsize attribute
181of the message queue.
182
183The blocking behavior of the message queue is set by O_NONBLOCK at mq_open
184or by setting O_NONBLOCK in mq_flags in a call to mq_setattr.  If this is
185a blocking queue, the process does block and wait on an empty queue.  If
186this a non-blocking queue, the process does not block. Upon successful
187completion, mq_receive returns the length of the selected message in bytes
188and the message is removed from the queue.
189
190@subsection Notification of Receipt of a Message on an Empty Queue
191
192The mq_notify() function registers the calling process to be notified of
193message arrival at an empty message queue.  Every message queue has the
194ability to notify one (and only one) process whenever the queue's state
195changes from empty (0 messages) to nonempty.  This means that the process
196does not have to block or constantly poll while it waits for a message. 
197By calling mq_notify, a notification request is attached to a message
198queue.  When a message is received by an empty queue, if there are no
199processes blocked and waiting for the message, then the queue notifies the
200requesting process of a message arrival.  There is only one signal sent by
201the message queue, after that the notification request is de-registered
202and another process can attach its notification request.  After receipt of
203a notification, a process must re-register if it wishes to be notified
204again.
205
206If there is a process blocked and waiting for the message, that process
207gets the message, and notification is not sent.  Only one process can have
208a notification request attached to a message queue at any one time.  If
209another process attempts to register a notification request, it fails. 
210You can de-register for a message queue by passing a NULL to mq_notify,
211this removes any notification request attached to the queue.  Whenever the
212message queue is closed, all notification attachments are removed.
213
214@subsection Setting the Attributes of a Message Queue
215
216The mq_setattr() function is used to set attributes associated with the
217open message queue description referenced by the message queue descriptor
218specified by mqdes.  The *omqstat represents the old or previous
219attributes.  If omqstat is non-NULL, the function mq_setattr() stores, in
220the location referenced by omqstat, the previous message queue attributes
221and the current queue status.  These values are the same as would be
222returned by a call to mq_getattr() at that point.
223
224There is only one mq_attr.mq_flag that can be altered by this call.  This
225is the flag that deals with the blocking and non-blocking behavior of the
226message queue.  If the flag is set then the message queue is non-blocking,
227and requests to send or receive do not block while waiting for resources. 
228If the flag is not set, then message send and receive may involve waiting
229for an empty queue or waiting for a message to arrive.
230
231@subsection Getting the Attributes of a Message Queue
232
233The mq_getattr() function is used to get status information and attributes
234of the message queue associated with the message queue descriptor.  The
235results are returned in the mq_attr structure referenced by the mqstat
236argument.  All of these attributes are set at create time, except the
237blocking/non-blocking behavior of the message queue which can be
238dynamically set by using mq_setattr.  The attribute mq_curmsg is set to
239reflect the number of messages on the queue at the time that mq_getattr
240was called.
241
242@section Directives
243
244This section details the message passing manager's directives. A
245subsection is dedicated to each of this manager's directives and describes
246the calling sequence, related constants, usage, and status codes.
247
248@page
249@subsection mq_open - Open a Message Queue
250
251@subheading CALLING SEQUENCE:
252
253@example
254#include <mqueue.h>
255
256mqd_t mq_open(
257  const char     *name,
258  int             oflag,
259  mode_t          mode,
260  struct mq_attr *attr
261);
262@end example
263
264@subheading STATUS CODES:
265
266@code{EACCES} - Either the message queue exists and the permissions
267requested in oflags were denied, or the message does not exist and
268permission to create one is denied.
269
270@code{EEXIST} - You tried to create a message queue that already exists.
271
272@code{EINVAL} - An inappropriate name was given for the message queue, or
273the values of mq-maxmsg or mq_msgsize were less than 0.
274
275@code{ENOENT} - The message queue does not exist, and you did not specify
276to create it.
277
278@code{EINTR} - The call to mq_open was interrupted by a signal.
279
280@code{EMFILE} - The process has too many files or message queues open. 
281This is a process limit error.
282
283@code{ENFILE} - The system has run out of resources to support more open
284message queues.  This is a system error.
285
286@code{ENAMETOOLONG} - mq_name is too long.
287
288@subheading DESCRIPTION:
289
290The mq_open () function establishes the connection between a process and a
291message queue with a message queue descriptor.  If the message queue
292already exists, mq_open opens it, if the message queue does not exist,
293mq_open creates it.  Message queues can have multiple senders and
294receivers.  If mq_open is successful, the function returns a message queue
295descriptor.  Otherwise, the function returns a -1 and sets 'errno' to
296indicate the error.
297
298The name of the message queue is used as an argument.  For the best of
299portability, the name of the message queue should begin with a "/" and no
300other "/" should be in the name.  Different systems interpret the name in
301different ways.
302
303The oflags contain information on how the message is opened if the queue
304already exists.  This may be O_RDONLY for read only, O_WRONLY for write
305only, of O_RDWR, for read and write.
306
307In addition, the oflags contain information needed in the creation of a
308message queue. @code{O_NONBLOCK} - If the non-block flag is set then the
309message queue is non-blocking, and requests to send and receive messages
310do not block waiting for resources.  If the flag is not set then the
311message queue is blocking, and a request to send might have to wait for an
312empty message queue.  Similarly, a request to receive might have to wait
313for a message to arrive on the queue. @code{O_CREAT} - This call specifies
314that the call the mq_open is to create a new message queue.  In this case
315the mode and attribute arguments of the function call are utilized.  The
316message queue is created with a mode similar to the creation of a file,
317read and write permission creator, group, and others.
318
319The geometry of the message queue is contained in the attribute structure. 
320This includes mq_msgsize that dictates the maximum size of a single
321message, and the mq_maxmsg that dictates the maximum number of messages
322the queue can hold at one time.  If a NULL is used in the mq_attr
323argument, then the message queue is created with implementation defined
324defaults. @code{O_EXCL} - is always set if O_CREAT flag is set.  If the
325message queue already exists, O_EXCL causes an error message to be
326returned, otherwise, the new message queue fails and appends to the
327existing one.
328
329@subheading NOTES:
330
331The mq_open () function does not add or remove messages from the queue. 
332When a new message queue is being created, the mq_flag field of the
333attribute structure is not used.
334
335@page
336@subsection mq_close - Close a Message Queue
337
338@subheading CALLING SEQUENCE:
339
340@example
341#include <mqueue.h>
342
343int mq_close(mqd_t mqdes);
344@end example
345
346@subheading STATUS CODES:
347
348@code{EINVAL} - The descriptor does not represent a valid open message
349queue
350
351@subheading DESCRIPTION:
352
353The mq_close function removes the association between the message queue
354descriptor, mqdes, and its message queue. If mq_close() is successfully
355completed, the function returns a value of zero; otherwise, the function
356returns a value of -1 and sets errno to indicate the error.
357
358@subheading NOTES:
359
360If the process had successfully attached a notification request to the
361message queue via mq_notify, this attachment is removed, and the message
362queue is available for another process to attach for notification. 
363mq_close has no effect on the contents of the message queue, all the
364messages that were in the queue remain in the queue.
365
366@page
367@subsection mq_unlink - Remove a Message Queue
368
369@subheading CALLING SEQUENCE:
370
371@example
372#include <mqueue.h>
373
374int mq_unlink(const char *name);
375@end example
376
377@subheading STATUS CODES:
378
379@code{EINVAL} - The descriptor does not represent a valid message queue
380
381@subheading DESCRIPTION:
382
383The mq_unlink() function removes the named message queue.  If the message
384queue is not open when mq_unlink is called, then the queue is immediately
385eliminated.  Any messages that were on the queue are lost, and the queue
386can not be opened again.  If processes have the queue open when mq_unlink
387is called, the removal of the queue is delayed until the last process
388using the queue has finished.  However, the name of the message queue is
389removed so that no other process can open it.  Upon successful completion,
390the function returns a value of zero.  Otherwise, the named message queue
391is not changed by this function call, and the function returns a value of
392-1 and sets errno to indicate the error.
393
394@subheading NOTES:
395
396Calls to mq_open() to re-create the message queue may fail until the
397message queue is actually removed.  However, the mq_unlink() call need not
398block until all references have been closed; it may return immediately.
399
400@page
401@subsection mq_send - Send a Message to a Message Queue
402
403@subheading CALLING SEQUENCE:
404
405@example
406#include<mqueue.h>
407int mq_send(
408  mqd_t         mqdes,
409  const char   *msg_ptr,
410  size_t        msg_len,
411  unsigned int  msg_prio
412);
413@end example
414
415@subheading STATUS CODES:
416
417@code{EBADF} - The descriptor does not represent a valid message queue, or the queue was opened for read only O_RDONLY
418@code{EINVAL} - The value of msg_prio was greater than the MQ_PRIO_MAX.
419@code{EMSGSIZE} - The msg_len is greater than the mq_msgsize attribute of the message queue
420@code{EAGAIN} - The message queue is non-blocking, and there is no room on the queue for another message as specified by the mq_maxmsg.
421@code{EINTR} - The message queue is blocking.  While the process was waiting for free space on the queue, a signal arrived that interrupted the wait.
422
423@subheading DESCRIPTION:
424
425The mq_send() function adds the message pointed to by the argument msg_ptr
426to the message queue specified by mqdes.  Each message is assigned a
427priority , from 0 to MQ_PRIO_MAX. MQ_PRIO_MAX is defined in <limits.h> and
428must be at least 32.  Messages are added to the queue in order of their
429priority.  The highest priority message is at the front of the queue.
430
431The maximum number of messages that a message queue may accept is
432specified at creation by the mq_maxmsg field of the attribute structure. 
433If this amount is exceeded, the behavior of the process is determined
434according to what oflag was used when the message queue was opened.  If
435the queue was opened with O_NONBLOCK flag set, then the EAGAIN error is
436returned.  If the O_NONBLOCK flag was not set, the process blocks and
437waits for space on the queue, unless it is interrupted by a signal.
438
439Upon successful completion, the mq_send () function returns a value of
440zero.  Otherwise, no message is enqueued, the function returns -1, and
441errno is set to indicate the error.
442
443@subheading NOTES:
444
445If the specified message queue is not full, mq_send inserts the message at
446the position indicated by the msg_prio argument.
447
448@page
449@subsection mq_receive - Receive a Message from a Message Queue
450
451@subheading CALLING SEQUENCE:
452
453@example
454#include <mqueue.h>
455
456size_t mq_receive(
457  mqd_t         mqdes,
458  char         *msg_ptr,
459  size_t        msg_len,
460  unsigned int *msg_prio
461);
462@end example
463
464@subheading STATUS CODES:
465
466@code{EBADF} - The descriptor does not represent a valid message queue, or the queue was opened for write only O_WRONLY
467@code{EMSGSIZE} - The msg_len is less than the mq_msgsize attribute of the message queue
468@code{EAGAIN} - The message queue is non-blocking, and the queue is empty
469@code{EINTR} - The message queue is blocking.  While the process was waiting for a message to arrive on the queue, a signal arrived that interrupted the wait.
470
471@subheading DESCRIPTION:
472
473The mq_receive function is used to receive the oldest of the highest
474priority message(s) from the message queue specified by mqdes.  The
475messages are received in FIFO order within the priorities.  The received
476message's priority is stored in the location referenced by the msg_prio. 
477If the msg_prio is a NULL, the priority is discarded.  The message is
478removed and stored in an area pointed to by msg_ptr whose length is of
479msg_len.  The msg_len must be at least equal to the mq_msgsize attribute
480of the message queue.
481
482The blocking behavior of the message queue is set by O_NONBLOCK at mq_open
483or by setting O_NONBLOCK in mq_flags in a call to mq_setattr.  If this is
484a blocking queue, the process blocks and waits on an empty queue.  If this
485a non-blocking queue, the process does not block.
486
487Upon successful completion, mq_receive returns the length of the selected
488message in bytes and the message is removed from the queue.  Otherwise, no
489message is removed from the queue, the function returns a value of -1, and
490sets errno to indicate the error.
491
492@subheading NOTES:
493
494If the size of the buffer in bytes, specified by the msg_len argument, is
495less than the mq_msgsize attribute of the message queue, the function
496fails and returns an error
497
498@page
499@subsection mq_notify - Notify Process that a Message is Available
500
501@subheading CALLING SEQUENCE:
502
503@example
504#include <mqueue.h>
505
506int mq_notify(
507  mqd_t                  mqdes,
508  const struct sigevent *notification
509);
510@end example
511
512@subheading STATUS CODES:
513
514@code{EBADF} - The descriptor does not refer to a valid message queue
515@code{EBUSY} - A notification request is already attached to the queue
516
517@subheading DESCRIPTION:
518
519If the argument notification is not NULL, this function registers the
520calling process to be notified of message arrival at an empty message
521queue associated with the specified message queue descriptor, mqdes.
522
523Every message queue has the ability to notify one (and only one) process
524whenever the queue's state changes from empty (0 messages) to nonempty. 
525This means that the process does not have to block or constantly poll
526while it waits for a message.  By calling mq_notify, a notification
527request is attached to a message queue.  When a message is received by an
528empty queue, if there are no processes blocked and waiting for the
529message, then the queue notifies the requesting process of a message
530arrival.  There is only one signal sent by the message queue, after that
531the notification request is de-registered and another process can attach
532its notification request.  After receipt of a notification, a process must
533re-register if it wishes to be notified again.
534
535If there is a process blocked and waiting for the message, that process
536gets the message, and notification is not be sent.  Only one process can
537have a notification request attached to a message queue at any one time. 
538If another process attempts to register a notification request, it fails. 
539You can de-register for a message queue by passing a NULL to mq_notify;
540this removes any notification request attached to the queue.  Whenever the
541message queue is closed, all notification attachments are removed.
542
543Upon successful completion, mq_notify returns a value of zero; otherwise,
544the function returns a value of -1 and sets errno to indicate the error.
545
546@subheading NOTES:
547
548It is possible for another process to receive the message after the notification is sent but before the notified process has sent its receive request.
549
550@page
551@subsection mq_setattr - Set Message Queue Attributes
552
553@subheading CALLING SEQUENCE:
554
555@example
556#include <mqueue.h>
557
558int mq_setattr(
559  mqd_t                 mqdes,
560  const struct mq_attr *mqstat,
561  struct mq_attr       *omqstat
562);
563@end example
564
565@subheading STATUS CODES:
566
567@code{EBADF} - The message queue descriptor does not refer to a valid, open queue.
568@code{EINVAL} - The mq_flag value is invalid.
569
570@subheading DESCRIPTION:
571
572The mq_setattr function is used to set attributes associated with the open
573message queue description referenced by the message queue descriptor
574specified by mqdes.  The *omqstat represents the old or previous
575attributes.  If omqstat is non-NULL, the function mq_setattr() stores, in
576the location referenced by omqstat, the previous message queue attributes
577and the current queue status.  These values are the same as would be
578returned by a call to mq_getattr() at that point.
579
580There is only one mq_attr.mq_flag which can be altered by this call. 
581This is the flag that deals with the blocking and non-blocking behavior of
582the message queue.  If the flag is set then the message queue is
583non-blocking, and requests to send or receive do not block while waiting
584for resources.  If the flag is not set, then message send and receive may
585involve waiting for an empty queue or waiting for a message to arrive.
586
587Upon successful completion, the function returns a value of zero and the
588attributes of the message queue have been changed as specified. 
589Otherwise, the message queue attributes is unchanged, and the function
590returns a value of -1 and sets errno to indicate the error.
591
592@subheading NOTES:
593
594All other fields in the mq_attr are ignored by this call.
595
596@page
597@subsection mq_getattr - Get Message Queue Attributes
598
599@subheading CALLING SEQUENCE:
600
601@example
602#include <mqueue.h>
603int mq_getattr(mqd_t mqdes, struct mq_attr *mqstat);
604@end example
605
606@subheading STATUS CODES:
607
608@code{EBADF} - The message queue descriptor does not refer to a valid,
609open message queue.
610
611@subheading DESCRIPTION:
612
613The mqdes argument specifies a message queue descriptor.  The mq_getattr
614function is used to get status information and attributes of the message
615queue associated with the message queue descriptor.  The results are
616returned in the mq_attr structure referenced by the mqstat argument.  All
617of these attributes are set at create time, except the
618blocking/non-blocking behavior of the message queue which can be
619dynamically set by using mq_setattr.  The attribute mq_curmsg is set to
620reflect the number of messages on the queue at the time that mq_getattr
621was called.
622
623Upon successful completion, the mq_getattr function returns zero. 
624Otherwise, the function returns -1 and sets errno to indicate the error.
625
626@subheading NOTES:
627
Note: See TracBrowser for help on using the repository browser.