New pages for POSIX message queues API

This commit is contained in:
Michael Kerrisk 2006-03-01 03:21:20 +00:00
parent b8798cc43f
commit 80a99f39e6
8 changed files with 1290 additions and 0 deletions

67
man3/mq_close.3 Normal file
View File

@ -0,0 +1,67 @@
'\" t
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" Copyright (C) 2006 Michael Kerrisk <mtk-manpages@gmx.net>
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.TH MQ_CLOSE 3 2006-02-25 "Linux 2.6.16" "Linux Programmer's Manual"
.SH NAME
mq_close \- close a message queue descriptor
.SH SYNOPSIS
.nf
.B #include <mqueue.h>
.sp
.BI "mqd_t mq_close(mqd_t " mqdes );
.fi
.SH DESCRIPTION
.BR mq_close ()
closes the message queue descriptor
.IR mqdes .
If the calling process has attached a notification request
to this message queue via
.IR mqdes ,
then this request is removed,
and another process can now attach a notification request.
.SH NOTES
All open message queues are automatically closed on process termination,
or upon
.BR execve (2).
.SH RETURN VALUE
On success
.BR mq_close ()
returns 0; on error, \-1 is returned, with
.I errno
set to indicate the error.
.SH ERRORS
.TP
.B EBADF
The descriptor specified in
.I mqdes
is invalid.
.SH CONFORMING TO
POSIX.1-2001.
.SH "SEE ALSO"
.BR mq_getattr (3),
.BR mq_notify (3),
.BR mq_open (3),
.BR mq_receive (3),
.BR mq_send (3),
.BR mq_unlink (3),
.BR mq_overview (7)

140
man3/mq_getattr.3 Normal file
View File

@ -0,0 +1,140 @@
'\" t
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" Copyright (C) 2006 Michael Kerrisk <mtk-manpages@gmx.net>
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.TH MQ_GETATTR 3 2006-02-25 "Linux 2.6.16" "Linux Programmer's Manual"
.SH NAME
mq_getattr, mq_setattr \- get/set message queue attributes
.SH SYNOPSIS
.nf
.B #include <mqueue.h>
.sp
.BI "mqd_t mq_getattr(mqd_t " mqdes ", struct mq_attr *" attr );
.BI "mqd_t mq_setattr(mqd_t " mqdes ", struct mq_attr *" newattr ","
.BI " struct mq_attr *" oldattr );
.fi
.SH DESCRIPTION
.BR mq_getattr ()
and
.BR mq_setattr ()
respectively retrieve and modify attributes of the message queue
referred to by the descriptor
.IR mqdes .
.BR mq_getattr ()
returns an
.I mq_attr
structure in the buffer pointed by
.IR attr.
This structure is defined as:
.in +0.25i
.nf
struct mq_attr {
long mq_flags; /* Flags: 0 or O_NONBLOCK */
long mq_maxmsg; /* Max. # of messages on queue */
long mq_msgsize; /* Max. message size (bytes) */
long mq_curmsgs; /* # of messages currently in queue */
};
.fi
.in -0.25i
.PP
The
.I mq_flags
field contains flags associated with the open message queue description.
This field is initialised when the queue is created by
.BR mq_open ().
The only flag that can appear in this field is
.BR O_NONBLOCK .
The
.I mq_maxmsg
and
.I mq_msgsize
fields are set when the message queue is created by
.BR mq_open ().
The
.I mq_maxmsg
field is an upper limit on the number of messages
that may be placed on the queue using
.BR mq_send ().
The
.I mq_msgsize
field is an upper limit on the size of messages
that may be placed on the queue.
Both of these fields must have a value greater than zero.
Two
.I /proc
files that place ceilings on the values for these fields are described in
.BR mq_open (3).
The
.I mq_curmsgs
field returns the number of messages currently held in the queue.
.BR mq_setattr ()
sets message queue attributes using information supplied in the
.I mq_attr
structure pointed to by
.IR newattr .
The only attribute that can be modified is the setting of the
.B O_NONBLOCK
flag in
.IR mq_flags .
The other fields in
.I newattr
are ignored.
If the
.I oldattr
field is not NULL,
then the buffer that it points to is used to return an
.I mq_attr
structure that contains the same information that is returned by
.BR mq_getattr ().
.SH RETURN VALUE
On success
.BR mq_getattr ()
and
.BR mq_setattr ()
return 0; on error, \-1 is returned, with
.I errno
set to indicate the error.
.SH ERRORS
.TP
.B EBADF
The descriptor specified in
.I mqdes
is invalid.
.TP
.B EINVAL
.I newattr->mq_flags
contained set bits other than
.BR O_NONBLOCK .
.SH CONFORMING TO
POSIX.1-2001.
.SH "SEE ALSO"
.BR mq_close (3),
.BR mq_notify (3),
.BR mq_open (3),
.BR mq_receive (3),
.BR mq_send (3),
.BR mq_unlink (3),
.BR mq_overview (7)

