如何在 LynxOS/POSIX 中同步对共享内存的访问?
我正在 LynxOS SE (符合 POSIX 标准)系统上实现两个进程,这些进程将进行通信通过共享内存。
一个进程将充当“生产者”,另一个进程将充当“消费者”。在多线程系统中,我的方法是使用互斥体和 condvar(条件变量)对,消费者等待 condvar(使用 pthread_cond_wait),生产者向其发出信号(使用 < code>pthread_cond_signal)当共享内存更新时。
如何在多进程而不是多线程架构中实现这一点?
是否有 LynxOS/POSIX 方法来创建可在进程之间使用的 condvar/mutex 对?
或者在这种情况下还有其他同步机制更合适吗?
I am implementing two processes on a LynxOS SE (POSIX conformant) system that will communicate via shared memory.
One process will act as a "producer" and the other a "consumer". In a multi-threaded system my approach to this would be to use a mutex and condvar (condition variable) pair, with the consumer waiting on the condvar (with pthread_cond_wait
) and the producer signalling it (with pthread_cond_signal
) when the shared memory is updated.
How do I achieve this in a multi-process, rather than multi-threaded, architecture?
Is there a LynxOS/POSIX way to create a condvar/mutex pair that can be used between processes?
Or is some other synchronization mechanism more appropriate in this scenario?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
感谢@nos,但我想对他的回答进行一些扩展。
最后(为了清楚起见,排除错误处理)我做了如下操作:
1. 定义共享内存结构
这包含进程间同步对象和要共享的数据。
2. 创建共享内存并设置大小(主进程)
在主进程上创建一个新的共享内存对象:
2. 或打开共享内存(从进程)
在从进程上附加到现有对象:
3. Mmap 到进程空间
4. 初始化同步变量(仅限主进程)
就是这样。
Mutex 和 cond 现在可以正常使用来控制对共享数据的访问。
唯一真正的问题是确保主进程在从进程启动之前已创建共享内存并初始化同步变量。并确保根据需要使用
munmap()
和shm_unlink()
进行整理。注意:XSI 替代方案
POSIX:XSI 扩展具有其他共享内存的函数(
shmget()
、shmat()
等),如果可能会更有用。 strong> 它们可用,但它们不在我正在使用的 LynxOS-SE 版本上。Credit goes to @nos, but I'd like to expand a little bit on his answer.
In the end (excluding error handling for clarity) I did as follows:
1. Define the shared memory structure
This contains the inter-process sync objects and the data to be shared.
2. Create the shared memory and set size (Master process)
On the Master process create a new shared memory object:
2. OR Open the shared memory (Slave process)
On the Slave just attach to existing object:
3. Mmap into process space
4. Init the sync variables (Master process only)
That's it.
Mutex and cond can now be used as normal to control access to the shared data.
The only real gotchas are making sure the Master process has created the shared memory and initialised the sync variables before the Slave process is started. And making sure you tidy up with
munmap()
andshm_unlink()
as required.Note: XSI Alternative
The POSIX:XSI extension has other functions for sharing memory (
shmget()
,shmat()
etc) which may be more useful if they are available, but they are not on the version of LynxOS-SE I am using.创建进程共享互斥体/条件的标准方法。变量是使用设置 pthread_mutexattr_setpshared/pthread_condattr_setpshared 的属性来初始化它们。检查 LynxOS 是否支持。
您自然需要放置这样的互斥体/条件。共享内存中的变量以某种方式存在,因此所有进程都可以使用它。
The standard way to create a process shared mutex/cond. variable is to initialize them with an attribute where you set pthread_mutexattr_setpshared/pthread_condattr_setpshared. Check if LynxOS supports that.
You'll naturally need to place such mutexes/cond. variables in shared memory somehow, so all processes can use it.
这是通过未命名的 POSIX 信号量(即信号量本身)完成的被放置在共享内存中。
This is done with unnamed POSIX semaphores, i.e. the semaphores themselves are placed in shared memory.
您可以使用共享内存/信号量 IPC 来实现此目的。
这是一篇带有很好示例的文章:
http: //www-personal.washtenaw.cc.mi.us/~chasselb/linux275/ClassNotes/ipc/shared_mem.htm
You can achive this with the shared memory/semaphore IPC.
Here's an article with a nice example:
http://www-personal.washtenaw.cc.mi.us/~chasselb/linux275/ClassNotes/ipc/shared_mem.htm