source: rtems/doc/posix_users/message.t @ adee5979

4.104.114.84.95
Last change on this file since adee5979 was adee5979, checked in by Joel Sherrill <joel.sherrill@…>, on 05/04/00 at 19:45:17

Numerous changes based on comments from Stephan Wilms <Stephan.Wilms@…>
including a new section in the Getting Started called "Where to
Go From Here", lots of index entries added, and more configuration
table information.

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