256
man3/mq_notify.3 Normal file
View File

@ -0,0 +1,256 @@
'\" t
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" Copyright (C) 2006 Michael Kerrisk <mtk-manpages@gmx.net>
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.TH MQ_NOTIFY 3 2006-02-25 "Linux 2.6.16" "Linux Programmer's Manual"
.SH NAME
mq_notify \- register for notification when a message is available
.SH SYNOPSIS
.nf
.B #include <mqueue.h>
.sp
.BI "mqd_t mq_notify(mqd_t " mqdes ", const struct sigevent *" notification );
.fi
.SH DESCRIPTION
.BR mq_notify ()
allows the calling process to register or unregister for delivery of
an asynchronous notification when a new message arrives on
the empty message queue referred to by the descriptor
.IR mqdes .
The
.I notification
argument is a pointer to a
.I sigevent
structure that is defined something like the following:
.in +0.25i
.nf
union sigval { /* Data passed with notification */
int sival_int; /* Integer value */
void *sival_ptr; /* Pointer value */
};
struct sigevent {
int sigev_notify; /* Notification method */
int sigev_signal; /* Notification signal */
union sigval sigev_value; /* Data passed with notification */
void (*sigev_notify_function) (union sigval);
/* Function for thread notification */
void *sigev_notify_attributes;
/* Thread function attributes */
};
.fi
.in -0.25i
If
.I notification
is a non-NULL pointer, then
.BR mq_notify ()
registers the calling process to receive message notification.
The
.I sigev_notify
field of the
.I sigevent
to which
.I notification
points specifies how notification is to be performed.
This field has one of the following values:
.TP
.B SIGEV_NONE
A "null" notification: the calling process is registered as the target
for notification, but when a message arrives, no notification is sent.
.\" When is SIGEV_NONE useful?
.TP
.B SIGEV_SIGNAL
Notify the process by sending the signal specified in
.IR sigev_signal .
If the signal is caught with a signal handler that
was registered using the
.BR sigaction (2)
.B SA_SIGINFO
flag, then the following fields are set in the
.I siginfo_t
structure that is passed as the second argument of the handler:
.I si_code
is set to
.BR SI_MESGQ ;
.I si_signo
is set to the signal number;
.I si_value
is set to the value specified in
.IR notification->sigev_value ;
.\" I don't know of other implementations that set
.\" si_pid and si_uid -- MTK
.I si_pid
is set to the PID of the process that sent the message; and
.I si_uid
is set to the real user ID of the sending process.
.TP
.B SIGEV_THREAD
Deliver notification by invoking
.I notification->sigev_thread_function
as the start function of a new thread.
The function is invoked with
.IR notification->sigev_value
as its sole argument.
If
.IR notification->sigev_notify_attributes
is not NULL, then it should point to a
.I pthread_attr_t
structure that defines attributes for the thread.
.\" FIXME add a cross reference to pthread_attr_init(3), when
.\" that page is one day written.
.PP
Only one process can be registered to receive notification
from a message queue.
If
.I notification
is NULL, and the calling process is currently registered to receive
notifications for this message queue, then the registration is removed;
another process can then register to receive a message notification
for this queue.
Message notification only occurs when a new message arrives and
the queue was previously empty.
If the queue was not empty at the time
.BR mq_notify ()
was called, then a notification will only occur after
the queue is emptied and a new message arrives.
If another process or thread is waiting to read a message
from an empty queue using
.BR mq_receive (),
then any message notification registration is ignored:
the message is delivered to the process or thread calling
.BR mq_receive (),
and the message notification registration remains in effect.
Notification occurs once: after a notification is delivered,
the notification registration is removed,
and another process can register for message notification.
If the notified process wishes to receive the next notification,
it can use
.BR mq_notify ()
to request a further notification.
This should be done before emptying all unread messages from the queue.
(Placing the queue in non-blocking mode is useful for emptying
the queue of messages without blocking once it is empty.)
.SH RETURN VALUE
On success
.BR mq_notify ()
returns 0; on error, \-1 is returned, with
.I errno
set to indicate the error.
.SH ERRORS
.TP
.B EBADF
The descriptor specified in
.I mqdes
is invalid.
.TP
.B EBUSY
Another process has already registered to receive notification
for this message queue.
.TP
.B EINVAL
.I notification->sigev_notify
is not one of the permitted values; or
.I notification->sigev_notify
is
.B SIGEV_SIGNAL
and
.I notification->sigev_signo
is not a a valid signal number.
.TP
.B ENOMEM
Insufficient memory.
.SH CONFORMING TO
POSIX.1-2001.
.SH EXAMPLE
The following program registers a notification request for the
message queue named in its command-line argument.
Notification is performed by creating a thread.
The thread executes a function which reads one message from the
queue and then terminates the process.
.nf
#include <pthread.h>
#include <mqueue.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define die(msg) { perror(msg); exit(EXIT_FAILURE); }
static void /* Thread start function */
tfunc(union sigval sv)
{
struct mq_attr attr;
ssize_t nr;
void *buf;
mqd_t mqdes = *((mqd_t *) sv.sival_ptr);
/* Determine max. msg size; allocate buffer to receive msg */
if (mq_getattr(mqdes, &attr) == -1) die("mq_getattr");
buf = malloc(attr.mq_msgsize);
if (buf == NULL) die("malloc");
nr = mq_receive(mqdes, buf, attr.mq_msgsize, NULL);
if (nr == -1) die("mq_receive");
printf("Read %ld bytes from MQ\n", (long) nr);
free(buf);
exit(EXIT_SUCCESS); /* Terminate the process */
}
int
main(int argc, char *argv[])
{
mqd_t mqdes;
struct sigevent not;
assert(argc == 2);
mqdes = mq_open(argv[1], O_RDONLY);
if (mqdes == (mqd_t) -1) die("mq_open");
not.sigev_notify = SIGEV_THREAD;
not.sigev_notify_function = tfunc;
not.sigev_notify_attributes = NULL;
not.sigev_value.sival_ptr = &mqdes; /* Arg. to thread func. */
if (mq_notify(mqdes, &not) == -1) die("mq_notify");
pause(); /* Process will be terminated by thread function */
}
.fi
.SH "SEE ALSO"
.BR mq_close (3),
.BR mq_getattr (3),
.BR mq_open (3),
.BR mq_receive (3),
.BR mq_send (3),
.BR mq_unlink (3),
.BR mq_overview (7)

