1.消息队列
消息队列是消息的链接表,存放在内核中并由消息队列标识符标识。 标识符是IPC对象的内部名, 而它的外部名则是key(键), 它的基本类型是key_t, 在头文件<sys/types.h>中定义为长整型.。键由内核变换成标识符。
用户可以从消息队列中读取数据和添加消息,其中发送进程添加消息到队列的末尾,接收进程在队列的头部接收消息,消息一旦被接收,就会从队列中删除。和FIFO有点类似,但是它可以实现消息的随机查询,比FIFO具有更大的优势(比如按消息的类型字段取消息)。
2.消息队列的三个数据结构
linux内核采用的结构msqid_ds管理消息队列
[cpp] view plain copy
- struct msqid_ds
- {
- struct ipc_perm msg_perm; //消息队列访问权限
- struct msg *msg_first; //指向第一个消息的指针
- struct msg *msg_last; //指向最后一个消息的指针
- ulong msg_cbytes; //消息队列当前的字节数
- ulong msg_qnum; //消息队列当前的消息个数
- ulong msg_qbytes; //消息队列可容纳的最大字节数
- pid_t msg_lsqid; //最后发送消息的进程号ID
- pid_t msg_lrqid; //最后接收消息的进程号ID
- time_t msg_stime; //最后发送消息的时间
- time_t msg_rtime; //最后接收消息的时间
- time_t msg_ctime; //最近修改消息队列的时间
- };
linux内核采用的结构msg_queue来描述消息队列
[cpp] view plain copy
- struct msg_queue {
- structkern_ipc_perm q_perm;
- time_tq_stime; /* last msgsndtime */
- time_tq_rtime; /* last msgrcvtime */
- time_tq_ctime; /* last changetime */
- unsignedlong q_cbytes; /* current number of bytes on queue*/
- unsignedlong q_qnum; /* number of messages inqueue */
- unsignedlong q_qbytes; /* max number of bytes on queue */
- pid_tq_lspid; /* pid oflast msgsnd */
- pid_tq_lrpid; /* lastreceive pid */
- structlist_head q_messages;
- structlist_head q_receivers;
- structlist_head q_senders;
- };
syetem V IPC 为每一个IPC结构设置了一个ipc_perm结构,该结构规定了许可权和所有者
[cpp] view plain copy
- struct ipc_perm
- {
- key_t key; //调用shmget()时给出的关键字
- uid_t uid; //共享内存所有者的有效用户ID
- gid_t gid; //共享内存所有者所属组的有效组ID
- uid_t cuid; //共享内存创建 者的有效用户ID
- gid_t cgid; //共享内存创建者所属组的有效组ID
- unsigned short mode; //Permissions + SHM_DEST和SHM_LOCKED标志
- unsignedshort seq; //序列号
- };
3.与消息队列有关的函数
3.1创建打开消息队列
3.2添加消息
3.3读取消息
3.4获得或修改消息队列或者删除消息队列
4.消息队列读取数据工作模式
附:
进程间通信方式
进程间的通信方式——1、pipe(管道)
https://blog.csdn.net/Windgs_YF/article/details/79973804
进程间通信方式———3、信号量(Semaphore)
https://blog.csdn.net/windgs_yf/article/details/79974049
进程间通信方式——4、共享内存
https://blog.csdn.net/windgs_yf/article/details/79974077