我在共享内存段中使用简单的 pthreads 进程共享互斥体来协调多个服务器实例。
代码很简单:
启动时,服务器附加到共享内存段(如果存在),或者创建它(如果不存在):
shm_open()
, mmap(MAP_SHARED)
等等
这在测试时效果很好,但是一旦部署一段时间后,我就会遇到服务器实例根本不协调的情况。我可以通过在服务器启动后删除共享内存段来复制这一点: future 的服务器实例将创建/使用一个新段,但现有的服务器实例仍保留在没有人使用的旧段中,因此实际上它与其余部分隔离...
所以我的猜测是/dev/shm 中的共享内存段以某种方式被删除,而不是被我删除。这是唯一有意义的事情......这里发生了什么?
在 Linux 4.9 上运行 debian。
最佳答案
啊,看起来像systemd罪魁祸首是:
来自dev-shm-files-disappearing :
After hours of searching and reading, I found the culprit. It's a setting for systemd. The
/etc/systemd/logind.conf
contains default configuration options, with each of them commented out. TheRemoveIPC
option is set to yes by default. That option tells systemd to clean up interprocess communication (IPC) for "user accounts" who aren't logged in. This does not affect "system accounts"In my case, the files and directories were being created for a user account, not a system account.
There are two possible solutions:
- Create the files with/for a system user -- a user created with the system option (
adduser -r
oradduser --system
)- Edit
/etc/systemd/logind.conf
, uncomment the lineRemoveIPC=yes
, change it toRemoveIPC=no
, save, and reboot the systemIn my case, I went with option #2 because the user was already created.
另请参阅this post在 postgresql 邮件列表上。
关于c - 共享内存段被删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49979349/