Skip to main content

Mqueue

Functions

mq_getattr(mqd_t mqdes, struct mq_attr *mqstat)#

int mq_getattr (mqd_t mqdes, struct mq_attr *mqstat)

Type: int

Parameters:

Details#

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

Returns:

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

  • EBADF: mqdes is not a valid message queue descriptor

mq_setattr(mqd_t mqdes, const struct mq_attr mqstat, struct mq_attr omqstat)#

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

Type: int

Parameters:

Details#

Sets the message queue attributes

Returns:

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

  • ENOTSUP

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

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

Type: mqd_t

Parameters:

  • const char * name: the full path to the message queue
  • int oflag: the flags used for opening the message queue
  • ... : mode_t mode, const struct mq_attr attr when O_CREAT is set in oflag*

Details#

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 , the message queue will only be available to the calling process. All other message queues can be shared among processes.

Returns:

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

  • ENAMETOOLONG: name length is greater than NAME_MAX
  • EEXIST: O_CREAT and O_EXCL are set in oflag but the queue already exists
  • ENOENT: O_CREAT 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: O_CREAT is set and attr is not null but mq_maxmsg or mq_msgsize is less than or equal to zero

mq_close(mqd_t mqdes)#

int mq_close (mqd_t mqdes)

Type: int

Parameters:

  • mqd_t mqdes: the message queue handler

Details#

This function closes the message queue.

Returns:

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

  • EBADF: mqdes is not a valid descriptor

mq_unlink(const char *name)#

int mq_unlink (const char *name)

Type: int

Parameters:

  • const char * name: the full path to the message queue

Details#

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().

Returns:

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

mq_discard(mqd_t mqdes)#

void mq_discard (mqd_t mqdes)

Type: void

Parameters:

  • mqd_t mqdes

mq_flush(mqd_t mqdes)#

void mq_flush (mqd_t mqdes)

Type: void

Parameters:

  • mqd_t mqdes

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

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

Type: int

Parameters:

  • mqd_t mqdes
  • const struct sigevent * notification

Details#

This function is not supported.

Returns:

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

  • ENOTSUP: feature is not supported

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

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

Type: ssize_t

Parameters:

  • mqd_t mqdes: the message queue handle
  • char * msg_ptr: a pointer to the destination memory for the message
  • size_t msg_len: the number of bytes available in msg_ptr (must be at least the size of the message size)
  • unsigned * msg_prio: if not NULL, the priority of the received message is stored here

Details#

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

Returns:

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

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

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

Type: ssize_t

Parameters:

Details#

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 O_NONBLOCK is set in mqdes, the function returns immediately whether a message is ready or not.

Returns:

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

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

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

Type: int

Parameters:

  • mqd_t mqdes: the message queue handle
  • const char * msg_ptr: a pointer to the message to be sent
  • size_t msg_len: the message size (must be less than or equal to mq_msgsize associated with mqdes
  • unsigned msg_prio: the priority of the message (see MQ_PRIO_MAX)

Details#

This function sends a message pointed to by msg_ptr. If there is no room in the queue (and O_NONBLOCK is not set in mqdes), the thread is blocked until a message is removed from the queue.If O_NONBLOCK is set in mqdes, the function returns immediately whether a message is sent or not.

Returns:

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

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

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

Type: int

Parameters:

  • mqd_t mqdes: see mq_send()
  • const char * msg_ptr: see mq_send()
  • size_t msg_len: see mq_send()
  • unsigned msg_prio: see mq_send()
  • const struct timespec * abs_timeout: the absolute timeout value

Details#

This function sends a message pointed to by msg_ptr. If there is no room in the queue (and O_NONBLOCK 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 O_NONBLOCK is set in mqdes, the function returns immediately whether a message is sent or not.

Returns:

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

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

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

Type: ssize_t

Parameters:

  • mqd_t mqdes
  • char * msg_ptr
  • size_t msg_len
  • unsigned * msg_prio

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

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

Type: int

Parameters:

  • mqd_t mqdes
  • const char * msg_ptr
  • size_t msg_len
  • unsigned msg_prio

Details

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](structmq__attr) attr;  char msg[32];
  attr.[mq_maxmsg](structmq__attr#structmq__attr_1aa535e1fa6c07c1d189e720ba15f9af08) = 4;  attr.mq_msgsize = 32;
  mdes = [mq_open](group__mqueue#group__mqueue_1gaf5d8bf423701bd1783849119511381a5)("/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](group__mqueue#group__mqueue_1ga753177f77f6eec2a80b57e8a68b36bed)(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](group__mqueue#group__mqueue_1gaf5d8bf423701bd1783849119511381a5)("/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](group__mqueue#group__mqueue_1ga98eea38b09dabab5144afcaf109c82cd)(mdes, msg, strlen(msg), &msg_prio) < 0 ){     perror("failed to send message");  }
  printf("received: %s\n", msg);