Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the mmap function of shared memory in Linux system programming

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

What is the mmap function of shared memory in Linux system programming? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Shared memory concept

Shared memory is the most efficient IPC way to communicate, because the process can read and write memory directly without copying data. However, it does not have its own synchronization mechanism and needs to be synchronized with semaphores and other ways.

After the shared memory is created, the same physical memory is mapped to multiple process address spaces. When one process modifies the data of the shared memory, the other processes can see the modified content, and vice versa.

Mmap function

Function prototype:

Void mmap (void adrr, size_t length, int prot, int flags, int fd, off_t offset)

Return value:

Success: return the first address of the created mapping area

Failed: return MAP_FAILED

The meaning of specific parameters:

Addr: points to the first address of the mapping area, which is determined by the system kernel and is generally set to NULL

Length: the size of the mapping area to be created

Prot: the permissions of the mapping zone are generally as follows:

PROT_EXEC mapping area can be executed

PROT_READ mapping area can be read

The PROT_WRITE mapping area can be written to

PROT_NONE mapping area cannot be accessed

Flags: refers to the flag bit of the mapping area. MAP_FIXED and MAP_PRIVATE must select one:

MAP_FIXED: changes made to the mapping area will be reflected in the physical device, but you need to call msync () or munmap ()

MAP_PRIVATE: changes made to the mapping area are not reflected in the physical device.

Fd: the file descriptor of the created mapping area

Offset: the offset of the mapped file, which is generally set to 0, which means mapping from scratch.

Mumap function

Function prototype:

Int munmap (void * addr, size_t length)

Function function:

Just as free is needed after malloc, after the mapping area created by the mmap call is used, you need to call munmap to release it.

Routine

Write process:

# include # include typedef struct {11 int id; char name [20]; char gender;} stu; int main (int argc, char * argv []) {stu * p = NULL; int fd = 0; stu student = {10, "harry",'m'}; if (argc)

< 2) { printf("useage: ./a.out file\n"); return -1; } fd = open(argv[1], O_RDWR | O_CREAT, 0664); if (fd == -1) { printf("ERROR: open failed!\n"); return -1; } ftruncate(fd, sizeof(stu)); p = mmap(NULL, sizeof(stu), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (p == MAP_FAILED) { printf("ERROR: mmap failed!\n"); return -1; } close(fd); while (1) { memcpy(p, &student, sizeof(stu)); student.id++; sleep(2); } munmap(p, sizeof(stu)); return 0; } 读进程: #include #include #include #include #include #include typedef struct { int id; char name[20]; char gender; }stu; int main(int argc, char *argv[]) { stu *p = NULL; int fd = 0; if (argc < 2) { printf("useage: ./a.out file\n"); return -1; } fd = open(argv[1], O_RDONLY); if (fd == -1) { printf("ERROR: open failed!\n"); return -1; } p = mmap(NULL, sizeof(stu), PROT_READ, MAP_SHARED, fd, 0); if (p == MAP_FAILED) { printf("ERROR: mmap failed!\n"); return -1; } close(fd); while (1) { printf("id = %d, name = %s, gender = %c\n", p->

Id, p-> name, p-> gender); sleep (2);} munmap (p, sizeof (stu)); return 0;} after reading the above, have you mastered what the mmap function of shared memory is in Linux system programming? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report