5.1 Shared Memory
One of the simplest interprocess communication methods is using shared memory.Shared memory allows two or more processes to access the same memory as if they allcalled mallocand were returned pointers to the same actual memory.When oneprocess changes the memory, all the other processes see the modification.5.1.1 Fast Local CommunicationShared memory is the fastest form of interprocess communication because all features: 最快 无需内核参与 不用系统调用 避免复制失败processes share the same piece of memory. Access to this shared memory is as fast asaccessing a process’s nonshared memory, and it does not require a system call or entryto the kernel. It also avoids copying data unnecessarily.Because the kernel does not synchronize accesses to shared memory, you must provide
your own synchronization. For example, a process should not read from the
memory until after data is written there, and two processes must not write to the samememory location at the same time. A common strategy to avoid these race conditionsis to use semaphores, which are discussed in the next section. Our illustrative programs, | 用户程序实现进程同步 , 通常采用信号量方式though, show just a single process accessing the memory, to focus on the shared
memory mechanism and to avoid cluttering the sample code with synchronizationlogic.
5.1.2 The Memory Model
To use a shared memory segment, one process must allocate the segment.Then each 随后需要访问这个共享内存块的每一个进程都必须将这个共享内存绑定到自己的地址空间中process desiring to access the segment must attach the segment. After finishing its useof the segment, each process detaches the segment. At some point, one process mustdeallocate the segment.Understanding the Linux memory model helps explain the allocation and attachment process.Under Linux, each process’s virtual memory is split into pages. Each
process maintains a mapping from its memory addresses to these virtual memory pages,which contain the actual data. Even though each process has its own addresses, multipleprocesses’mappings can point to the same page, permitting sharing of memory.Memory pages are discussed further in Section 8.8,“The mlockFamily: LockingPhysical Memory,”of Chapter 8,“Linux System Calls.”Allocating a new shared memory segment causes virtual memory pages to be created. Because all processes desire to access the same shared segment, only one processshould allocate a new shared segment. Allocating an existing segment does not createnew pages, but it does return an identifier for the existing pages.To permit a processto use the shared memory segment, a process attaches it, which adds entries mappingfrom its virtual memory to the segment’s shared pages.When finished with the segment,these mapping entries are removed.When no more processes want to access
these shared memory segments, exactly one process must deallocate the virtual memory pages.All shared memory segments are allocated as integral multiples of the system’s pagesize, which is the number of bytes in a page of memory. On Linux systems, the pagesize is 4KB, but you should obtain this value by calling the getpagesize function.5.1.3 Allocation
A process allocates a shared memory segment using shmget(“SHared MemoryGET”). Its first parameter is an integer key that specifies which segment to create.Unrelated processes can access the same shared segment by specifying the same keyvalue. Unfortunately, other processes may have also chosen the same fixed key, whichcould lead to conflict. Using the special constant IPC_PRIVATE as the key value guaranteesthat a brand new memory segment is created
Its second parameter specifies the number of bytes in the segment. Because segments
are allocated using pages, the number of actually allocated bytes is rounded up
to an integral multiple of the page size.The third parameter is the bitwise or of flag values that specify options to shmget.The flag values include these:* IPC_CREAT—This flag indicates that a new segment should be created.This per-mits creating a new segment while specifying a key value.* IPC_EXCL—This flag, which is always used with IPC_CREAT, causes shmget to failif a segment key is specified that already exists.Therefore, it arranges for the calling process to have an “exclusive”segment. If this flag is not given and the keyof an existing segment is used,shmget returns the existing segment instead ofcreating a new one.* Mode flags—This value is made of 9 bits indicating permissions granted toowner, group, and world to control access to the segment. Execution bits areignored. An easy way to specify permissions is to use the constants defined in<sys/stat.h>and documented in the section 2 statman page.1For example,S_IRUSR and S_IWUSR specify read and write permissions for the owner of theshared memory segment, and S_IROTH and S_IWOTH specify read and write per-missions for others. For example, this invocation of shmget creates a new shared memory segment (oraccess to an existing one, if shm_key is already used) that’s readable and writeable tothe owner but not other users.int segment_id = shmget (shm_key, getpagesize (),IPC_CREAT |S_IRUSR | S_IWUSER);If the call succeeds,shmget returns a segment identifier. If the shared memory segmentalready exists, the access permissions are verified and a check is made to ensure thatthe segment is not marked for destruction. 同时会检查该内存块是否被标记为等待摧毁状态