IIPCshare memory

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 all
called mallocand were returned pointers to the same actual memory.When one
process changes the memory, all the other processes see the modification.
5.1.1 Fast Local Communication
Shared 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 as
accessing a process’s nonshared memory, and it does not require a system call or entry
to 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 same
memory location at the same time. A common strategy to avoid these race conditions
is 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 synchronization
logic.

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 use
of the segment, each process detaches the segment. At some point, one process must
deallocate 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, multiple
processes’mappings can point to the same page, permitting sharing of memory.
Memory pages are discussed further in Section 8.8,“The mlockFamily: Locking
Physical 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 process
should allocate a new shared segment. Allocating an existing segment does not create
new pages, but it does return an identifier for the existing pages.To permit a process
to use the shared memory segment, a process attaches it, which adds entries mapping
from 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 page
size, which is the number of bytes in a page of memory. On Linux systems, the page
size 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 Memory
GET”). 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 key
value. Unfortunately, other processes may have also chosen the same fixed key, which
could lead to conflict. Using the special constant IPC_PRIVATE as the key value guarantees

that 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 fail
if 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 key
of an existing segment is used,shmget returns the existing segment instead of
creating a new one.
* Mode flags—This value is made of 9 bits indicating permissions granted to
owner, group, and world to control access to the segment. Execution bits are
ignored. An easy way to specify permissions is to use the constants defined in
<sys/stat.h>and documented in the section 2 statman page.1
For example,
S_IRUSR and S_IWUSR specify read and write permissions for the owner of the
shared 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 (or
access to an existing one, if shm_key is already used) that’s readable and writeable to
the 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 segment
already exists, the access permissions are verified and a check is made to ensure that
the segment is not marked for destruction. 同时会检查该内存块是否被标记为等待摧毁状态

原文地址:https://www.cnblogs.com/michile/p/2893473.html