原创首发于CSDN,转载请注明出处,谢谢!https://blog.csdn.net/weixin_46959681/article/details/114167643
文章目录
消息队列 MQ
存放在内核中的消息队列实质上是一条有关消息的链表,该链表的每一个消息节点(Node)都存放着信息。 一个消息队列由标识符(即队列 ID)来标识。
拓 展 : 拓展: 拓展: 标识符是IPC对象的内部名。目的是多个进程间能够访问到同一个进程,需要提供一个外部名。即“键(key)”,键与每个IPC对象关联,并作为对象的外部名。键的数据类型为key_t,由内核变换成标识符。
|特点
消息队列是存放在内核中的消息链表,伴随着内核对系统资源的持续调用而持续存在,仅在内核删除一个消息队列时,对应的消息队列才会真正被删除。
消息队列具链表的特点,同节点查找一样可以实现消息的随机查询,读取消息时不一定要像队列以先进先出的次序读取,也可以按消息的类型读取。
相比于笔者的上一篇博文中只能承载无格式字节流的管道,消息队列克服了信号传递信息少、缓冲区受限等特点,能将不同进程的数据流以消息队列形式发送给任意进程。 在数据读取以及发送方面,管道发送的数据没有类型,读取数据端无差别从管道中按照前后顺序读取;消息队列有类型,读端可以根据数据类型读取特定的数据。
|函数原型
#include <sys/msg.h>
#include <sys/types.h>
#include <sys/ipc.h>
// 第一步:创建或打开消息队列,成功返回队列ID,失败返回-1。
int msgget(key_t key, int flag);
// 第二步:在消息队列中添加消息,成功返回0,失败返回-1。
int msgsnd(int msqid, const void *ptr, size_t size, int flag);
// 第三步:读取消息;成功返回消息数据的长度,失败返回-1。
int msgrcv(int msqid, void *ptr, size_t size, long type,int flag);
// 第四步:在内核中删除消息队列,成功返回0,失败返回-1。
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
|代码演示 msgGet.c、msgSnd.c
第一步:运行 msgGet.c
生成的编译文件 get
/* msgGet.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[128]; /* message data */
};
int main()
{
int msgid;
struct msgbuf readBuf;
key_t key;
key = ftok(".",'z');
printf("key = %x\n",key);
msgid = msgget(key,IPC_CREAT|0777);
if(msgid == -1){
printf("Get message queue failure.\n");
perror("why:");
exit(-1);
}
msgrcv(msgid,&readBuf,sizeof(readBuf.mtext),888,0);
printf("Read from message queue: %s\n",readBuf.mtext);
//删除消息队列。
msgctl(msgid,IPC_RMID,NULL);
return 0;
}
第二步:运行 msgSnd.c
生成的编译文件 send
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include<sys/stat.h>
#include<stdio.h>
#include<errno.h>
#include<fcntl.h>
#include<string.h>
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[128]; /* message data */
};
int main()
{
int msgid;
struct msgbuf sendBuf = {888,"Bytes is coming from msgSnd."};
key_t key;
key = ftok(".",'z');
printf("key = %x\n",key);
msgid = msgget(key,IPC_CREAT|0777);
if(msgid == -1){
printf("Get message queue failure.\n");
perror("why:");
}
msgsnd(msgid,&sendBuf,strlen(sendBuf.mtext),0);
//删除消息队列。
msgctl(msgid,IPC_RMID,NULL);
return 0;
}
运行结果:
【键值 key 一致表示接受、发送都是同一个消息队列。】
|经典应用场景
这个回答基本全面覆盖了消息队列的常见的应用场景:
- 系统解藕
- 异步
- 电商大流量的削峰/限流
共享内存 SHM
|特点
共享内存是效率最高的IPC通信方式,真正原因在于其 映射一段能被其他进程所访问的内存 。虽然由由一个进程创建,但是系统内其他多个进程都可以访问并进行相应的操作。这样就不需要在 客户端 和 服务端 之间进行赋值。
互斥性 —— 使用共享内存的唯一需要注意的是多个进程对一给定的存储区的同步访问。
- 若服务器进程正在向共享存储区写入数据,则写完数据之前客户进程不应读取数据。
- 或者客户进程正在从共享内存中读取数据,服务进程不应写入数据。故我们通常需要使用 信号量 对共享内存进行同步控制。
或者
来自知乎文章 进程间的通信方式(一):共享内存
|函数原型
#include <sys/shm.h>
// 创建一个共享内存:成功返回共享内存ID,失败返回-1
int shmget(key_t key, size_t size, int flag);
// 挂在共享内存到当前进程的地址空间:成功返回指向共享内存的指针,失败返回-1
void *shmat(int shm_id, const void *addr, int flag);
// 断开与共享内存的连接:成功返回0,失败返回-1
int shmdt(void *addr);
// 控制共享内存的相关信息:成功返回0,失败返回-1
int shmctl(int shm_id, int cmd, struct shmid_ds *buf);
|代码演示 shmw.c
、shmr.c
第一步:运行 shmw.c
生成的文件 w
创建共享代码池并往里写东西。
/* shmw.c */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main()
{
int shmid;
char *shmaddr;
key_t key;
key = ftok(".",1);
printf("key value : %d\n", key);
shmid = shmget(key, 1024*3, IPC_CREAT|0666);
if(shmid == -1){
printf("Shmget fail.\n");
perror("why");
exit(-1);
}
//挂载共享内存。
shmaddr = shmat(shmid,0,0);
printf("shmat success.\n");
strcpy(shmaddr,"You have to evolve.");
//睡眠5秒,使得进程读取字符指针指向的数据。
sleep(4);
shmdt(shmaddr);
shmctl(shmid,IPC_RMID,0);
printf("Quit.\n");
return 0;
}
第二步:运行 shmr.c
生成的文件 r
读取上一步写入的内容。
/* shmr.c */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main()
{
int shmid;
char *shmaddr;
//
key_t key;
key = ftok(".",1);
printf("key value : %d\n", key);
shmid = shmget(key,1024*3,0);
if(shmid == -1){
printf("Shmget fail.\n");
perror("why");
exit(-1);
}
shmaddr = shmat(shmid,0,0);
printf("shmat success.\n");
printf("data: %s\n",shmaddr);
shmdt(shmaddr);
shmctl(shmid,IPC_RMID,0);
printf("Quit.\n");
return 0;
}
运行结果:
写入 | 读取 |
---|---|
参考资料
- 参考博客 进程间通信的五种方式
- 参考博客 进程间的五种通信方式介绍-详解
- 知乎文章 进程间的通信方式(一):共享内存
文章更新记录
- “消息队列” 一节完成。 「2021.2.27 15:58」
- “共享内存” 一节完成。 「2021.3.1 22:20」
- 将 “共享内存” 一节特点补上,增加了两张图片。 「2021.3.2 11:12」
- 添加 “参考资料” 一栏。 「2021.3.2 11:33」
- 把该篇博文移动到二级专栏“Linux操作系统”。 「2021.5.13 14:52」
P.S. 拖延了一天的博文终于完成了……多浪费了一天!