Linux 上的 msemaphore?
AIX(还有 HPUX,如果有人关心的话)有一个很好的小功能,称为 msemaphores,它可以轻松地同步多个进程共享的内存映射文件的细粒度部分(例如记录)。 有谁知道linux中有类似的东西吗?
需要明确的是,msemaphore 函数通过相关链接进行描述 此处。
AIX (and HPUX if anyone cares) have a nice little feature called msemaphores that make it easy to synchronize granular pieces (e.g. records) of memory-mapped files shared by multiple processes. Is anyone aware of something comparable in linux?
To be clear, the msemaphore functions are described by following the related links here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
POSIX 信号量可以放置在进程之间共享的内存中,如果
sem_init(3)
的第二个参数“pshared
”为 true。 这似乎与 msem 的作用相同。这是一个相当愚蠢的测试,但它有效:
因为信号量在两个进程之间共享,所以
pong
不会从ping
获取交错数据,尽管睡觉
s。POSIX semaphores can be placed in memory shared between processes, if the second argument to
sem_init(3)
, "pshared
", is true. This seems to be the same as whatmsem
does.This is a pretty dumb test, but it works:
Because the semaphore is shared between the two processes, the
pong
s don't get interleaved data from theping
s despite thesleep
s.这可以使用 POSIX 共享内存互斥体来完成:
现在您可以使用普通的 pthread_mutex_lock() 等调用从映射的多个进程中解锁和锁定 &some_shared_mmap_struct.mutex 。
事实上,您甚至可以按照以下方式实现 msem API:(未经测试)
This can be done using POSIX shared-memory mutexes:
Now you can unlock and lock &some_shared_mmap_structure.mutex using ordinary pthread_mutex_lock() etc calls, from multiple processes that have it mapped.
Indeed, you can even implement the msem API in terms of this: (untested)
在Linux下,你也许可以用SysV共享内存来实现你想要的; 快速谷歌搜索发现这个(相当旧的)指南可能会有所帮助。
Under Linux, you may be able to achieve what you want with SysV shared memory; quick googling turned up this (rather old) guide that may be of help.