Overview
The message queue can be regarded as a linked list of messages, and threads can read and write messages in the queue.
Before a process writes a message to a queue, another process does not need to wait for a message to arrive on the queue.
A process can write some messages to a queue, terminate it, and let another process read the messages at a later time. When the last close of a pipe or FIFO occurs, the data still on that pipe or FIFO will be cleared.
POSIX message queue features
Reads to the POSIX message queue always return the oldest message with the highest priority.
POSIX message queues allow a signal to be generated or a thread to be started when a message is placed on an empty queue.
Each message in the queue has the following properties:
priority
The length of the data part of the message
the data itself
The layout of the message queue:
mq_open mq_close mq_unlink function
The mq_open function creates a new message queue or opens an existing message queue.
#include <mqueue.h> mqd_t mq_open(const char *name,int oflag,.../*mode_t mode,struct mq_attr *attr */);
name: posix IPC name, which may or may not be a real pathname in a filesystem.
oflag参数:O_RDONLY,O_WRONLY,O_RDWR,O_CREAT,O_EXCEL,O_NONBLOCK
mode: This parameter is required when creating a new queue.
constant value | illustrate |
S_IRUSR S_IWUSR |
User (owner) read User (owner) write |
S_IRGRP S_IWGRP |
(belonging to) group member read (belonging to) group members write |
S_IROTH S_IWOTH |
read by other users written by other users |
attr: This parameter is required when creating a new queue. Used to specify certain properties for the new queue, or default properties if it is a null pointer.
mq_open return value: message queue descriptor.