POSIX message queues 讓 processe 間用 message 交換訊息,提供 System V message queues (msgget()、msgsnd()、msgrcv()、...) 類似的功能。
mq_open() 建立 message queue,回傳 message queue descriptor (mqd_t) 作為後續參照。每個 message queue 有一個 /somename 格式的 name,最長 NAME_MAX (i.e., 255)。兩個 processe 可以用相同 name 開啟同一個 message queue。
#include <fcntl.h> /* For O_* constants */ #include <sys/stat.h> /* For mode constants */ #include <mqueue.h> mqd_t mq_open(const char *name, int oflag); mqd_t mq_open(const char *name, int oflag, mode_t mode, struct mq_attr *attr);
mq_send() 和 mq_receive() 送收訊息。每個訊息有優先權,從低到高是 0 到 sysconf(_SC_MQ_PRIO_MAX) - 1,高優先權先收。
int mq_send(mqd_t mqdes, const char *msg_ptr,
size_t msg_len, unsigned int msg_prio);
int mq_timedsend(mqd_t mqdes, const char *msg_ptr,
size_t msg_len, unsigned int msg_prio,
const struct timespec *abs_timeout);
ssize_t mq_receive(mqd_t mqdes, char *msg_ptr,
size_t msg_len, unsigned int *msg_prio);
ssize_t mq_timedreceive(mqd_t mqdes, char *msg_ptr,
size_t msg_len, unsigned int *msg_prio,
const struct timespec *abs_timeout);
process 用 mq_close() 停用 queue。queue 不再需要,用 mq_unlink() 移除。
mq_getaddr() 和 mq_setattr() 取得和修改 queue 屬性。
A process can request asynchronous notification of the arrival of a message on a previously empty queue using mq_notify(3).
fork() 後,child 複製繼承 message queue,share the flags (mq_flags) that are associated with the open message queue description.
https://www.man7.org/linux/man-pages/man7/mq_overview.7.html
沒有留言:
張貼留言