博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IIPC--share memory
阅读量:6976 次
发布时间:2019-06-27

本文共 5443 字,大约阅读时间需要 18 分钟。

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/archive/2013/02/05/2893473.html

你可能感兴趣的文章
【算法拾遗】大数相加(不开辟额外空间)
查看>>
Python正则表达式精讲
查看>>
Python——Beautiful Soup 4.2.0 中文文档
查看>>
Python字符串拼接、截取及替换方法总结
查看>>
3.Session安装配置
查看>>
MXNet学习:预测结果-识别单张图片
查看>>
JAVA微信开发-新手接入指南
查看>>
作业一
查看>>
virsh命令来创建虚拟机
查看>>
7.1.11、each 指针复位
查看>>
DPDK-----初识
查看>>
说说 PWA 和微信小程序--Progressive Web App
查看>>
kill命令"-1"这个参数到底是杀进程还是reload?(转)
查看>>
struts2 result type=(chain、dispatcher、redirect、redirect-action)
查看>>
mysql foreign key(外键)
查看>>
Good Bye 2016 - C
查看>>
关于技术型人才与研究型人才
查看>>
GDB调试程序(完全手册)
查看>>
httpd: apr_sockaddr_info_get() failed for centossvn
查看>>
《Head First 统计学》读书笔记
查看>>