Linux system is an operating system that supports concurrent execution of multi-tasks. It can run multiple processes at the same time, thereby improving system utilization and efficiency. However, if data exchange and collaboration are required between these processes, some inter-process communication (IPC) methods need to be used, such as signals, shared memory, semaphores, etc. Among them, POSIX message queue is a relatively simple and reliable IPC method. It allows two or more processes to transmit messages through a queue without caring about the content and format of the message. This article will introduce the methods of POSIX message queue in Linux system, including the creation, opening, sending, receiving, closing and deletion of message queue.
#include #include #include mq_open() //创建/获取消息队列fd mq_get() //设置/获取消息队列属性 mq_send()/mq_receive() //发送/接收消息 mq_close() //脱接消息队列 mq_unlink() //删除消息队列
Known from $man mq_overview
: The message queue is uniquely identified by a name in the form of '/somename'. The maximum length of the name string cannot be towards NAME_MAX (i.e., 255), two processes Communicate by using the same message queue name
//创建一个POSIX消息队列或打开一个已经存在的消息队列,成功返回消息队列描述符mqdes供其他函数使用,失败返回 (mqd_t)-1设errno //Link with -lrt. 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);
oflag
must include one of:
can be Bitwised ORed:
modeIf there is O_CREAT in oflag, then mode is used to represent the permissions of the newly created message queue
attrIf there is O_CREAT in oflag, then attr represents Attributes of the message queue. If attr is NULL, the message queue will be configured according to the default settings (mq_overview(7) for details.)
//设置/修改 / 获取消息队列属性,成功返回0,失败返回-1设errno //Link with -lrt. int mq_setattr(mqd_t mqdes, const struct mq_attr *newattr, struct mq_attr *oldattr); int mq_getattr(mqd_t mqdes, struct mq_attr *attr);
mqattr structure
struct mq_attr { long mq_flags; /* Flags: 0 or O_NONBLOCK */ long mq_maxmsg; /* Max. # of messages on queue */ long mq_msgsize; /* Max. message size (bytes) */ long mq_curmsgs; /* # of messages currently in queue */ };
//发送消息到mqdes指向的消息队列。成功返回0,失败返回-1设errno //Link with -lrt. int mq_send(mqd_t mqdes, const char *msg_ptr,size_t msg_len, unsigned int msg_prio); //如果消息队列满 #include //额外的header int mq_timedsend(mqd_t mqdes, const char *msg_ptr,size_t msg_len, unsigned int msg_prio, const struct timespec *abs_timeout);
msg_len msg_ptr指向的消息队列的长度,这个长度必须msg_prio 一个用于表示消息优先级的非0整数,消息按照优先级递减的顺序被放置在消息队列中,同样优先级的消息,新的消息在老的之后,如果消息队列满了,就进入blocked状态,新的消息必须等到消息队列有空间了进入,或者调用被signal中断了。如果flag里有O_NOBLOCK选项,则此时会直接报错
abs_timeout:如果消息队列满了,那么就根据abs_timeout指向的结构体表明的时间进行锁定,里面的时间是从970-01-01 00:00:00 +0000 (UTC)开始按微秒计量的时间,如果时间到了,那么mq_timesend()立即返回
struct timespec { time_t tv_sec; /* seconds */ long tv_nsec; /* nanoseconds */ };
//从消息队列中取出优先级最高的里面的最老的消息,成功返回消息取出消息的大小,失败返回-1设errno //具体功能参照mq_send()/mq_timesend() //Link with -lrt. ssize_t mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned int *msg_prio); #include //额外的header ssize_t mq_timedreceive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned int *msg_prio, const struct timespec *abs_timeout);
//允许调用进程注册或去注册同步来消息的通知,成功返回0,失败返回-1设errno //Link with -lrt. int mq_notify(mqd_t mqdes, const struct sigevent *sevp);
sevp指向sigevent的指针
union sigval { /* Data passed with notification */ int sival_int; /* Integer value */ void* sival_ptr; /* Pointer value */ }; struct sigevent { int sigev_notify; /* Notification method */ int sigev_signo; /* Notification signal */ union sigval sigev_value; /* Data passed with notification */ void(*sigev_notify_function) (union sigval); //Function used for thread notification (SIGEV_THREAD) void* sigev_notify_attributes; // Attributes for notification thread (SIGEV_THREAD) pid_t sigev_notify_thread_id; /* ID of thread to signal (SIGEV_THREAD_ID) */ };
sigev_notify使用下列的宏进行配置:
//关闭消息队列描述符mqdes,如果有进程存在针对这个队列的notification request,那么也会被移除 //成功返回0,失败返回-1设errno //Link with -lrt. int mq_close(mqd_t mqdes);
//移除队列名指定的消息队列,一旦最后一个进程关闭了针对这个消息队列的描述符,就会销毁这个消息队列 //成功返回0,失败返回-1设errno //Link with -lrt. int mq_unlink(const char *name);
本文介绍了Linux系统中POSIX 消息队列的方法,包括消息队列的创建、打开、发送、接收、关闭和删除等方面。通过了解和掌握这些知识,我们可以更好地使用POSIX 消息队列来实现进程间通信,提高系统的稳定性和效率。当然,Linux系统中POSIX 消息队列还有很多其他的特性和用法,需要我们不断地学习和研究。希望本文能给你带来一些启发和帮助。
The above is the detailed content of Linux IPC POSIX Message Queuing: A Simple Way to Reliable Message Delivery. For more information, please follow other related articles on the PHP Chinese website!