Shared memory concept
Shared memory is the most efficient IPC method because processes can read and write directly to the memory without copying data. But it does not have its own synchronization mechanism, need to cooperate with semaphore and other ways to synchronize.
When shared memory is created, the same physical memory is mapped to the address space of multiple processes. When one process modifies the data in the shared memory, the other processes can see the changes, and vice versa.
Mmap function
Function prototype:
void *mmap(void *adrr, size_t length, int prot, int flags, int fd, off_t offset);
The return value:
Success: returns the first address of the created mapping area.
Failed: MAP_FAILED is returned
Specific parameter meanings:
Addr: the first address to the mapping area, which is determined by the system kernel and is generally set to NULL;
Length: indicates the size of the mapping area to be created.
Prot: indicates the permission of a mapping area, which can be:
The PROT_EXEC mapping region can be executed
PROT_READ Map region can be read
PROT_WRITE The mapping area can be written
The PROT_NONE mapping region cannot be accessed
Flags: indicates the flags of the mapping area. MAP_FIXED and MAP_PRIVATE must be selected:
MAP_FIXED: Changes made to the mapping area will be reflected on the physical device, but need to call msync() or munmap();
MAP_PRIVATE: Changes made in a mapping area are not reflected on physical devices.
Fd: file descriptor for the created mapping area;
Offset: indicates the offset of the mapped file. It is usually set to 0, indicating that the mapping starts from the beginning.
Mumap function
Function prototype:
int munmap(void *addr, size_t length);
Function functions:
Just as you need free after malloc, you need to call MunMap to free the mapping area created by the Mmap call after it is used.
routine
Writing process:
1#include <stdio.h>
2#include <sys/mman.h>
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6#include <unistd.h>
7#include <string.h>
8
9typedef struct
10{
11 int id;
12 char name[20];
13 char gender;
14}stu;
15
16int main(int argc, char *argv[])
17{
18 stu *p = NULL;
19 int fd = 0;
20 stu student = {10, "harry".'m'}; 21 and 22if (argc < 2) {
23 printf("useage: ./a.out file\n");
24 return- 1; 25 } 26 27 fd = open(argv[1], O_RDWR | O_CREAT, 0664); 28if (fd == -1) {
29 printf("ERROR: open failed! \n");
30 return- 1; 31 } 32 ftruncate(fd, sizeof(stu)); 33 34 p = mmap(NULL, sizeof(stu), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 35if (p == MAP_FAILED) {
36 printf("ERROR: mmap failed! \n");
37 return- 1; 38 } 39 40 close(fd); 41 and 42while(1) { 43 memcpy(p, &student, sizeof(stu)); 44 student.id++; 45 sleep(2); 46 } 47 munmap(p, sizeof(stu)); 48 and 49return 0;
50}
Copy the code
Reading process:
1#include <stdio.h>
2#include <sys/mman.h>
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6#include <unistd.h>
7
8typedef struct
9{
10 int id;
11 char name[20];
12 char gender;
13}stu;
14
15int main(int argc, char *argv[])
16{
17 stu *p = NULL;
18 int fd = 0;
19
20 if (argc < 2) {
21 printf("useage: ./a.out file\n");
22 return- 1; 23 } 24 25 fd = open(argv[1], O_RDONLY); 26if (fd == -1) {
27 printf("ERROR: open failed! \n");
28 return- 1; 29 } 30 31 p = mmap(NULL, sizeof(stu), PROT_READ, MAP_SHARED, fd, 0); 32if (p == MAP_FAILED) {
33 printf("ERROR: mmap failed! \n");
34 return- 1; 35 } 36 37 close(fd); 38 and 39while(1) {40printf("id = %d, name = %s, gender = %c\n", p->id, p->name, p->gender);
41 sleep(2);
42 }
43
44 munmap(p, sizeof(stu));
45
46 return 0;
47}
Copy the code
5T technical information, including: Linux, C/C++, Python, Raspberry PI, embedded, Java, artificial intelligence, etc. Public number inside reply into group, invite you to enter master such as cloud technology exchange group.