214
man3/mq_open.3 Normal file
View File

@ -0,0 +1,214 @@
'\" t
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" Copyright (C) 2006 Michael Kerrisk <mtk-manpages@gmx.net>
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.TH MQ_OPEN 3 2006-02-25 "Linux 2.6.16" "Linux Programmer's Manual"
.SH NAME
mq_open \- open a message queue
.SH SYNOPSIS
.nf
.B #include <mqueue.h>
.sp
.BI "mqd_t mq_open(const char *" name ", int " oflag );
.BI "mqd_t mq_open(const char *" name ", int " oflag ", mode_t " mode ", mq_attr " attr );
.fi
.SH DESCRIPTION
.BR mq_open ()
creates a new POSIX message queue or opens an existing queue.
The queue is identified by
.IR name .
For details of the construction of
.IR name ,
see
.BR mq_overview (7).
The
.I oflag
argument specifies flags that control the operation of the call.
Exactly one of the following must be specified in
.IR oflag:
.TP
.B O_RDONLY
Open the queue to receive messages only.
.TP
.B O_WRONLY
Open the queue to send messages only.
.TP
.B O_RDWR
Open the queue to both send and receive messages.
.PP
Zero or more of the following flags can additionally be
.IR OR ed
in
.IR oflag :
.TP
.B O_NONBLOCK
Open the queue in non-blocking mode.
In circumstances where
.BR mq_receive ()
and
.BR mq_send ()
would normally block, these functions instead fail with the error
.BR EAGAIN .
.TP
.B O_CREAT
Create the message queue if it does not exist.
The owner (user ID) of the message queue is set to the effective
user ID of the calling process.
The group ownership (group ID) is set to the effective group ID
of the calling process.
.\" In reality the file system IDs are used on Linux.
.TP
.B O_EXCL
If
.B O_CREAT
was specified in
.IR oflag ,
and a queue with the given
.I name
already exists, then fail with the error
.BR EEXIST .
.PP
If
.B O_CREAT
is specified in
.IR oflag ,
then two additional arguments must be supplied.
The
.I mode
argument specifies the permissions to be placed on the new queue,
as for
.BR open (2).
The permissions settings are masked against the process umask.
The
.I attr
argument specifies attributes for the queue. See
.BR mq_getattr (2)
for details.
If
.I attr
is NULL, then the queue is created with implementation-defined
default attributes.
.SH RETURN VALUE
On success,
.BR mq_open ()
returns a message queue descriptor for use by other
message queue functions.
On error,
.BR mq_open ()
returns
.IR "(mqd_t)\ \-1",
with
.I errno
set to indicate the error.
.SH ERRORS
.TP
.B EACCESS
The queue exists, but the caller does not have permission to
open it in the specified mode.
.TP
.B EINVAL
.B O_CREAT
was specified in
.IR oflag ,
and
.I attr
was not NULL, but
.I attr->mq_maxmsg
or
.I attr->mq_msqsize
was invalid.
Both of these fields must be greater than zero.
In a process that is unprivileged (does not have the
.B CAP_SYS_RESOURCE
capability),
.I attr->mq_maxmsg
must be less than or equal to the
.I msg_max
limit, and
.I attr->mq_msgsize
must be less than or equal to the
.I msgsize_max
limit.
In addition, even in a privileged process,
.I attr->mq_maxmsg
cannot exceed the
.B HARD_MAX
limit.
(See
.BR mq_overview (7)
for details of these limits.)
.TP
.B EEXIST
Both
.B O_CREAT
and
.B O_EXCL
were specified in
.IR oflag ,
but a queue with this
.I name
already exists.
.TP
.B EMFILE
The process already has the maximum number of files and
message queues open.
.TP
.B ENAMETOOLONG
.IR name
was too long.
.TP
.B ENFILE
The system limit on the total number of open files and message queues
has been reached.
.TP
.B ENOENT
The
.B O_CREAT
flag was not specified in
.IR oflag ,
and no queue with this
.I name
exists.
.TP
.B ENOMEM
Insufficient memory.
.TP
.B ENOSPC
Insufficient space for the creation of a new message queue.
This probably occurred because the
.I queues_max
limit was encountered; see
.BR mq_overview (7).
.SH CONFORMING TO
POSIX.1-2001.
.SH BUGS
In kernels before 2.6.14,
the process umask was not applied to the permissions specified in
.IR mode .
.SH "SEE ALSO"
.BR mq_close (3),
.BR mq_getattr (3),
.BR mq_notify (3),
.BR mq_receive (3),
.BR mq_send (3),
.BR mq_unlink (3),
.BR mq_overview (7)

