共享内存 Reader(只读)在 Writer 终止时挂起
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在共享内存中创建的标头中添加一个标志来指示 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.
在 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.