Linux进程本地存储

发布于 2024-10-20 21:42:37 字数 541 浏览 2 评论 0原文

存储与进程关联的值的最佳 Linux 方法是什么?

我们有一个动态加载和卸载的库。运行时,该库将创建一个大型数据结构......大约 1GB。当库被卸载时,它将该数据结构留在进程的内存中。当进程重新加载库时,我们需要库能够找到该数据结构的地址。我们这样做是因为我们的服务器软件更新是实时发生的,我们没有时间创建该数据结构。当更新失败时,我们也会取消更新,因此这可以让我们快速回退到以前的版本。我们无法更改调用应用程序的代码,因此它可以向我们传递数据结构的地址。

pthreads 有线程本地存储。我正在寻找类似于线程本地存储的东西,除了它是进程本地存储。我不想在临时目录中创建文件,因为我们的服务器有时会崩溃并重新启动......我不想处理必须清理死进程数据文件的问题。我正在查看 /proc/ 目录,这是理想的,因为它在进程终止后消失,但我不确定 proc_fs.h 内的函数是否应该从用户态应用程序调用。

谢谢!

What is the best linux way to store a value associated with a process.

We have a library that we dynamically load and unload. While running, the library will create a large data structure....about 1GB. When the library is unloaded it leaves that data structure in the process' memory. When the process reloads the library we need the library to be able to find the address of this data structure. We do this because updates to our server software occur in real time and can't afford the time to create that data structure. we also un-update when an update fails, so this lets us fall back to a previous version quickly. We do not have the ability to change the code of the calling application so it could pass us the address of the data structure.

pthreads has thread local storage. I am looking for something analogous to thread local storage except it would be process local storage. I don't want to create a file in a temp dir because our servers crash and restart from time to time....I don't want to deal with having to clean dead process data files. I was looking at the /proc/ directory, which would be ideal since it goes away after a process terminates, but I am unsure if functions inside proc_fs.h are meant to be called from a user-land application.

Thanks!

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

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

发布评论

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

评论(1

对你而言 2024-10-27 21:42:37

加载库的可执行文件不能只为您保留这个,并在加载库后传递它?

无论哪种方式,一些替代方法是:

  • 扫描 /proc/self/maps 查找数据 - 取决于您如何分配它。
  • 打开一个文件,dup2() 将其写入“神奇”fd,rm 该文件,然后将状态数据写入其中。关闭后它会完全消失。
  • 加载一个小型库,其唯一目的是为您存储该地址。重新加载时再次dlopen(),并向其询问地址。
  • 设置/读取环境变量

我个人会选择环境变量。

The executable that loads the library can't just keep this for you, and pass it after loading the library?

Either way, some alternatives are:

  • Scan /proc/self/maps for the data - depending on how you allocated it.
  • Open a file, dup2() it to a "magic" fd, rm the file and then write your state data to it. It'll go away completely when closed.
  • Load a tiny library which has the sole purpose of storing this address for you. dlopen() it again when reloaded, and ask it for the address.
  • Set/read an environment variable

Personally I'd go with the environment variable.

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