区别于管道
1.不同于管道面向字节流,它是以数据块为单位进行传送的(消息队列类似于网络通讯协议中的UDP,管道类似于TCP)
2.区别于管道生命进程随进程,消息队列的生命周期是随内核的
3.区别fifo管道的先进先出,消息队列读取是并行的
消息队列
消息队列是系统的ipc资源,
操作系统为每个ipc资源都统一维护了一个数据结构ipc_perm
struct ipc_perm
{
key_t key; /*使用其标识ipc_perm资源*/
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; /* 序列号*/
};
而对于消息队列的结构体mgqid_ds 也是具有上面的结构体的
struct msqid_ds {
struct ipc_perm msg_perm;
struct msg *msg_first; /* first message on queue,unused */
struct msg *msg_last; /* last message in queue,unused */
__kernel_time_t msg_stime; /* last msgsnd time */
__kernel_time_t msg_rtime; /* last msgrcv time */
__kernel_time_t msg_ctime; /* last change time */
unsigned long msg_lcbytes; /* Reuse junk fields for 32 bit */
unsigned long msg_lqbytes; /* ditto */
unsigned short msg_cbytes; /* current number of bytes on queue */
unsigned short msg_qnum; /* number of messages in queue */
unsigned short msg_qbytes; /* max number of bytes on queue */
__kernel_ipc_pid_t msg_lspid; /* pid of last msgsnd */
__kernel_ipc_pid_t msg_lrpid; /* last receive pid */
};
可以看到看到消息队列的的结构体中第一个成员变量就是ipc_perm
同时从第二个和第三个成员变量可以看出 它有一个头指针和尾指针
可以猜想它存储消息的队列大概就是一个双向链表
还注意到这个指针是一个结构