148
man3/mq_receive.3 Normal file
View File

@ -0,0 +1,148 @@
'\" t
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" Copyright (C) 2006 Michael Kerrisk <mtk-manpages@gmx.net>
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.TH MQ_RECEIVE 3 2006-02-25 "Linux 2.6.16" "Linux Programmer's Manual"
.SH NAME
mq_receive, mq_timedreceive \- receive a message from a message queue
.SH SYNOPSIS
.nf
.B #include <mqueue.h>
.sp
.BI "mqd_t mq_receive(mqd_t " mqdes ", char *" msg_ptr ,
.BI " size_t " msg_len ", unsigned *" msg_prio );
.sp
.B #define _XOPEN_SOURCE 600
.B #include <time.h>
.B #include <mqueue.h>
.sp
.BI "mqd_t mq_timedreceive(mqd_t " mqdes ", char *" msg_ptr ,
.BI " size_t " msg_len ", unsigned *" msg_prio ,
.BI " const struct timespec *" abs_timeout );
.fi
.SH DESCRIPTION
.BR mq_receive ()
removes the oldest message with the highest priority from
the message queue referred to by the descriptor
.IR mqdes ,
and places it in the buffer pointed to by
.IR msg_ptr .
The
.I msg_len
argument specifies the size of the buffer pointed to by
.IR msg_ptr ;
this must be greater than the
.I mq_msgsize
attribute of the queue (see
.BR mq_getattr (3)).
If
.I prio
is not NULL, then the buffer to which it points is used
to return the priority associated with the received message.
If the queue is empty, then, by default,
.BR mq_receive ()
blocks until a message becomes available,
or the call is interrupted by a signal handler.
If the
.B O_NONBLOCK
flag is enabled for the message queue description,
then the call instead fails immediately with the error
.BR EAGAIN .
.BR mq_timedreceive ()
behaves just like
.BR mq_receive (),
except that if the queue is empty and the
.B O_NONBLOCK
flag is not enabled for the message queue description, then
.I abs_timeout
points to a structure which specifies a ceiling on the time for which
the call will block.
This ceiling is an absolute timeout in seconds and nanoseconds
since the Epoch (midnight on the morning of 1 January 1970),
specified in the following structure:
.sp
.RS
.nf
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
.fi
.RE
If no message is available,
and the timeout has already expired by the time of the call,
.BR mq_timedreceive ()
returns immediately.
.SH RETURN VALUE
On success,
.BR mq_receive ()
and
.BR mq_timedreceive ()
return the number of bytes in the received message;
on error, \-1 is returned, with
.I errno
set to indicate the error.
.SH ERRORS
.TP
.B EAGAIN
The queue was empty, and the
.B O_NONBLOCK
flag was set for the message queue description referred to by
.IR mqdes .
.TP
.B EBADF
The descriptor specified in
.I mqdes
was invalid.
.TP
.B EMSGSIZE
.IR msg_len
was less than the
.I mq_msgsize
attribute of the message queue.
.TP
.B EINTR
The call was interrupted by a signal handler.
.TP
.B EINVAL
The call would have blocked, and
.I abs_timeout
was invalid, either because
.I tv_sec
was less than zero, or because
.I tv_nsec
was less than zero or greater than 1000 million.
.TP
.B ETIMEDOUT
The call timed out before a message could be transferred.
.SH CONFORMING TO
POSIX.1-2001.
.SH "SEE ALSO"
.BR mq_close (3),
.BR mq_getattr (3),
.BR mq_notify (3),
.BR mq_open (3),
.BR mq_send (3),
.BR mq_unlink (3),
.BR mq_overview (7)

