Shared memory is one of the IPC mechanisms. It allows two unrelated processes to access the same memory, which is a very efficient way to pass data.
Shmget function
原型:intsize,int shmflg)头文件:<sys/ipc.h><sys/shm.h>功能:创建/获取一个共享内存,成功则返回共享内存的描述符id,失败返回-1参数:key:键值 size:创建的共享内存大小 shmflg:打开标志。如值为IPC_CREAT,则会创建一个新的共享内存
The shared memory created by the Shmget function returns its descriptor ID, and if you want to use that shared memory, you also need to map the shared memory you created to the memory space of the process that called the function, using the function Shmat.
Shmat function
原型:void *shmat(int shmid,constvoid *shmaddr,int shmflg)头文件:<sys/types.h><sys/shm.h>功能:将共享内存连接到调用该函数的进程的地址空间,成功返回映射后的地址,失败返回-1参数:shmid:共享内存的描述符 shmaddr:可以使用该参数来指定映射的地址,但一般设为NULL,让操作系统选择一块没有使用的内存地址来进行映射 shmflg:状态标志
SHMDT function
原型:int shmdt(constvoid *shmaddr)头文件:<sys/types.h><sys/shm.h>功能:与shmat对立,用来脱离与共享内存的连接映射。成功返回0,失败返回-1参数:shmaddr:需要与共享内存脱离映射的进程内存地址
Shmctl
原型:int shmctl(int shmid,int cmd,struct shmid_ds *buf)头文件:<sys/ipc.h><sys/shm.h>功能:根据cmd的值对共享内存进行不同的操作。操作失败返回-1参数:shmid:共享内存描述符id cmd:操作命令,如为IPC_RMID,则表示删除共享内存。 buf:在Linux系统中每个共享内存都有一个struct shmid_ds来对应描述该共享内存。shmctl函数中buf用于获取Linux描述该贡献内存的struct shmid_ds,但是一般不使用
Instance
Process A: Creates shared memory and maps to the memory address of its own process, and then writes data to shared memory. When you are finished writing, disconnect the mappings.
#include <sys/ipc.h>#include <sys/shm.h>#include <sys/types.h>#include <stdio.h>#include <string.h>typedef struct{intMEM_FLG;Charbuff[1024x768]; }shared_mem,*shared_mem_ptr;voidMain () {/ * Create shared memory * /key_t Key=ftok ("/home/jx/processa",1);intShmid=shmget (Key,sizeof(SHARED_MEM), ipc_creat);/ * Map Shared memory * /Shared_mem_ptr shm_ptr; Shm_ptr= (shared_mem_ptr) Shmat (Shmid,null,0); shm_ptr->mem_flg=0;/ * Write data to Shared memory * / intrunning=1; while(running) {if(shm_ptr->mem_flg==0) {scanf('%s ', Shm_ptr->buff); shm_ptr->mem_flg=1;intA=strcmp(Shm_ptr->buff,"Exit");if(a==0) running=0; } }/ * Disconnect mappings * /SHMDT ((Const void*) shm_ptr);}
Process B: Gets the shared memory and maps it to the memory address of its own process, reads the shared memory data, reads the disconnected and shared memory mappings, and deletes the shared memory.
#include <sys/ipc.h>#include <sys/shm.h>#include <sys/types.h>#include <stdio.h>#include <string.h>typedef struct{intMEM_FLG;Charbuff[1024x768]; }shared_mem,*shared_mem_ptr;voidMain () {/ * Get shared memory * /key_t Key=ftok ("/home/jx/processa",1);intShmid=shmget (Key,sizeof(SHARED_MEM), ipc_creat);/ * Map Shared memory * /Shared_mem_ptr shm_ptr; Shm_ptr= (shared_mem_ptr) Shmat (Shmid,null,0);/ * Read shared memory data * / intrunning=1; while(running) {if(shm_ptr->mem_flg==1) {printf("%s\n", Shm_ptr->buff); shm_ptr->mem_flg=0;intA=strcmp(Shm_ptr->buff,"Exit");if(a==0) running=0; } }/ * Disconnect mappings * /SHMDT ((Const void*) shm_ptr);/ * Delete Shared memory * / structShmid_ds *buf; Shmctl (SHMID,IPC_RMID,BUF);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Shared Memory Communication programming