Shared memory for communication between processes
1 Description
Take a couple as an analogy. For example, a man and a woman are going on a date, and the two parties use a piece of paper to exchange information. Assuming that there is a table between the two and a piece of paper on the table, the man can write his thoughts on the paper in front of him. Next, I will show it directly to the woman. The same reason, in terms of processes, there are two processes A and B. Process A has its own independent space, and process B has its own independent space. The independent process spaces will not communicate with each other. , But there is a public memory space in the memory, and this public memory address A and B can be obtained, they can face the same physical space, if A has a pointer to the public address space, although the pointer The address of is not in the space of A, but A can read the contents of the pointer address, and the same is true for B.
So what is the programming idea to realize shared memory?
- First, create a shared memory space, if any, open it directly
- Mapping, mapping the shared space to A, B space
- Then you can exchange data
- Free shared memory after data exchange
2 The code prototypes of shared memory are:
1 #include <sys/shm.h>
2 int shmget(key_t key, size_t size, int shmflag);
// Create or obtain a shared memory: return the shared memory ID if successful , and return -1 if it fails
Size: The size of the shared memory must be aligned in megabytes, so it opens up a mega-level space
例:shmid = shmget(key,1024*4,IPC_CREAT|0666);
3 void *shmat(int shmid, const void *shmaddr, int shmflag);
// Connect the shared memory to the address space of the current process: return a pointer to the shared memory on success, return -1 on failure
Shmid: We get the ID of the shmid above shmid = shmget(key_t key, size_t size, int shmflag);
Shmaddr: We generally take 0 and automatically arrange shared memory for us.
Shmflag: We generally also take 0 , 0 means the mapped shared memory is readable and writable. Of course you can arrange it yourself
例:shmat(shmid,0,0);
4 int shmdt(void *addr);
// Disconnect from the shared memory: return 0 on success , return -1 on failure
5 int shmctl(int shmid, int cmd, struct shmid_ds *buf);
// Remove information about shared memory to prevent space occupation: return 0 on success , return -1 on failure
Smd: is the instruction
3 example
Practice with simple examples
shmw.c
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main()
{
int shmid;
char *shmaddr;
key_t key;
key = ftok(".",1);
shmid = shmget(key,1024*4,IPC_CREAT|0666); // 可读可写
if(shmid == -1){ //判断是否成功,成功返回//指向共享内存的指针,失//败返回-1
printf("shmget error\n");
exit(-1);
}
shmaddr = shmat(shmid,0,0); //映射共享内存
printf("shmat ok\n");
strcpy(shmaddr,"Hello, welcome"); //映射成功后直接,把内容复制到该地址中
sleep(5); //睡眠5秒,让其他程序读
shmdt(shmaddr); //卸载共享内存
shmctl(shmid,IPC_RMID,0); //关掉共享内存
printf("quit\n");
return 0;
}
shmr.c
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main()
{
int shmid;
char *shmaddr;
key_t key;
key = ftok(".",1);
shmid = shmget(key,1024*4,0);//获取不创建设为0
if(shmid == -1){
printf("shmget error\n");
exit(-1);
}
shmaddr = shmat(shmid,0,0);
printf("shmat ok\n");
printf("data: %s\n",shmaddr); //直接读取
shmdt(shmaddr);
printf("quit\n");
return 0;
}