153
man3/mq_send.3 Normal file
View File

@ -0,0 +1,153 @@
'\" t
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" Copyright (C) 2006 Michael Kerrisk <mtk-manpages@gmx.net>
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.TH MQ_SEND 3 2006-02-25 "Linux 2.6.16" "Linux Programmer's Manual"
.SH NAME
mq_send, mq_timedsend \- send a message to a message queue
.SH SYNOPSIS
.nf
.B #include <mqueue.h>
.sp
.BI "mqd_t mq_send(mqd_t " mqdes ", const char *" msg_ptr ,
.BI " size_t " msg_len ", unsigned " msg_prio );
.sp
.B #define _XOPEN_SOURCE 600
.B #include <time.h>
.B #include <mqueue.h>
.sp
.BI "mqd_t mq_timedsend(mqd_t " mqdes ", const char *" msg_ptr ,
.BI " size_t " msg_len ", unsigned " msg_prio ,
.BI " const struct timespec *" abs_timeout );
.fi
.SH DESCRIPTION
.BR mq_send ()
adds the message pointed to by
.IR msg_ptr
to the message queue referred to by the descriptor
.IR mqdes .
The
.I msg_len
argument specifies the length of the message pointed to by
.IR msg_ptr ;
this length must be less than or equal to the queue's
.I mq_msgsize
attribute.
Zero-length messages are allowed.
The
.I msg_prio
argument is a non-negative integer that specifies the priority
of this message.
Messages are placed on the queue in decreasing order of priority,
with newer messages of the same priority being placed after
older messages with the same priority.
If the message queue is already full
(i.e., the number of messages on the queue equals the queue's
.I mq_maxmsg
attribute), then, by default,
.BR mq_send ()
blocks until sufficient space becomes available to allow the message
to be queued, or until the call is interrupted by a signal handler.
If the
.B O_NONBLOCK
flag is enabled for the message queue description,
then the call instead fails immediately with the error
.BR EAGAIN .
.BR mq_timedsend ()
behaves just like
.BR mq_send (),
except that if the queue is full and the
.B O_NONBLOCK
flag is not enabled for the message queue description, then
.I abs_timeout
points to a structure which specifies a ceiling on the time for which
the call will block.
This ceiling is an absolute timeout in seconds and nanoseconds
since the Epoch (midnight on the morning of 1 January 1970),
specified in the following structure:
.sp
.RS
.nf
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
.fi
.RE
If the message queue is full,
and the timeout has already expired by the time of the call,
.BR mq_timedsend ()
returns immediately.
.SH RETURN VALUE
On success,
.BR mq_send ()
and
.BR mq_timedsend ()
return zero; on error, \-1 is returned, with
.I errno
set to indicate the error.
.SH ERRORS
.TP
.B EAGAIN
The queue was empty, and the
.B O_NONBLOCK
flag was set for the message queue description referred to by
.IR mqdes .
.TP
.B EBADF
The descriptor specified in
.I mqdes
was invalid.
.TP
.B EMSGSIZE
.IR msg_len
was greater than the
.I mq_msgsize
attribute of the message queue.
.TP
.B EINTR
The call was interrupted by a signal handler.
.TP
.B EINVAL
The call would have blocked, and
.I abs_timeout
was invalid, either because
.I tv_sec
was less than zero, or because
.I tv_nsec
was less than zero or greater than 1000 million.
.TP
.B ETIMEDOUT
The call timed out before a message could be transferred.
.SH CONFORMING TO
POSIX.1-2001.
.SH "SEE ALSO"
.BR mq_close (3),
.BR mq_getattr (3),
.BR mq_notify (3),
.BR mq_open (3),
.BR mq_receive (3),
.BR mq_unlink (3),
.BR mq_overview (7)

