The shared memory area is the fastest available in the IPC form . Once the memory area is mapped to the address space of the process that shares it, the transfer of inter-process data no longer involves the kernel . However, there is usually some form of synchronization between the processes that store or take information from the shared memory area. No longer involves the kernel: the process no longer passes data to each other by running a system tune into the kernel. The kernel must establish a memory-mapping relationship that agrees that each process shares the memory area. The memory area is then managed.
By default, a child process that is derived from fork does not share the memory area with its parent process.
The MMAP function maps a file or a POSIX shared memory area object to the address space of the calling process. The purpose of using this function is to:
1. Use normal files to provide memory mapped I/O.
2. Use special files to provide anonymous memory mappings.
3. Use Shm_open to provide a POSIX shared memory area between unrelated processes.
#include <sys/mman.h>void *mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset);
addr Specifies the starting address of the in-process space to which the descriptive descriptor FD should be mapped.
If the pointer is null, the kernel chooses the start address by itself. In either case. The return value of the function is the start address of the descriptive descriptor FD that is mapped to the memory area.
Len is the number of bytes mapped into the calling process address space, which starts at the first offset byte from the beginning of the mapped file.
Offset is usually set to 0.
The prot parameter specifies the protection of the memory area mapping area.
There are four types: prot_read (data readable), Prot_write (data writable), prot_exec (data can be run), Prot_none (data not accessible).
Flags have the following three kinds: map_shared (change is shared), map_private (change is private), map_fixed (accurate explanation of addr parameters).
One of the methods of sharing memory areas between parent and child processes is that the parent process specifies map_shared call Mmap before calling fork.
After successful return of MMAP, FD parameters can be closed. This operation has no effect on the mapping relationship established by Mmap.
To delete a mapping relationship for the address space of a process, we call Munmap.
(int munmap (void *addr, size_t len))
Call Msync to run synchronization.
(int msync (void *addr, size_t len, int flags))
Note: not all files can process memory mappings, for example: attempting to map a descriptive descriptor of an access terminal or socket to memory causes Mmap to return an error. These types of descriptive descriptors must be interviewed using read and write.
Another use of mmap is to provide a shared memory area between unrelated processes.
Program Examples:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <semaphore.h> #include < fcntl.h> #include <sys/stat.h> #include <sys/mman.h> #define SEM_NAME "Mysem" #define File_mode (s_irusr| s_iwusr| s_irgrp| S_iroth) int main (int argc, char **argv) {intfd, I, nloop, zero = 0;int*ptr;sem_t*mutex;if (argc! = 3) {printf ("Usage:incrl &L t;pathname> < #nloops >\n "); return-1;} Nloop = Atoi (argv[2])/*open file, initialize to 0, map into memory*/fd = open (argv[1], O_RDWR | O_creat, File_mode); write (fd, &zero, sizeof (int));p tr = mmap (NULL, sizeof (int), Prot_read | Prot_write, map_shared, FD, 0); close (FD);/*create, initialize, and unlink Semaphore*/mutex = Sem_open (sem_name, O_creat | O_EXCL, File_mode, 1); if (Mutex < 0) {printf ("Sem_open error.\n"); return-1;} Sem_unlink (Sem_name); Setbuf (stdout, NULL),/*stdout is unbuffered*/if (fork () = = 0) {for (i = 0; i < Nloop; i++) {sem_wait ( Mutex);p rintf ("child:%d\n", (*ptr) + +); Sem_post (mutex);} Exit (0);} /*pareNt*/for (i = 0; i < Nloop; i++) {sem_wait (mutex);p rintf ("parent:%d\n", (*ptr) + +); Sem_post (mutex);} Exit (0);}
Attention:when memory maps a normal file, the size of the mapped area in memory is usually equal to the size of the file. However, the file size and memory map area size can vary.
Linux environment programming shared memory Area (a): Shared memory Area Simple introduction