c - 共享内存段被删除?

标签 c linux pthreads shared-memory mmap

我在共享内存段中使用简单的 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. The RemoveIPC 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:

  1. Create the files with/for a system user -- a user created with the system option (adduser -r or adduser --system)
  2. Edit /etc/systemd/logind.conf, uncomment the line RemoveIPC=yes, change it to RemoveIPC=no, save, and reboot the system

In my case, I went with option #2 because the user was already created.

另请参阅this post在 postgresql 邮件列表上。

关于c - 共享内存段被删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49979349/

相关文章:

linux - RPM 包允许安装不同的文件集

c - 卡在执行多线程(Windows 上的 pthread)

c++ - 什么时候调用 CloseHandle?

c - 如何在 C 中读取 .csv 文件

c - C 函数中的简单函数

linux - 如何在 Linux 中获得内存修改通知

linux - 差异两个转速? -- Linux

c++ - 在 main() 中的 pthread_join 之后出现段错误

c - Slax-Linux上的.c文件

c - 在 C 中为结构分配正确的值时遇到问题