如何在 LynxOS/POSIX 中同步对共享内存的访问?

发布于 2024-08-28 00:44:28 字数 409 浏览 11 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

獨角戲 2024-09-04 00:44:28

感谢@nos,但我想对他的回答进行一些扩展。
最后(为了清楚起见,排除错误处理)我做了如下操作:

1. 定义共享内存结构

这包含进程间同步对象和要共享的数据。

typedef struct
{
    // Synchronisation objects
    pthread_mutex_t ipc_mutex;
    pthread_cond_t  ipc_condvar;
    // Shared data
    int number;
    char data[1024];
} shared_data_t;

2. 创建共享内存并设置大小(主进程)

在主进程上创建一个新的共享内存对象:

fd = shm_open(SHAREDMEM_FILENAME, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
ftruncate(fd, sizeof(shared_data_t));

2. 或打开共享内存(从进程)

在从进程上附加到现有对象:

fd = shm_open(SHAREDMEM_FILENAME, O_RDWR, S_IRUSR|S_IWUSR);

3. Mmap 到进程空间

// Specify addr of calling address, mostly use NULL is most portable way
shared_data_t* sdata = (shared_data_t*)mmap(NULL, sizeof(shared_data_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);

4. 初始化同步变量(仅限主进程)

pthread_mutexattr_t mutex_attr;
pthread_mutexattr_init(&mutex_attr);
pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&sdata->ipc_mutex, &mutex_attr);

pthread_condattr_t cond_attr;
pthread_condattr_init(&cond_attr);
pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED);
pthread_cond_init(&sdata->ipc_condvar, &cond_attr);

就是这样。

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.

typedef struct
{
    // Synchronisation objects
    pthread_mutex_t ipc_mutex;
    pthread_cond_t  ipc_condvar;
    // Shared data
    int number;
    char data[1024];
} shared_data_t;

2. Create the shared memory and set size (Master process)

On the Master process create a new shared memory object:

fd = shm_open(SHAREDMEM_FILENAME, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
ftruncate(fd, sizeof(shared_data_t));

2. OR Open the shared memory (Slave process)

On the Slave just attach to existing object:

fd = shm_open(SHAREDMEM_FILENAME, O_RDWR, S_IRUSR|S_IWUSR);

3. Mmap into process space

// Specify addr of calling address, mostly use NULL is most portable way
shared_data_t* sdata = (shared_data_t*)mmap(NULL, sizeof(shared_data_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);

4. Init the sync variables (Master process only)

pthread_mutexattr_t mutex_attr;
pthread_mutexattr_init(&mutex_attr);
pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&sdata->ipc_mutex, &mutex_attr);

pthread_condattr_t cond_attr;
pthread_condattr_init(&cond_attr);
pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED);
pthread_cond_init(&sdata->ipc_condvar, &cond_attr);

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() and shm_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.

霞映澄塘 2024-09-04 00:44:28

创建进程共享互斥体/条件的标准方法。变量是使用设置 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.

深爱成瘾 2024-09-04 00:44:28

这是通过未命名的 POSIX 信号量(即信号量本身)完成的被放置在共享内存中。

This is done with unnamed POSIX semaphores, i.e. the semaphores themselves are placed in shared memory.

情域 2024-09-04 00:44:28

您可以使用共享内存/信号量 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文