共享内存是进程间通信中最简单的方式之一。共享内存允许两个或更多进程访问同一块内存,就如同 malloc() 函数向不同进程返回了指向同一个物理内存区域的指针。当一个进程改变了这块地址中的内容的时候,其它进程都会察觉到这个更改。其特点是:
- 共享内存是进程间共享数据的一种最快的方法。一个进程向共享的内存区域写入了数据,共享这个内存区域的所有进程就可以立刻看到其中的内容。
- 使用共享内存要注意的是多个进程之间对一个给定存储区访问的互斥。若一个进程正在向共享内存区写数据,则在它做完这一步操作前,别的进程不应当去读、写这些数据。
共享内存有两种方式,即 shm 和 mmap 方式。前者直接共享物理内存,后者通过一个中间文件间接共享内存。这篇博客只讲一下 shm 方式共享内存。
函数
#include <sys/ipc.h>
#include <sys/shm.h>
/* 创建或打开一块共享内存区。*/
int shmget(key_t key, size_t size,int shmflg);
/* 参数:
key:进程间通信键值,ftok() 的返回值。
size:该共享存储段的长度(字节)。
shmflg:标识函数的行为及共享内存的权限,其取值如下:
IPC_CREAT:如果不存在就创建
IPC_EXCL: 如果已经存在则返回失败
位或权限位:共享内存位或权限位后可以设置共享内存的访问权限,格式和 open() 函数的 mode_t 一样,但可执行权限未使用。
*/
/* 返回值:
成功:共享内存标识符。
失败:-1。
*/
#include <sys/types.h>
#include <sys/shm.h>
/* 将一个共享内存段映射到调用进程的数据段中。简单来理解,
让进程和共享内存建立一种联系,让进程某个指针指向此共享内存。*/
void *shmat(int shmid, const void *shmaddr, int shmflg);
/* 参数:
shmid:共享内存标识符,shmget() 的返回值。
shmaddr:共享内存映射地址(若为 NULL 则由系统自动指定),推荐使用 NULL。
shmflg:共享内存段的访问权限和映射条件( 通常为 0 ),具体取值如下:
0:共享内存具有可读可写权限。
SHM_RDONLY:只读。
SHM_RND:(shmaddr 非空时才有效)
*/
/* 返回值:
成功:共享内存段映射地址( 相当于这个指针就指向此共享内存 )
失败:-1
*/
#include <sys/types.h>
#include <sys/shm.h>
/* 将共享内存和当前进程分离(仅仅是断开联系并不删除共享内存,相当于让之前的指向此共享内存的指针,不再指向)。*/
int shmdt(const void *shmaddr);
/* 参数:
shmaddr:共享内存映射地址。
*/
/* 返回值:
成功:0
失败:-1
*/
#include <sys/ipc.h>
#include <sys/shm.h>
/* 共享内存属性的控制。*/
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
/* 参数:
shmid:共享内存标识符。
cmd:函数功能的控制,其取值如下:
IPC_RMID:删除。(常用 )
IPC_SET:设置 shmid_ds 参数,相当于把共享内存原来的属性值替换为 buf 里的属性值。
IPC_STAT:保存 shmid_ds 参数,把共享内存原来的属性值备份到 buf 里。
SHM_LOCK:锁定共享内存段( 超级用户 )。
SHM_UNLOCK:解锁共享内存段。
SHM_LOCK 用于锁定内存,禁止内存交换。并不代表共享内存被锁定后禁止其它进程访问。其真正的意义是:被锁定的内存不允许被交换到虚拟内存中。这样做的优势在于让共享内存一直处于内存中,从而提高程序性能。
buf:shmid_ds 数据类型的地址,用来存放或修改共享内存的属性。
*/
/* 返回值:
成功:0
失败:-1
*/
代码
/* study.cpp 写端代码 */
#include<sys/ipc.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/* 用于传递消息的结构体 */
typedef struct _msg
{
int id;
char str[64];
}MSG;
int main()
{
MSG* msg;
/* 获取键值 */
key_t key = ftok("./",2015);
if(key == -1)
{
perror("ftok");
exit(-1);
}
/* 创建或打开一块共享内存,并返回内存标识符 */
int shd = shmget(key,sizeof(MSG),IPC_CREAT | 0666);
if(shd == -1)
{
perror("shmget");
exit(-1);
}
/* 映射内存地址到当前进程,并返回内存块的地址 */
msg = (MSG*)shmat(shd,NULL,0);
if(msg == (MSG*)-1)
{
perror("shmat");
exit(-1);
}
/* 改变内存地址的数据 */
memset((void*)msg,0,sizeof(MSG));
msg->id = 99;
strcpy(msg->str,"Hello world!");
/* 查看系统中的共享内存 */
system("ipcs -m");
return 0;
}
/* main.c 读端代码 */
#include<sys/ipc.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/* 用于传递消息的结构体 */
typedef struct _msg
{
int id;
char str[64];
}MSG;
int main()
{
MSG* msg = NULL;
/* 获取键值 */
key_t key = ftok("./",2015);
if(key == -1)
{
perror("ftok");
exit(-1);
}
/* 创建或打开一块共享内存,并返回内存标识符 */
int shd = shmget(key,sizeof(MSG),IPC_CREAT | 0666);
if(shd == -1)
{
perror("shmget");
exit(-1);
}
/* 映射内存地址到当前进程,并返回内存块的地址 */
msg = (MSG*)shmat(shd,NULL,0);
if(msg == (MSG*)-1)
{
perror("shmat");
exit(-1);
}
/* 输出共享内存中的数据 */
printf("id = %d\nstr = %s\n",msg->id,msg->str);
/* 解除映射关系 */
int ret = shmdt(msg);
if(ret < 0)
{
perror("shmdt");
exit(-1);
}
/* 删除共享内存 */
shmctl(shd,IPC_RMID,NULL);
system("ipcs -m");
return 0;
}
编译执行如下:
lingyun@manjaro ~/Document/CppCode/study gcc study.cpp -o study
lingyun@manjaro ~/Document/CppCode/study ./study
------------ 共享内存段 --------------
键 shmid 拥有者 权限 字节 连接数 状态
0x00000000 3178496 lingyun 600 524288 2 目标
0x00000000 294913 lingyun 600 524288 2 目标
0x00000000 327682 lingyun 600 524288 2 目标
0x00000000 360451 lingyun 600 524288 2 目标
0x00000000 393220 lingyun 600 524288 2 目标
0x00000000 425989 lingyun 600 524288 2 目标
0x00000000 458758 lingyun 600 524288 2 目标
0x00000000 491527 lingyun 600 524288 2 目标
0x00000000 524296 lingyun 600 524288 2 目标
0x00000000 622601 lingyun 600 524288 2 目标
0x00000000 2785290 lingyun 600 524288 2 目标
0x00000000 1048587 lingyun 600 524288 2 目标
0x00000000 1114124 lingyun 600 524288 2 目标
0x00000000 16908301 lingyun 600 524288 2 目标
0x00000000 1572878 lingyun 600 524288 2 目标
0x00000000 12288015 lingyun 600 524288 2 目标
0x00000000 12353552 lingyun 600 524288 2 目标
0x00000000 16056337 lingyun 700 166600 2 目标
0x51290019 1802258 lingyun 600 33024 1
0x00000000 3080211 lingyun 600 16777216 2 目标
0x00000000 10649620 lingyun 600 524288 2 目标
0x00000000 11894805 lingyun 700 16472 2 目标
0x00000000 3473430 lingyun 600 524288 2 目标
0xdf06138e 16941079 lingyun 666 68 1 //字节数为68,是我们的共享内存
0x00000000 13762584 lingyun 600 524288 2 目标
lingyun@manjaro ~/Document/CppCode/study
lingyun@manjaro ~/Document/CppCode/study gcc main.c -o main
lingyun@manjaro ~/Document/CppCode/study ./main
id = 99
str = Hello world! //打印出了共享内存中的数据,并且下面不会有68个字节的共享内存段了,因为已经把它删除了
------------ 共享内存段 --------------
键 shmid 拥有者 权限 字节 连接数 状态
0x00000000 3178496 lingyun 600 524288 2 目标
0x00000000 294913 lingyun 600 524288 2 目标
0x00000000 327682 lingyun 600 524288 2 目标
0x00000000 360451 lingyun 600 524288 2 目标
0x00000000 393220 lingyun 600 524288 2 目标
0x00000000 425989 lingyun 600 524288 2 目标
0x00000000 458758 lingyun 600 524288 2 目标
0x00000000 491527 lingyun 600 524288 2 目标
0x00000000 524296 lingyun 600 524288 2 目标
0x00000000 622601 lingyun 600 524288 2 目标
0x00000000 2785290 lingyun 600 524288 2 目标
0x00000000 1048587 lingyun 600 524288 2 目标
0x00000000 1114124 lingyun 600 524288 2 目标
0x00000000 17072141 lingyun 600 524288 2 目标
0x00000000 1572878 lingyun 600 524288 2 目标
0x00000000 12288015 lingyun 600 524288 2 目标
0x00000000 12353552 lingyun 600 524288 2 目标
0x00000000 16056337 lingyun 700 166600 2 目标
0x51290019 1802258 lingyun 600 33024 1
0x00000000 3080211 lingyun 600 16777216 2 目标
0x00000000 10649620 lingyun 600 524288 2 目标
0x00000000 11894805 lingyun 700 16472 2 目标
0x00000000 3473430 lingyun 600 524288 2 目标
0x00000000 13762584 lingyun 600 524288 2 目标
lingyun@manjaro ~/Document/CppCode/study