MQ_NOTIFY(3)               POSIX Programmer's Manual              MQ_NOTIFY(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.


NNAAMMEE
       mq_notify -- notify process that a message is available (RREEAALLTTIIMMEE)

SSYYNNOOPPSSIISS
       #include <mqueue.h>

       int mq_notify(mqd_t _m_q_d_e_s, const struct sigevent *_n_o_t_i_f_i_c_a_t_i_o_n);

DDEESSCCRRIIPPTTIIOONN
       If the argument _n_o_t_i_f_i_c_a_t_i_o_n is not NULL, this function shall register
       the calling process to be notified of message arrival at an empty
       message queue associated with the specified message queue descriptor,
       _m_q_d_e_s.  The notification specified by the _n_o_t_i_f_i_c_a_t_i_o_n argument shall
       be sent to the process when the message queue transitions from empty to
       non-empty. At any time, only one process may be registered for
       notification by a message queue. If the calling process or any other
       process has already registered for notification of message arrival at
       the specified message queue, subsequent attempts to register for that
       message queue shall fail.

       If _n_o_t_i_f_i_c_a_t_i_o_n is NULL and the process is currently registered for
       notification by the specified message queue, the existing registration
       shall be removed.

       When the notification is sent to the registered process, its
       registration shall be removed. The message queue shall then be
       available for registration.

       If a process has registered for notification of message arrival at a
       message queue and some thread is blocked in _m_q___r_e_c_e_i_v_e() or
       _m_q___t_i_m_e_d_r_e_c_e_i_v_e() waiting to receive a message when a message arrives
       at the queue, the arriving message shall satisfy the appropriate
       _m_q___r_e_c_e_i_v_e() or _m_q___t_i_m_e_d_r_e_c_e_i_v_e(), respectively. The resulting behavior
       is as if the message queue remains empty, and no notification shall be
       sent.

RREETTUURRNN VVAALLUUEE
       Upon successful completion, the _m_q___n_o_t_i_f_y() function shall return a
       value of zero; otherwise, the function shall return a value of -1 and
       set _e_r_r_n_o to indicate the error.

EERRRROORRSS
       The _m_q___n_o_t_i_f_y() function shall fail if:

       EEBBAADDFF  The _m_q_d_e_s argument is not a valid message queue descriptor.

       EEBBUUSSYY  A process is already registered for notification by the message
              queue.

       The _m_q___n_o_t_i_f_y() function may fail if:

       EEIINNVVAALL The _n_o_t_i_f_i_c_a_t_i_o_n argument is NULL and the process is currently
              not registered.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
       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.

           ##iinncclluuddee <<pptthhrreeaadd..hh>>
           ##iinncclluuddee <<mmqquueeuuee..hh>>
           ##iinncclluuddee <<aasssseerrtt..hh>>
           ##iinncclluuddee <<ssttddiioo..hh>>
           ##iinncclluuddee <<ssttddlliibb..hh>>
           ##iinncclluuddee <<uunniissttdd..hh>>

           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 maximum msg size; allocate buffer to receive msg */

               if (mq_getattr(mqdes, &attr) == -1) {
                   perror("mq_getattr");
                   exit(EXIT_FAILURE);
               }
               buf = malloc(attr.mq_msgsize);

               if (buf == NULL) {
                   perror("malloc");
                   exit(EXIT_FAILURE);
               }

               nr = mq_receive(mqdes, buf, attr.mq_msgsize, NULL);
               if (nr == -1) {
                   perror("mq_receive");
                   exit(EXIT_FAILURE);
               }

               printf("Read %ld bytes from message queue\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) {
                   perror("mq_open");
                   exit(EXIT_FAILURE);
               }

               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) {
                   perror("mq_notify");
                   exit(EXIT_FAILURE);
               }

               pause();    /* Process will be terminated by thread function */
           }

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       None.

RRAATTIIOONNAALLEE
       None.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _m_q___o_p_e_n(), _m_q___s_e_n_d(), _m_q___r_e_c_e_i_v_e(), _m_s_g_c_t_l(), _m_s_g_g_e_t(), _m_s_g_r_c_v(),
       _m_s_g_s_n_d()

       The Base Definitions volume of POSIX.1-2008, <<mmqquueeuuee..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
       -- Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, Copyright (C) 2013 by the Institute of
       Electrical and Electronics Engineers, Inc and The Open Group.  (This is
       POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
       event of any discrepancy between this version and the original IEEE and
       The Open Group Standard, the original IEEE and The Open Group Standard
       is the referee document. The original Standard can be obtained online
       at http://www.unix.org/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2013                         MQ_NOTIFY(3)
