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

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值