man-pages/man3/mq_notify.3

270 lines
7.7 KiB
Groff
Raw Normal View History

2006-03-01 03:21:20 +00:00
'\" t
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
2007-09-20 06:52:22 +00:00
.\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
2006-03-01 03:21:20 +00:00
.\"
.\" 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.
.\"
2006-03-01 03:21:20 +00:00
.\" 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. The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
2006-03-01 03:21:20 +00:00
.\" 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 2009-01-27 "Linux" "Linux Programmer's Manual"
2006-03-01 03:21:20 +00:00
.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
2007-08-02 16:48:31 +00:00
.sp
Link with \fI\-lrt\fP.
2006-03-01 03:21:20 +00:00
.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
2006-03-01 03:21:20 +00:00
the empty message queue referred to by the descriptor
.IR mqdes .
The
2006-03-01 03:21:20 +00:00
.I notification
argument is a pointer to a
2006-03-01 03:21:20 +00:00
.I sigevent
structure that is defined something like the following:
.in +4n
2006-03-01 03:21:20 +00:00
.nf
union sigval { /* Data passed with notification */
int sival_int; /* Integer value */
void *sival_ptr; /* Pointer value */
2006-03-01 03:21:20 +00:00
};
struct sigevent {
int sigev_notify; /* Notification method */
int sigev_signo; /* 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 */
2006-03-01 03:21:20 +00:00
};
.fi
.in
2006-03-02 02:04:20 +00:00
.PP
If
2006-03-01 03:21:20 +00:00
.I notification
is a non-NULL pointer, then
2006-03-01 03:21:20 +00:00
.BR mq_notify ()
registers the calling process to receive message notification.
The
.I sigev_notify
2006-03-01 03:21:20 +00:00
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
2006-03-01 03:21:20 +00:00
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
2006-05-22 08:27:25 +00:00
.IR sigev_signo .
If the signal is caught with a signal handler that
2006-03-01 03:21:20 +00:00
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;
2006-03-01 03:21:20 +00:00
.I si_value
is set to the value specified in
.IR notification\->sigev_value ;
2006-03-01 03:21:20 +00:00
.\" 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.
2006-03-02 02:04:20 +00:00
The same information is available if the signal is accepted using
.BR sigwaitinfo (2).
2006-03-01 03:21:20 +00:00
.TP
.B SIGEV_THREAD
Deliver notification by invoking
.I notification\->sigev_notify_function
2006-03-01 03:21:20 +00:00
as the start function of a new thread.
The function is invoked with
.I notification\->sigev_value
2006-03-01 03:21:20 +00:00
as its sole argument.
If
.I notification\->sigev_notify_attributes
2006-03-01 03:21:20 +00:00
is not NULL, then it should point to a
.I pthread_attr_t
structure that defines attributes for the thread (see
.BR pthread_attr_init (3)).
2006-03-01 03:21:20 +00:00
.PP
Only one process can be registered to receive notification
from a message queue.
If
2006-03-01 03:21:20 +00:00
.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
2006-03-01 03:21:20 +00:00
for this queue.
Message notification only occurs when a new message arrives and
2006-03-01 03:21:20 +00:00
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
2006-03-01 03:21:20 +00:00
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 (3),
then any message notification registration is ignored:
2006-03-01 03:21:20 +00:00
the message is delivered to the process or thread calling
.BR mq_receive (3),
2006-03-01 03:21:20 +00:00
and the message notification registration remains in effect.
Notification occurs once: after a notification is delivered,
the notification registration is removed,
2006-03-01 03:21:20 +00:00
and another process can register for message notification.
If the notified process wishes to receive the next notification,
it can use
2006-03-01 03:21:20 +00:00
.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
2006-03-01 03:21:20 +00:00
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
2006-03-01 03:21:20 +00:00
set to indicate the error.
.SH ERRORS
.TP
.B EBADF
The descriptor specified in
2006-03-01 03:21:20 +00:00
.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
2006-03-01 03:21:20 +00:00
is not one of the permitted values; or
.I notification\->sigev_notify
is
.B SIGEV_SIGNAL
and
.I notification\->sigev_signo
2007-03-01 01:12:16 +00:00
is not a valid signal number.
2006-03-01 03:21:20 +00:00
.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
2006-03-01 03:21:20 +00:00
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>
2007-11-19 03:25:26 +00:00
#define handle_error(msg) \\
do { perror(msg); exit(EXIT_FAILURE); } while (0)
2006-03-01 03:21:20 +00:00
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 */
2007-06-20 21:39:45 +00:00
if (mq_getattr(mqdes, &attr) == \-1)
2007-11-19 03:25:26 +00:00
handle_error("mq_getattr");
2006-03-01 03:21:20 +00:00
buf = malloc(attr.mq_msgsize);
2007-06-20 21:39:45 +00:00
if (buf == NULL)
2007-11-19 03:25:26 +00:00
handle_error("malloc");
2006-03-01 03:21:20 +00:00
nr = mq_receive(mqdes, buf, attr.mq_msgsize, NULL);
2007-06-20 21:39:45 +00:00
if (nr == \-1)
2007-11-19 03:25:26 +00:00
handle_error("mq_receive");
2006-03-01 03:21:20 +00:00
2006-12-16 06:12:03 +00:00
printf("Read %ld bytes from MQ\\n", (long) nr);
2006-03-01 03:21:20 +00:00
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);
2007-06-20 21:39:45 +00:00
if (mqdes == (mqd_t) \-1)
2007-11-19 03:25:26 +00:00
handle_error("mq_open");
2006-03-01 03:21:20 +00:00
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. */
2007-06-20 21:39:45 +00:00
if (mq_notify(mqdes, &not) == \-1)
2007-11-19 03:25:26 +00:00
handle_error("mq_notify");
2006-03-01 03:21:20 +00:00
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)