共享内存 Reader(只读)在 Writer 终止时挂起

发布于 2024-11-09 03:52:22 字数 207 浏览 0 评论 0原文

在linux中,C,使用共享内存,我有一个Writer进程和许多Readers。

当 Writer 终止时,任何正在运行的 Reader 都会挂起。 Writer 在终止时确实会执行 shm_unlink 操作。

我认为内核知道读者并且他们可以继续阅读,这可能是错误的。当然数据不会改变,所以读者可以知道什么时候优雅地结束。

有什么想法如何实现这一点?

In linux, C, using shared memory, I have a single Writer process and many Readers.

When the Writer terminates, any running Readers hang. The Writer does do shm_unlink when it terminates.

I thought, may be wrongly, that the kernel knows about the Readers and that they can continue to read. Of course the data will not change, so the Readers can know when to end gracefully.

Any ideas how to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

挖鼻大婶 2024-11-16 03:52:22

您可以在共享内存中创建的标头中添加一个标志来指示 EOF。您可以定期更新时间戳以检测写入者不干净的终止/

您还可以使用 shmctl 找出附加人员的数量,这可能会帮助您确定写入者是否已经消失。

您还可以尝试重新附加/fstat来验证该段尚未被删除。

如果这些答案不能满足,请告诉我们是 POSIX 还是 System V 共享内存。

You can have a flag in a header you create in the shared memory to indicate EOF. You could have a timestamp you update periodically to detect writer unclean termination/

You can also use shmctl to find out the number of attached people, which might help you figure out if the writer has gone away.

You can also try to re-attach/fstat to validate that the segment has not been deleted.

If these answers do not satisfy, please let us know whether it is POSIX or System V shared memory.

緦唸λ蓇 2024-11-16 03:52:22

在 Linux 中,内核不知道你的读者或作者(或者,它不知道哪个是哪个)。 shm_unlink 只是删除名称;在所有用户取消映射并关闭或终止之前,它不会删除 shmem 段。如果您让这些读取器/写入器通过 pshared 互斥体进行通信和阻止/semaphores,那么当作者去世时,你确实可能会遇到挂起的读者。最好让作者在队列中留下一条消息,告诉读者它正在终止。

In linux, the kernel doesn't know about your readers or writers (or, well, it doesn't know which is which). And shm_unlink just removes the name; it doesn't delete the shmem segment until all users have unmapped and closed it or terminated. If you're having these readers/writers communicate and block through pshared mutexes/semaphores, then when the writer dies you could indeed end up with hanging readers. It's best to have the writer leave a message in the queue to tell the readers that it's terminating.

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