66
man3/mq_unlink.3 Normal file
View File

@ -0,0 +1,66 @@
'\" t
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" Copyright (C) 2006 Michael Kerrisk <mtk-manpages@gmx.net>
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.TH MQ_UNLINK 3 2006-02-25 "Linux 2.6.16" "Linux Programmer's Manual"
.SH NAME
mq_unlink \- remove a message queue
.SH SYNOPSIS
.nf
.B #include <mqueue.h>
.sp
.BI "mqd_t mq_unlink(const char *" name );
.fi
.SH DESCRIPTION
.BR mq_unlink ()
removes the specified message queue
.IR name .
The message queue name is removed immediately.
The queue itself is destroyed once any other processes that have
the queue open close their descriptors referring to the queue.
.SH RETURN VALUE
On success
.BR mq_unlink ()
returns 0; on error, \-1 is returned, with
.I errno
set to indicate the error.
.SH ERRORS
.TP
.B EACCES
The caller does not have permission to unlink this message queue.
.TP
.B ENAMETOOLONG
.IR name
was too long.
.TP
.B ENOENT
There is no message queue with the given
.IR name .
.SH CONFORMING TO
POSIX.1-2001.
.SH "SEE ALSO"
.BR mq_close (3),
.BR mq_getattr (3),
.BR mq_notify (3),
.BR mq_open (3),
.BR mq_receive (3),
.BR mq_send (3),
.BR mq_overview (7)

