将值存储在独立于进程的内存中

发布于 2024-09-02 09:27:48 字数 254 浏览 4 评论 0 原文

我需要一种方法来通过进程 A 暂时存储一个值。进程 A 可以在将值存储在内存中后退出。一段时间后,进程 B 访问内存的同一位置并读取值。我需要存储在内存中,因为我不希望数据在重新启动后保持不变。但只要系统正常运行,数据就必须能够独立于进程进行访问。我在 Windows 中尝试了 MailSlots 和临时文件,两者似乎都存在问题,进程引用计数降至零,实体不会保留在内存中。在 Windows 中最好使用 Win32 API,什么是合适的机制?

  • 加内什

I need a way to store a value somewhere for temporarily by say Process A. Process A can exit the after storing the value in memory. After sometime Process B comes accesses the same location of memory and read the value. I need to store in memory, because I dont want the data to persistent across reboots. But as long as the system is up, it Independent of the Process the data must be accessible. I tried MailSlots and Temporary files in windows, both seem to have problem where the process reference count drops to zero , the entities dont persist in memory. What is a suitable mechanism for this in Windows preferably using Win32 API?

  • Ganesh

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

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

发布评论

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

评论(4

我很OK 2024-09-09 09:27:48

编写一个在启动时启动的服务,并让它创建一些共享内存。
然后进程 A 可以填充该共享内存,之后进程 B 可以读取它。

如果您的系统重新启动,共享内存就会消失,您将拥有一块全新的共享内存。
确保您的服务正确“初始化”共享内存。

Write a service that is started at boot time, and let it create some shared memory.
This shared memory can then be filled by process A, and process B can read it afterwards.

If your system is rebooted, the shared memory is gone and you have a fresh, new piece of shared memory.
Make sure that your service correctly 'initializes' the shared memory.

我恋#小黄人 2024-09-09 09:27:48

当 ProcessA 退出时,数据必须驻留在内存中而不是存储在磁盘上的某个位置,是否有原因?当您提到临时文件时,我问这些文件应该可以工作,除非 ProcessA 以意外方式失败。

根据您的需求,提供共享/快速/原子数据的好方法是通过 ESENT API

Is there a reason why the data must be resident in memory when ProcessA quits as opposed to being stored somewhere on disk? I ask as you mention temporary files which should work unless ProcessA fails in an unexpected way.

Depending on your needs a nice way to provide shared/fast/atomic data is via the ESENT API.

背叛残局 2024-09-09 09:27:48

尝试以下操作。我不能说我知道这有效,但看起来很合理。

使用 OpenFileMapping< 在全局命名空间中创建共享内存文件/a>.然后调用 Duplicatehandle 并获取目标进程句柄使用某个比进程 A 生存时间更长的进程。您可以将句柄添加到 winlogon.exe,这应该可以防止共享内存在进程 A 终止时被破坏。然后在进程B中就可以查找共享内存文件了。

Try the following. I can't say I know this works, but it seems reasonable.

Create a shared memory file in the global namespace using OpenFileMapping. Then call Duplicatehandle and for the target process handle use some process that will live longer than process A. You may be able to add the handle to winlogon.exe This should stop the shared memory from being destroyed when process A terminates. Then in process B you can look up the shared memory file.

摇划花蜜的午后 2024-09-09 09:27:48

好吧,我设法在一个不退出的进程上创建一个 MailSlot,其他两个进程可以作为客户端读取和写入 MailSlot 服务器...即使客户端退出,Mailslot 仍然会有数据... MailSlot 服务器使我能够在 MailSlot 服务器进程启动时将数据存储在易失性内存中..或者操作系统启动..并在操作系统重新启动时消失...感谢所有的想法和帮助....:)

Well, I managed to a create a MailSlot on a Process which doesnt exit, the other two Processes can read and write to the MailSlot server as clients... Even if the clients exit, the Mailslot will still have the data... the MailSlot server enables me to store data in volatile memory has long as the MailSlot server process is up.. or the OS is up.. and vanishes on OS reboot... Thanks for all the ideas and help.... :)

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