Skip to main content

Mqueue

More...

Functions#

Name
intmq_getattr(mqd_t mqdes, struct mq_attr * mqstat)
intmq_setattr(mqd_t mqdes, const struct mq_attr mqstat, struct mq_attr omqstat)
mqd_tmq_open(const char * name, int oflag, ... ...)
intmq_close(mqd_t mqdes)
intmq_unlink(const char * name)
voidmq_discard(mqd_t mqdes)
voidmq_flush(mqd_t mqdes)
intmq_notify(mqd_t mqdes, const struct sigevent * notification)
ssize_tmq_receive(mqd_t mqdes, char msg_ptr, size_t msg_len, unsigned msg_prio)
ssize_tmq_timedreceive(mqd_t mqdes, char msg_ptr, size_t msg_len, unsigned msg_prio, const struct timespec * abs_timeout)
intmq_send(mqd_t mqdes, const char * msg_ptr, size_t msg_len, unsigned msg_prio)
intmq_timedsend(mqd_t mqdes, const char msg_ptr, size_t msg_len, unsigned msg_prio, const struct timespec abs_timeout)
ssize_tmq_tryreceive(mqd_t mqdes, char msg_ptr, size_t msg_len, unsigned msg_prio)
intmq_trysend(mqd_t mqdes, const char * msg_ptr, size_t msg_len, unsigned msg_prio)

Detailed Description#

This interface allows processes to share data by sending and receiving messages.

Here is an example of how to create a new message queue and put a message in the new queue:

//md2code:include#include <mqueue.h>#include <stdio.h>
//md2code:main  mqd_t mdes;  struct mq_attr attr;  char msg[32];
  attr.mq_maxmsg = 4;  attr.mq_msgsize = 32;
  mdes = mq_open("/path/to/queue", O_CREAT|O_EXCL|O_RDWR, 0666, &attr);  if ( mdes == (mqd_t)-1 ){     perror("failed to create queue");     return;  }
  //now send a message in the queue  strcpy(msg, "this is the message");  if ( mq_send(mdes, msg, strlen(msg), 0) < 0 ){     perror("failed to send message");  }

Another process can read the message in the queue my using the following code:

//md2code:main  mqd_t mdes;  char msg[32];  unsigned msg_prio;
  mdes = mq_open("/path/to/queue", O_RDWR);  if ( mdes == (mqd_t)-1 ){     perror("failed to create queue");     return;  }
  //now send a message in the queue  if ( mq_receive(mdes, msg, strlen(msg), &msg_prio) < 0 ){     perror("failed to send message");  }
  printf("received: %s\n", msg);

Functions Documentation#

mq_getattr#

int mq_getattr(    mqd_t mqdes,    struct mq_attr * mqstat)

Return: Zero on success or -1 with errno (see Errno) set to:

  • EBADF: mqdes is not a valid message queue descriptor

This function gets the message queue attributes and stores them at mqstat.

mq_setattr#

int mq_setattr(    mqd_t mqdes,    const struct mq_attr * mqstat,    struct mq_attr * omqstat)

Return: Zero on success or -1 with errno (see Errno) set to:

  • ENOTSUP

Sets the message queue attributes

mq_open#

mqd_t mq_open(    const char * name,    int oflag,    ... ...)

Parameters:

  • name The name of the message queue
  • oflag The flags to use when opening (O_CREAT, O_EXCL, O_RDWR, O_RDONLY)

Return: Zero on success or -1 with errno (see Errno) set to:

  • ENAMETOOLONG: name length is greater than NAME_MAX
  • EEXIST: OCREAT and O_EXCL are set in _oflag but the queue already exists
  • ENOENT: OCREAT is not set in _oflag and the queue does not exist
  • ENOMEM: not enough memory for the queue
  • EACCES: permission to create name queue is denied
  • EINVAL: OCREAT is set and _attr is not null but mq_maxmsg or mq_msgsize is less than or equal to zero

This function opens or creates a new message queue.

If using O_CREAT, the following prototype is used:

mqd_t mq_open(const char * name, int oflag, mode_t mode, const struct mq_attr * attr);

If the name starts with user, the message queue will only be available to the calling process. All other message queues can be shared among processes.

mq_close#

int mq_close(    mqd_t mqdes)