246
man7/mq_overview.7 Normal file
View File

@ -0,0 +1,246 @@
'\" t
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" Copyright (C) 2006 Michael Kerrisk <mtk-manpages@gmx.net>
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.TH MQ_OVERVIEW 7 2006-02-25 "Linux 2.6.16" "Linux Programmer's Manual"
.SH NAME
mq_overview \- Overview of POSIX message queues
.SH DESCRIPTION
POSIX message queues allow processes to exchange data in
the form of messages.
Message queues are created and opened using
.BR mq_open (3);
this function returns a
.I message queue descriptor
.RI ( mqd_t ),
which is used to refer to the open message queue in later calls.
Each message queue is identified by a
.I name
of the form
.IR /somename .
Two processes can operate on the same queue by passing the same name to
.BR mq_open ().
Messages are transferred to and from a queue using
.BR mq_send (3)
and
.BR mq_receive (3).
When a process has finished using the queue, it closes it using
.BR mq_close (3),
and when the queue is no longer required, it can be deleted using
.BR mq_unlink (3).
Queue attributes can be retrieved and (in some cases) modified using
.BR mq_getattr (3)
and
.BR mq_setattr (3).
A process can request asynchronous notification
of the arrival of a message on a previously empty queue using
.BR mq_notify (3).
A message queue descriptor is a reference to an
.IR "open message queue description"
(cf.
.BR open (2)).
After a
.BR fork (2),
a child inherits copies of its parent's message queue descriptors,
and these descriptors refer to the same open message queue descriptions
as the corresponding descriptors in the parent.
Corresponding descriptors in the two processes share the flags
.RI ( mq_flags )
that are associated with the open message queue description.
Each message has an associated
.IR priority ,
and messages are always delivered to the receiving process
highest priority first.
Message priorities range from 0 (low) to
.I sysconf(_SC_MQ_PRIO_MAX)\ -\ 1
(high).
On Linux,
.I sysconf(_SC_MQ_PRIO_MAX)
returns 32768, but POSIX.1-2001 only requires
an implementation to support priorities in the range 0 to 31;
some implementations only provide this range.
.SH LINUX SPECIFIC DETAILS
.SS Versions
POSIX message queues have been supported on Linux since kernel 2.6.6.
Glibc support has been provided since version 2.3.4.
.SS Kernel configuration
Support for POSIX message queues is configurable via the
.B CONFIG_POSIX_MQUEUE
kernel configuration option.
This option is enabled by default.
.SS Persistence
POSIX message queues have kernel persistence:
if not removed by
.BR mq_unlink (),
a message queue will exist until the system is shut down.
.SS Linking
Programs using the POSIX message queue API must be compiled with
.I cc \-lrt
to link against the real-time library,
.IR librt .
.SS /proc interfaces
The following interfaces can be used to limit the amount of
kernel memory consumed by POSIX message queues:
.TP
.I /proc/sys/fs/mqueue/msg_max
This file can be used to view and change the ceiling value for the
maximum number of messages in a queue.
This value acts as a ceiling on the
.I attr->mq_maxmsg
argument given to
.BR mq_open (3).
The default and minimum value for
.I msg_max
is 10; the upper limit is HARD_MAX:
.IR "(131072\ /\ sizeof(void\ *))"
(32768 on Linux/86).
This limit is ignored for privileged processes
.RB ( CAP_SYS_RESOURCE ),
but the HARD_MAX ceiling is nevertheless imposed.
.TP
.I /proc/sys/fs/mqueue/msgsize_max
This file can be used to view and change the ceiling on the
maximum message size.
This value acts as a ceiling on the
.I attr->mq_msgsize
argument given to
.BR mq_open (3).
The default and minimum value for
.I msgsize_max
is 8192 bytes; the upper limit is INT_MAX
(2147483647 on Linux/86).
This limit is ignored for privileged processes
.RB ( CAP_SYS_RESOURCE ).
.TP
.I /proc/sys/fs/mqueue/queues_max
This file can be used to view and change the system-wide limit on the
number of message queues that can be created.
Only privileged processes
.RB ( CAP_SYS_RESOURCE )
can create new message queues once this limit has been reached.
The default value for
.I queues_max
is 256; it can be changed to any value in the range 0 to INT_MAX.
.SS Resource limit
The
.BR RLIMIT_MSGQUEUE
resource limit, which places a limit on the amount of space
that can be consumed by all of the message queues
belonging to a process's real user ID, is described in
.BR getrlimit (2).
.SS Mounting the message queue file system
On Linux, message queues are created in a virtual file system.
(Other implementations may also provide such a feature,
but the details are likely to differ.)
This file system can be mounted using the following commands:
.in +0.25i
.nf
$ mkdir /dev/mqueue
$ mount -t mqueue none /dev/mqueue
.fi
.in -0.25i
The sticky bit is automatically enabled on the mount directory.
After the file system has been mounted, the message queues on the system
can be viewed and manipulated using the commands usually used for files
(e.g.,
.BR ls (1)
and
.BR rm (1)).
The contents of each file in the directory consist of a single line
containing information about the queue:
.in +0.25i
.nf
$ ls /dev/mqueue/mymq
QSIZE:129 NOTIFY:2 SIGNO:0 NOTIFY_PID:8260
$ mount -t mqueue none /dev/mqueue
.fi
.in -0.25i
These fields are as follows:
.TP
.B
QSIZE
Number of bytes of data in all messages in the queue.
.TP
.B NOTIFY_PID
If this is non-zero, then the process with this PID has used
.BR mq_notify (3)
to register for asynchronous message notification,
and the remaining fields describe how notification occurs.
.TP
.B NOTIFY
Notification method:
0 is
.BR SIGEV_SIGNAL ;
1 is
.BR SIGEV_NONE;
and
2 is
.BR SIGEV_THREAD .
.TP
.B SIGNO
Signal number to be used for
.BR SIGEV_SIGNAL .
.SS Polling message queue descriptors
On Linux, a message queue descriptor is actually a file descriptor,
and can be monitored using
.BR select (2),
.BR poll (2),
or
.BR epoll (4).
This is not portable.
.SH "CONFORMING TO"
POSIX.1-2001.
.SH NOTES
System V message queues
.RB ( msgget (2),
.BR msgsnd (2),
.BR msgrcv (2),
etc.) are an older API for exchanging messages between processes.
POSIX message queues provide a better designed interface than
System V message queues;
on the other hand POSIX message queues are less widely available
(especially on older systems) than System V message queues.
.SH EXAMPLE
An example of the use of various message queue functions is shown in
.BR mq_notify (3).
.SH "SEE ALSO"
.BR getrlimit (2),
.BR mq_close (3),
.BR mq_getattr (3),
.BR mq_notify (3),
.BR mq_open (3),
.BR mq_receive (3),
.BR mq_send (3),
.BR mq_unlink (3),
.BR poll (2),
.BR select (2),
.BR epoll (4)
.\" FIXME add SEE ALSO refs from System V message queue pages to
.\" POSIX message queue pages.