Linux进程本地存储 [英] linux process localstorage

查看:69
本文介绍了Linux进程本地存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux中,存储与进程关联的值的最佳方法是什么.

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

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

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

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.

谢谢!

推荐答案

加载库的可执行文件不能只保留给您,而是在加载库后传递给它?

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:

  • 扫描/proc/self/maps以查找数据-取决于您如何分配数据.
  • 打开一个文件,将其dup2()转换为魔术"文件,将其保留,然后将状态数据写入其中.关闭后它将完全消失.
  • 加载一个小型库,其唯一目的是为您存储此地址. dlopen()重新加载时再次输入,并要求其提供地址.
  • 设置/读取环境变量
  • 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.

这篇关于Linux进程本地存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