Return: Zero on success or -1 with errno (see Errno) set to:

  • EBADF: mqdes is not a valid descriptor

This function closes the message queue.

mq_unlink#

int mq_unlink(    const char * name)

Return: Zero on success or -1 with errno (see Errno) set to:

  • ENAMETOOLONG: length of name exceeds NAME_MAX
  • EACCES: write permission to name is denied
  • ENOENT: the queue does not exist
  • EIO: I/O error while deleting the queue

This function deletes a message queue if no other processes have it open. If another process has the queue open, the queue will be deleted when said process closes the queue using mq_close().

mq_discard#

void mq_discard(    mqd_t mqdes)

mq_flush#

void mq_flush(    mqd_t mqdes)

mq_notify#

int mq_notify(    mqd_t mqdes,    const struct sigevent * notification)

Return: Zero on success or -1 with errno (see Errno) set to:

  • ENOTSUP: feature is not supported

This function is not supported.

mq_receive#

ssize_t mq_receive(    mqd_t mqdes,    char * msg_ptr,    size_t msg_len,    unsigned * msg_prio)

Return: Zero on success or -1 with errno (see Errno) set to:

  • EAGAIN: no message on the queue and O_NONBLOCK is set in the descriptor flags
  • EIO: I/O error while accessing the queue
  • EMSGSIZE: msg_len is less than the message size of the queue
  • EBADF: mqdes is not a valid message queue descriptor

This function removes a message from the queue and stores the message at msg_ptr.

mq_timedreceive#

ssize_t mq_timedreceive(    mqd_t mqdes,    char * msg_ptr,    size_t msg_len,    unsigned * msg_prio,    const struct timespec * abs_timeout)

Return: Zero on success or -1 with errno (see Errno) set to:

  • EAGAIN: no message on the queue and O_NONBLOCK is set in the descriptor flags
  • EIO: I/O error while accessing the queue
  • ETIMEDOUT: abs_timeout was exceeded by CLOCK_REALTIME
  • EMSGSIZE: msg_len is less than the message size of the queue
  • EBADF: mqdes is not a valid message queue descriptor

This function removes a message from the queue and stores the message at msg_ptr. If no messages are available, the thread is blocked until either a messsage is available or the value of CLOCK_REALTIME is less then abs_timeout.

If ONONBLOCK is set in _mqdes, the function returns immediately whether a message is ready or not.

mq_send#

int mq_send(    mqd_t mqdes,    const char * msg_ptr,    size_t msg_len,    unsigned msg_prio)

Return: Zero on success or -1 with errno (see Errno) set to:

  • EAGAIN: no room on the queue and O_NONBLOCK is set in the descriptor flags
  • EIO: I/O error while accessing the queue
  • EBADF: mqdes is not a valid message queue descriptor

This function sends a message pointed to by msg_ptr. If there is no room in the queue (and ONONBLOCK is not set in _mqdes), the thread is blocked until a message is removed from the queue.

If ONONBLOCK is set in _mqdes, the function returns immediately whether a message is sent or not.

mq_timedsend#

int mq_timedsend(    mqd_t mqdes,    const char * msg_ptr,    size_t msg_len,    unsigned msg_prio,    const struct timespec * abs_timeout)

Return: Zero on success or -1 with errno (see Errno) set to:

  • EAGAIN: no message on the queue and O_NONBLOCK is set in the descriptor flags
  • EIO: I/O error while accessing the queue
  • ETIMEDOUT: abs_timeout was exceeded by CLOCK_REALTIME
  • EBADF: mqdes is not a valid message queue descriptor

This function sends a message pointed to by msg_ptr. If there is no room in the queue (and ONONBLOCK is not set in _mqdes), the thread is blocked until a message is removed from the queue or until the value of CLOCK_REALTIME exceeds abs_timeout.

If ONONBLOCK is set in _mqdes, the function returns immediately whether a message is sent or not.

mq_tryreceive#

ssize_t mq_tryreceive(    mqd_t mqdes,    char * msg_ptr,    size_t msg_len,    unsigned * msg_prio)

mq_trysend#

int mq_trysend(    mqd_t mqdes,    const char * msg_ptr,    size_t msg_len,    unsigned msg_prio)

Updated on 18 September 2021 at 21:44:10 MDT