Home > System Tutorial > LINUX > Linux IPC POSIX Message Queuing: A Simple Way to Reliable Message Delivery

Linux IPC POSIX Message Queuing: A Simple Way to Reliable Message Delivery

PHPz
Release: 2024-02-10 14:45:25
forward
790 people have browsed it

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.

Linux IPC POSIX 消息队列:一种实现可靠消息传递的简单方式

Model:

#include
#include 
#include 
mq_open()   //创建/获取消息队列fd       
mq_get()    //设置/获取消息队列属性   
mq_send()/mq_receive()    //发送/接收消息 
mq_close()      //脱接消息队列            
mq_unlink()     //删除消息队列            
Copy after login

Advantages of POSIX mq VS Sys V mq

  • A simpler file-based application interface
  • Full support for message priority (priority ultimately determines the position of the message in the queue)
  • Fully supports asynchronous notification of message arrival, which is implemented through signals or thread creation
  • Timeout mechanism for blocking send and receive operations

Message queue name

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

mq_open()

//创建一个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);
Copy after login

oflag
must include one of:

  • O_RDONLY means opening the message queue to only receive messages
  • O_WRONLY means opening the message queue to only send messages
  • O_RDWR Indicates opening the message queue in a form that can be received and sent

can be Bitwised ORed:

  • O_NONBLOCKOpen the message queue in nonblocking mode
  • O_CREATIf a message queue does not exist, create it. The UID of the owner of the message queue is set to the effective UID of the calling process, and the GID is set to the effective GID of the calling process
  • O_EXCL Ensure that the message queue is created. If the message queue already exists, an error occurs

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

mq_setattr() / mq_getattr()

//设置/修改 / 获取消息队列属性,成功返回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);
Copy after login

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 */
};
Copy after login

mq_send() / mq_timesend()

//发送消息到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);
Copy after login

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 */
};
Copy after login

mq_receive()/mq_timedreceive()

//从消息队列中取出优先级最高的里面的最老的消息,成功返回消息取出消息的大小,失败返回-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);
Copy after login

mq_notify()

//允许调用进程注册或去注册同步来消息的通知,成功返回0,失败返回-1设errno
//Link with -lrt.
int mq_notify(mqd_t mqdes, const struct sigevent *sevp);
Copy after login

sevp指向sigevent的指针

  • 如果sevp不是NULL,那么这个函数就将调用进程注册到通知进程,只有一个进程可以被注册为通知进程
  • 如果sevp是NULL且当前进程已经被注册过了,则去注册,以便其他进程注册
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) */
};
Copy after login

sigev_notify使用下列的宏进行配置:

  • SIGEV_NONE调用进程仍旧被注册,但是有消息来的时候什么都不通知
  • SIGEV_SIGNAL通过给调用进程发送sigev_signo指定的信号来通知进程有消息来了
  • SIGEV_THREAD一旦有消息到了,就激活sigev_notify_function作为新的线程的启动函数

mq_close()

//关闭消息队列描述符mqdes,如果有进程存在针对这个队列的notification request,那么也会被移除

//成功返回0,失败返回-1设errno
//Link with -lrt.
int mq_close(mqd_t mqdes);
Copy after login

mq_unlink():

//移除队列名指定的消息队列,一旦最后一个进程关闭了针对这个消息队列的描述符,就会销毁这个消息队列
//成功返回0,失败返回-1设errno
//Link with -lrt.
int mq_unlink(const char *name);
Copy after login

本文介绍了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!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template